|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase; |
15 | 15 | use Symfony\Component\Config\FileLocator; |
| 16 | +use Symfony\Component\Config\Loader\LoaderResolver; |
16 | 17 | use Symfony\Component\Config\Resource\FileResource; |
| 18 | +use Symfony\Component\Routing\Loader\AnnotationClassLoader; |
17 | 19 | use Symfony\Component\Routing\Loader\PhpFileLoader; |
| 20 | +use Symfony\Component\Routing\Loader\Psr4DirectoryLoader; |
18 | 21 | use Symfony\Component\Routing\Route; |
19 | 22 | use Symfony\Component\Routing\RouteCollection; |
| 23 | +use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\MyController; |
20 | 24 |
|
21 | 25 | class PhpFileLoaderTest extends TestCase |
22 | 26 | { |
@@ -297,4 +301,51 @@ public function testImportingAliases() |
297 | 301 |
|
298 | 302 | $this->assertEquals($expectedRoutes('php'), $routes); |
299 | 303 | } |
| 304 | + |
| 305 | + /** |
| 306 | + * @dataProvider providePsr4ConfigFiles |
| 307 | + */ |
| 308 | + public function testImportAttributesWithPsr4Prefix(string $configFile) |
| 309 | + { |
| 310 | + $locator = new FileLocator(\dirname(__DIR__).'/Fixtures'); |
| 311 | + new LoaderResolver([ |
| 312 | + $loader = new PhpFileLoader($locator), |
| 313 | + new Psr4DirectoryLoader($locator), |
| 314 | + new class() extends AnnotationClassLoader { |
| 315 | + protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot) |
| 316 | + { |
| 317 | + $route->setDefault('_controller', $class->getName().'::'.$method->getName()); |
| 318 | + } |
| 319 | + }, |
| 320 | + ]); |
| 321 | + |
| 322 | + $route = $loader->load($configFile)->get('my_route'); |
| 323 | + $this->assertSame('/my-prefix/my/route', $route->getPath()); |
| 324 | + $this->assertSame(MyController::class.'::__invoke', $route->getDefault('_controller')); |
| 325 | + } |
| 326 | + |
| 327 | + public function providePsr4ConfigFiles(): array |
| 328 | + { |
| 329 | + return [ |
| 330 | + ['psr4-attributes.php'], |
| 331 | + ['psr4-controllers-redirection.php'], |
| 332 | + ]; |
| 333 | + } |
| 334 | + |
| 335 | + public function testImportAttributesFromClass() |
| 336 | + { |
| 337 | + new LoaderResolver([ |
| 338 | + $loader = new PhpFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures')), |
| 339 | + new class() extends AnnotationClassLoader { |
| 340 | + protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot) |
| 341 | + { |
| 342 | + $route->setDefault('_controller', $class->getName().'::'.$method->getName()); |
| 343 | + } |
| 344 | + }, |
| 345 | + ]); |
| 346 | + |
| 347 | + $route = $loader->load('class-attributes.php')->get('my_route'); |
| 348 | + $this->assertSame('/my-prefix/my/route', $route->getPath()); |
| 349 | + $this->assertSame(MyController::class.'::__invoke', $route->getDefault('_controller')); |
| 350 | + } |
300 | 351 | } |
0 commit comments