- 添加路由注解基础类 Get、Post、Put、Delete - 实现路由前缀和版本控制注解 Prefix、Version - 添加中间件和路由名称注解 Middleware、Name - 创建路由服务提供者 RouteServiceProvider - 实现控制器目录扫描和路由自动注册 - 添加配置文件支持控制器目录自定义 - 完善单元测试和集成测试用例 - 添加测试控制器和相关测试代码 - 配置 composer 自动加载和 laravel 服务提供者 - 添加 phpunit 测试配置和基础测试用例
146 lines
4.7 KiB
PHP
146 lines
4.7 KiB
PHP
<?php
|
||
|
||
namespace Jltx\Routes\Tests\Feature;
|
||
|
||
use Jltx\Routes\Attribute\Get;
|
||
use Jltx\Routes\Attribute\Middleware;
|
||
use Jltx\Routes\Attribute\Post;
|
||
use Jltx\Routes\Attribute\Prefix;
|
||
use Jltx\Routes\Attribute\Version;
|
||
use Jltx\Routes\Tests\Fixtures\Controllers\TestController;
|
||
use Jltx\Routes\Tests\TestCase;
|
||
use ReflectionClass;
|
||
|
||
class RouteRegistrationTest extends TestCase
|
||
{
|
||
/** @test */
|
||
public function it_can_scan_controller_and_extract_attributes()
|
||
{
|
||
// 测试控制器类的属性提取
|
||
$reflection = new ReflectionClass(TestController::class);
|
||
|
||
// 检查类级别的属性
|
||
$prefixAttrs = $reflection->getAttributes(Prefix::class);
|
||
$versionAttrs = $reflection->getAttributes(Version::class);
|
||
$middlewareAttrs = $reflection->getAttributes(Middleware::class);
|
||
|
||
$this->assertCount(1, $prefixAttrs);
|
||
$this->assertCount(1, $versionAttrs);
|
||
$this->assertCount(1, $middlewareAttrs);
|
||
|
||
// 检查方法级别的属性
|
||
$methods = $reflection->getMethods();
|
||
|
||
// 查找 getUsers 方法
|
||
$getUsersMethod = null;
|
||
foreach ($methods as $method) {
|
||
if ($method->getName() === 'getUsers') {
|
||
$getUsersMethod = $method;
|
||
break;
|
||
}
|
||
}
|
||
|
||
$this->assertNotNull($getUsersMethod);
|
||
$getAttrs = $getUsersMethod->getAttributes(Get::class);
|
||
$this->assertCount(1, $getAttrs);
|
||
}
|
||
|
||
/** @test */
|
||
public function it_can_create_route_attribute_instances()
|
||
{
|
||
// 测试创建路由属性实例
|
||
$reflection = new ReflectionClass(TestController::class);
|
||
$methods = $reflection->getMethods();
|
||
|
||
// 查找 createUser 方法
|
||
$createUserMethod = null;
|
||
foreach ($methods as $method) {
|
||
if ($method->getName() === 'createUser') {
|
||
$createUserMethod = $method;
|
||
break;
|
||
}
|
||
}
|
||
|
||
$this->assertNotNull($createUserMethod);
|
||
|
||
// 获取 Post 属性实例
|
||
$postAttrs = $createUserMethod->getAttributes(Post::class);
|
||
$this->assertCount(1, $postAttrs);
|
||
|
||
$postInstance = $postAttrs[0]->newInstance();
|
||
$this->assertInstanceOf(Post::class, $postInstance);
|
||
$this->assertEquals('users', $postInstance->path);
|
||
}
|
||
|
||
/** @test */
|
||
public function it_supports_multiple_middleware_attributes()
|
||
{
|
||
// 测试重复的中间件属性支持
|
||
$reflection = new ReflectionClass(TestController::class);
|
||
$methods = $reflection->getMethods();
|
||
|
||
// 查找 createUser 方法(应该有两个中间件:类级别的 auth 和方法级别的 admin)
|
||
$createUserMethod = null;
|
||
foreach ($methods as $method) {
|
||
if ($method->getName() === 'createUser') {
|
||
$createUserMethod = $method;
|
||
break;
|
||
}
|
||
}
|
||
|
||
$this->assertNotNull($createUserMethod);
|
||
|
||
$middlewareAttrs = $createUserMethod->getAttributes(Middleware::class);
|
||
// 方法级别应该有一个 Middleware 属性
|
||
$this->assertCount(1, $middlewareAttrs);
|
||
|
||
// 类级别的 Middleware 属性也需要被检测到
|
||
$classMiddlewareAttrs = $reflection->getAttributes(Middleware::class);
|
||
$this->assertCount(1, $classMiddlewareAttrs);
|
||
}
|
||
|
||
/** @test */
|
||
public function it_can_override_version_attribute()
|
||
{
|
||
// 测试版本属性覆盖
|
||
$reflection = new ReflectionClass(TestController::class);
|
||
$methods = $reflection->getMethods();
|
||
|
||
// 查找 getPost 方法(应该覆盖类级别的版本)
|
||
$getPostMethod = null;
|
||
foreach ($methods as $method) {
|
||
if ($method->getName() === 'getPost') {
|
||
$getPostMethod = $method;
|
||
break;
|
||
}
|
||
}
|
||
|
||
$this->assertNotNull($getPostMethod);
|
||
|
||
$versionAttrs = $getPostMethod->getAttributes(Version::class);
|
||
$this->assertCount(1, $versionAttrs);
|
||
|
||
$versionInstance = $versionAttrs[0]->newInstance();
|
||
$this->assertEquals('v3', $versionInstance->version);
|
||
}
|
||
|
||
/** @test */
|
||
public function it_can_parse_class_file_for_namespace_and_class_name()
|
||
{
|
||
// 测试从文件中解析命名空间和类名
|
||
$content = file_get_contents(__DIR__.'/../Fixtures/Controllers/TestController.php');
|
||
|
||
$namespace = null;
|
||
$class = null;
|
||
|
||
if (preg_match('/namespace\s+(.+?);/', $content, $ns) &&
|
||
preg_match('/class\s+(\w+)/', $content, $cls)) {
|
||
$namespace = $ns[1];
|
||
$class = $cls[1];
|
||
}
|
||
|
||
$this->assertEquals('Jltx\Routes\Tests\Fixtures\Controllers', $namespace);
|
||
$this->assertEquals('TestController', $class);
|
||
}
|
||
}
|