RouteTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Tests\Annotation;
  11. use Doctrine\Common\Annotations\AnnotationReader;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\FooController;
  16. use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\FooController as FooAttributesController;
  17. class RouteTest extends TestCase
  18. {
  19. use ExpectDeprecationTrait;
  20. private function getMethodAnnotation(string $method, bool $attributes): Route
  21. {
  22. $class = $attributes ? FooAttributesController::class : FooController::class;
  23. $reflection = new \ReflectionMethod($class, $method);
  24. if ($attributes) {
  25. $attributes = $reflection->getAttributes(Route::class);
  26. $route = $attributes[0]->newInstance();
  27. } else {
  28. $reader = new AnnotationReader();
  29. $route = $reader->getMethodAnnotation($reflection, Route::class);
  30. }
  31. if (!$route instanceof Route) {
  32. throw new \Exception('Can\'t parse annotation');
  33. }
  34. return $route;
  35. }
  36. public static function provideDeprecationArrayAsFirstArgument()
  37. {
  38. return [
  39. ['requirements', ['locale' => 'en'], 'getRequirements'],
  40. ['options', ['compiler_class' => 'RouteCompiler'], 'getOptions'],
  41. ['name', 'blog_index', 'getName'],
  42. ['defaults', ['_controller' => 'MyBlogBundle:Blog:index'], 'getDefaults'],
  43. ['schemes', ['https'], 'getSchemes'],
  44. ['methods', ['GET', 'POST'], 'getMethods'],
  45. ['host', '{locale}.example.com', 'getHost'],
  46. ['condition', 'context.getMethod() == "GET"', 'getCondition'],
  47. ['value', '/Blog', 'getPath'],
  48. ['value', ['nl' => '/hier', 'en' => '/here'], 'getLocalizedPaths'],
  49. ];
  50. }
  51. /**
  52. * @group legacy
  53. *
  54. * @dataProvider provideDeprecationArrayAsFirstArgument
  55. */
  56. public function testDeprecationArrayAsFirstArgument(string $parameter, $value, string $getter)
  57. {
  58. $this->expectDeprecation('Since symfony/routing 5.3: Passing an array as first argument to "Symfony\Component\Routing\Annotation\Route::__construct" is deprecated. Use named arguments instead.');
  59. $route = new Route([$parameter => $value]);
  60. $this->assertEquals($route->$getter(), $value);
  61. }
  62. /**
  63. * @requires PHP 8
  64. *
  65. * @dataProvider getValidParameters
  66. */
  67. public function testLoadFromAttribute(string $methodName, string $getter, $expectedReturn)
  68. {
  69. $route = $this->getMethodAnnotation($methodName, true);
  70. $this->assertEquals($route->$getter(), $expectedReturn);
  71. }
  72. /**
  73. * @dataProvider getValidParameters
  74. */
  75. public function testLoadFromDoctrineAnnotation(string $methodName, string $getter, $expectedReturn)
  76. {
  77. $route = $this->getMethodAnnotation($methodName, false);
  78. $this->assertEquals($route->$getter(), $expectedReturn);
  79. }
  80. public static function getValidParameters(): iterable
  81. {
  82. return [
  83. ['simplePath', 'getPath', '/Blog'],
  84. ['localized', 'getLocalizedPaths', ['nl' => '/hier', 'en' => '/here']],
  85. ['requirements', 'getRequirements', ['locale' => 'en']],
  86. ['options', 'getOptions', ['compiler_class' => 'RouteCompiler']],
  87. ['name', 'getName', 'blog_index'],
  88. ['defaults', 'getDefaults', ['_controller' => 'MyBlogBundle:Blog:index']],
  89. ['schemes', 'getSchemes', ['https']],
  90. ['methods', 'getMethods', ['GET', 'POST']],
  91. ['host', 'getHost', '{locale}.example.com'],
  92. ['condition', 'getCondition', 'context.getMethod() == \'GET\''],
  93. ];
  94. }
  95. }