|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Routing\Tests\Annotation; |
13 | 13 |
|
| 14 | +use Doctrine\Common\Annotations\AnnotationReader; |
14 | 15 | use PHPUnit\Framework\TestCase; |
15 | 16 | use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; |
16 | 17 | use Symfony\Component\Routing\Annotation\Route; |
| 18 | +use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\FooController; |
| 19 | +use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\FooController as FooAttributesController; |
17 | 20 |
|
18 | 21 | class RouteTest extends TestCase |
19 | 22 | { |
20 | 23 | use ExpectDeprecationTrait; |
21 | 24 |
|
22 | | - /** |
23 | | - * @group legacy |
24 | | - */ |
25 | | - public function testInvalidRouteParameter() |
| 25 | + private function getMethodAnnotation(string $method, bool $attributes): Route |
26 | 26 | { |
27 | | - $this->expectException(\BadMethodCallException::class); |
28 | | - new Route(['foo' => 'bar']); |
| 27 | + $class = $attributes ? FooAttributesController::class : FooController::class; |
| 28 | + $reflection = new \ReflectionMethod($class, $method); |
| 29 | + |
| 30 | + if ($attributes) { |
| 31 | + $attributes = $reflection->getAttributes(Route::class); |
| 32 | + $route = $attributes[0]->newInstance(); |
| 33 | + } else { |
| 34 | + $reader = new AnnotationReader(); |
| 35 | + $route = $reader->getMethodAnnotation($reflection, Route::class); |
| 36 | + } |
| 37 | + |
| 38 | + if (!$route instanceof Route) { |
| 39 | + throw new \Exception('Can\'t parse annotation'); |
| 40 | + } |
| 41 | + |
| 42 | + return $route; |
| 43 | + } |
| 44 | + |
| 45 | + public function provideDeprecationArrayAsFirstArgument() |
| 46 | + { |
| 47 | + return [ |
| 48 | + ['requirements', ['locale' => 'en'], 'getRequirements'], |
| 49 | + ['options', ['compiler_class' => 'RouteCompiler'], 'getOptions'], |
| 50 | + ['name', 'blog_index', 'getName'], |
| 51 | + ['defaults', ['_controller' => 'MyBlogBundle:Blog:index'], 'getDefaults'], |
| 52 | + ['schemes', ['https'], 'getSchemes'], |
| 53 | + ['methods', ['GET', 'POST'], 'getMethods'], |
| 54 | + ['host', '{locale}.example.com', 'getHost'], |
| 55 | + ['condition', 'context.getMethod() == "GET"', 'getCondition'], |
| 56 | + ['value', '/Blog', 'getPath'], |
| 57 | + ['value', ['nl' => '/hier', 'en' => '/here'], 'getLocalizedPaths'], |
| 58 | + ]; |
29 | 59 | } |
30 | 60 |
|
31 | 61 | /** |
32 | 62 | * @group legacy |
| 63 | + * @dataProvider provideDeprecationArrayAsFirstArgument |
33 | 64 | */ |
34 | | - public function testTryingToSetLocalesDirectly() |
| 65 | + public function testDeprecationArrayAsFirstArgument(string $parameter, $value, string $getter) |
35 | 66 | { |
36 | | - $this->expectException(\BadMethodCallException::class); |
37 | | - new Route(['locales' => ['nl' => 'bar']]); |
| 67 | + $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.'); |
| 68 | + |
| 69 | + $route = new Route([$parameter => $value]); |
| 70 | + $this->assertEquals($route->$getter(), $value); |
38 | 71 | } |
39 | 72 |
|
40 | 73 | /** |
41 | 74 | * @requires PHP 8 |
42 | 75 | * @dataProvider getValidParameters |
43 | 76 | */ |
44 | | - public function testRouteParameters(string $parameter, $value, string $getter) |
| 77 | + public function testRouteParameters(string $methodName, string $getter, $expectedReturn) |
45 | 78 | { |
46 | | - $route = new Route(...[$parameter => $value]); |
47 | | - $this->assertEquals($route->$getter(), $value); |
| 79 | + $route = $this->getMethodAnnotation($methodName, true); |
| 80 | + $this->assertEquals($route->$getter(), $expectedReturn); |
48 | 81 | } |
49 | 82 |
|
50 | 83 | /** |
51 | 84 | * @group legacy |
52 | | - * @dataProvider getLegacyValidParameters |
| 85 | + * @dataProvider getValidParameters |
53 | 86 | */ |
54 | | - public function testLegacyRouteParameters(string $parameter, $value, string $getter) |
| 87 | + public function testLegacyRouteParameters(string $methodName, string $getter, $expectedReturn) |
55 | 88 | { |
56 | | - $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.'); |
57 | | - |
58 | | - $route = new Route([$parameter => $value]); |
59 | | - $this->assertEquals($route->$getter(), $value); |
| 89 | + $route = $this->getMethodAnnotation($methodName, false); |
| 90 | + $this->assertEquals($route->$getter(), $expectedReturn); |
60 | 91 | } |
61 | 92 |
|
62 | 93 | public function getValidParameters(): iterable |
63 | 94 | { |
64 | 95 | return [ |
65 | | - ['requirements', ['locale' => 'en'], 'getRequirements'], |
66 | | - ['options', ['compiler_class' => 'RouteCompiler'], 'getOptions'], |
67 | | - ['name', 'blog_index', 'getName'], |
68 | | - ['defaults', ['_controller' => 'MyBlogBundle:Blog:index'], 'getDefaults'], |
69 | | - ['schemes', ['https'], 'getSchemes'], |
70 | | - ['methods', ['GET', 'POST'], 'getMethods'], |
71 | | - ['host', '{locale}.example.com', 'getHost'], |
72 | | - ['condition', 'context.getMethod() == "GET"', 'getCondition'], |
| 96 | + ['simplePath', 'getPath', '/Blog'], |
| 97 | + ['localized', 'getLocalizedPaths', ['nl' => '/hier', 'en' => '/here']], |
| 98 | + ['requirements', 'getRequirements', ['locale' => 'en']], |
| 99 | + ['options', 'getOptions', ['compiler_class' => 'RouteCompiler']], |
| 100 | + ['name', 'getName', 'blog_index'], |
| 101 | + ['defaults', 'getDefaults', ['_controller' => 'MyBlogBundle:Blog:index']], |
| 102 | + ['schemes', 'getSchemes', ['https']], |
| 103 | + ['methods', 'getMethods', ['GET', 'POST']], |
| 104 | + ['host', 'getHost', '{locale}.example.com'], |
| 105 | + ['condition', 'getCondition', 'context.getMethod() == \'GET\''], |
73 | 106 | ]; |
74 | 107 | } |
75 | | - |
76 | | - public function getLegacyValidParameters(): iterable |
77 | | - { |
78 | | - yield from $this->getValidParameters(); |
79 | | - |
80 | | - yield ['value', '/Blog', 'getPath']; |
81 | | - yield ['value', ['nl' => '/hier', 'en' => '/here'], 'getLocalizedPaths']; |
82 | | - } |
83 | 108 | } |
0 commit comments