routes/tests/Unit/RouteServiceProviderTest.php
yeyixianyang 289bc41677 feat(routes): 初始化路由注解系统
- 添加路由注解基础类 Get、Post、Put、Delete
- 实现路由前缀和版本控制注解 Prefix、Version
- 添加中间件和路由名称注解 Middleware、Name
- 创建路由服务提供者 RouteServiceProvider
- 实现控制器目录扫描和路由自动注册
- 添加配置文件支持控制器目录自定义
- 完善单元测试和集成测试用例
- 添加测试控制器和相关测试代码
- 配置 composer 自动加载和 laravel 服务提供者
- 添加 phpunit 测试配置和基础测试用例
2025-12-06 21:13:27 +08:00

68 lines
2.4 KiB
PHP

<?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);
}
}