diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/class-attributes.php b/src/Symfony/Component/Routing/Tests/Fixtures/class-attributes.php new file mode 100644 index 0000000000000..3ed343e7a8c60 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/class-attributes.php @@ -0,0 +1,14 @@ +import( + resource: MyController::class, + type: 'attribute', + ) + ->prefix('/my-prefix'); +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/psr4-attributes.php b/src/Symfony/Component/Routing/Tests/Fixtures/psr4-attributes.php new file mode 100644 index 0000000000000..b11b9c52b21ba --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/psr4-attributes.php @@ -0,0 +1,15 @@ +import( + resource: [ + 'path' => './Psr4Controllers', + 'namespace' => 'Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers', + ], + type: 'attribute', + ) + ->prefix('/my-prefix'); +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/psr4-controllers-redirection.php b/src/Symfony/Component/Routing/Tests/Fixtures/psr4-controllers-redirection.php new file mode 100644 index 0000000000000..7c9a1c31d8aa4 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/psr4-controllers-redirection.php @@ -0,0 +1,7 @@ +import('psr4-controllers-redirection/psr4-attributes.php'); +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/psr4-controllers-redirection/psr4-attributes.php b/src/Symfony/Component/Routing/Tests/Fixtures/psr4-controllers-redirection/psr4-attributes.php new file mode 100644 index 0000000000000..a545475eb24d1 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/psr4-controllers-redirection/psr4-attributes.php @@ -0,0 +1,15 @@ +import( + resource: [ + 'path' => '../Psr4Controllers', + 'namespace' => 'Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers', + ], + type: 'attribute', + ) + ->prefix('/my-prefix'); +}; diff --git a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php index d069102b9e8f3..4d260ce99916a 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php @@ -13,10 +13,14 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Config\FileLocator; +use Symfony\Component\Config\Loader\LoaderResolver; use Symfony\Component\Config\Resource\FileResource; +use Symfony\Component\Routing\Loader\AnnotationClassLoader; use Symfony\Component\Routing\Loader\PhpFileLoader; +use Symfony\Component\Routing\Loader\Psr4DirectoryLoader; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\MyController; class PhpFileLoaderTest extends TestCase { @@ -297,4 +301,51 @@ public function testImportingAliases() $this->assertEquals($expectedRoutes('php'), $routes); } + + /** + * @dataProvider providePsr4ConfigFiles + */ + public function testImportAttributesWithPsr4Prefix(string $configFile) + { + $locator = new FileLocator(\dirname(__DIR__).'/Fixtures'); + new LoaderResolver([ + $loader = new PhpFileLoader($locator), + new Psr4DirectoryLoader($locator), + new class() extends AnnotationClassLoader { + protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot) + { + $route->setDefault('_controller', $class->getName().'::'.$method->getName()); + } + }, + ]); + + $route = $loader->load($configFile)->get('my_route'); + $this->assertSame('/my-prefix/my/route', $route->getPath()); + $this->assertSame(MyController::class.'::__invoke', $route->getDefault('_controller')); + } + + public function providePsr4ConfigFiles(): array + { + return [ + ['psr4-attributes.php'], + ['psr4-controllers-redirection.php'], + ]; + } + + public function testImportAttributesFromClass() + { + new LoaderResolver([ + $loader = new PhpFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures')), + new class() extends AnnotationClassLoader { + protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot) + { + $route->setDefault('_controller', $class->getName().'::'.$method->getName()); + } + }, + ]); + + $route = $loader->load('class-attributes.php')->get('my_route'); + $this->assertSame('/my-prefix/my/route', $route->getPath()); + $this->assertSame(MyController::class.'::__invoke', $route->getDefault('_controller')); + } }