routes/tests/Unit/RouteServiceProviderTest.php

68 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Jltx\Routes\Tests\Unit;
use Jltx\Routes\Attribute\Delete;
use Jltx\Routes\Attribute\Get;
use Jltx\Routes\Attribute\Middleware;
use Jltx\Routes\Attribute\Post;
use Jltx\Routes\Attribute\Prefix;
use Jltx\Routes\Attribute\Put;
use Jltx\Routes\Attribute\Version;
use Jltx\Routes\Providers\RouteServiceProvider;
use Jltx\Routes\Tests\TestCase;
use ReflectionClass;
class RouteServiceProviderTest extends TestCase
{
/** @test */
public function it_can_be_instantiated()
{
// 简单测试类可以被加载
$this->assertTrue(class_exists(RouteServiceProvider::class));
}
/** @test */
public function it_has_all_required_route_attributes()
{
// 测试所有必需的路由属性类是否存在
$this->assertTrue(class_exists(Get::class));
$this->assertTrue(class_exists(Post::class));
$this->assertTrue(class_exists(Put::class));
$this->assertTrue(class_exists(Delete::class));
$this->assertTrue(class_exists(Prefix::class));
$this->assertTrue(class_exists(Version::class));
$this->assertTrue(class_exists(Middleware::class));
}
/** @test */
public function route_attributes_have_correct_targets()
{
// 测试路由属性的目标是否正确
$getAttribute = new ReflectionClass(Get::class);
$postAttribute = new ReflectionClass(Post::class);
$prefixAttribute = new ReflectionClass(Prefix::class);
$versionAttribute = new ReflectionClass(Version::class);
$middlewareAttribute = new ReflectionClass(Middleware::class);
// Get 和 Post 应该只能用于方法
$getAttributes = $getAttribute->getAttributes(\Attribute::class);
$this->assertNotEmpty($getAttributes);
$postAttributes = $postAttribute->getAttributes(\Attribute::class);
$this->assertNotEmpty($postAttributes);
// Prefix 可以用于类和方法
$prefixAttributes = $prefixAttribute->getAttributes(\Attribute::class);
$this->assertNotEmpty($prefixAttributes);
// Version 可以用于类和方法
$versionAttributes = $versionAttribute->getAttributes(\Attribute::class);
$this->assertNotEmpty($versionAttributes);
// Middleware 可以用于类和方法,并且是可重复的
$middlewareAttributes = $middlewareAttribute->getAttributes(\Attribute::class);
$this->assertNotEmpty($middlewareAttributes);
}
}