AnnotationClassLoaderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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\Loader;
  11. use Doctrine\Common\Annotations\AnnotationReader;
  12. use Doctrine\Common\Annotations\AnnotationRegistry;
  13. use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
  14. use Symfony\Component\Routing\Loader\AnnotationClassLoader;
  15. use Symfony\Component\Routing\Route;
  16. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\AbstractClassController;
  17. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\ActionPathController;
  18. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\DefaultValueController;
  19. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\ExplicitLocalizedActionPathController;
  20. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\InvokableController;
  21. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\InvokableLocalizedController;
  22. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedActionPathController;
  23. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedMethodActionControllers;
  24. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedPrefixLocalizedActionController;
  25. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedPrefixMissingLocaleActionController;
  26. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedPrefixMissingRouteLocaleActionController;
  27. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedPrefixWithRouteWithoutLocale;
  28. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\MethodActionControllers;
  29. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\MissingRouteNameController;
  30. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\NothingButNameController;
  31. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\PrefixedActionLocalizedRouteController;
  32. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\PrefixedActionPathController;
  33. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController;
  34. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RouteWithPrefixController;
  35. class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
  36. {
  37. /**
  38. * @var AnnotationClassLoader
  39. */
  40. private $loader;
  41. protected function setUp()
  42. {
  43. $reader = new AnnotationReader();
  44. $this->loader = new class($reader) extends AnnotationClassLoader {
  45. protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
  46. {
  47. }
  48. };
  49. AnnotationRegistry::registerLoader('class_exists');
  50. }
  51. /**
  52. * @dataProvider provideTestSupportsChecksResource
  53. */
  54. public function testSupportsChecksResource($resource, $expectedSupports)
  55. {
  56. $this->assertSame($expectedSupports, $this->loader->supports($resource), '->supports() returns true if the resource is loadable');
  57. }
  58. public function provideTestSupportsChecksResource()
  59. {
  60. return [
  61. ['class', true],
  62. ['\fully\qualified\class\name', true],
  63. ['namespaced\class\without\leading\slash', true],
  64. ['ÿClassWithLegalSpecialCharacters', true],
  65. ['5', false],
  66. ['foo.foo', false],
  67. [null, false],
  68. ];
  69. }
  70. public function testSupportsChecksTypeIfSpecified()
  71. {
  72. $this->assertTrue($this->loader->supports('class', 'annotation'), '->supports() checks the resource type if specified');
  73. $this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
  74. }
  75. public function testSimplePathRoute()
  76. {
  77. $routes = $this->loader->load(ActionPathController::class);
  78. $this->assertCount(1, $routes);
  79. $this->assertEquals('/path', $routes->get('action')->getPath());
  80. }
  81. /**
  82. * @group legacy
  83. * @expectedDeprecation A placeholder name must be a string (0 given). Did you forget to specify the placeholder key for the requirement "foo" in "Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController"?
  84. * @expectedDeprecation A placeholder name must be a string (1 given). Did you forget to specify the placeholder key for the requirement "\d+" in "Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController"?
  85. * @expectedDeprecation A placeholder name must be a string (0 given). Did you forget to specify the placeholder key for the requirement "foo" of route "foo" in "Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController::foo()"?
  86. * @expectedDeprecation A placeholder name must be a string (1 given). Did you forget to specify the placeholder key for the requirement "\d+" of route "foo" in "Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController::foo()"?
  87. */
  88. public function testRequirementsWithoutPlaceholderName()
  89. {
  90. $this->loader->load(RequirementsWithoutPlaceholderNameController::class);
  91. }
  92. public function testInvokableControllerLoader()
  93. {
  94. $routes = $this->loader->load(InvokableController::class);
  95. $this->assertCount(1, $routes);
  96. $this->assertEquals('/here', $routes->get('lol')->getPath());
  97. $this->assertEquals(['GET', 'POST'], $routes->get('lol')->getMethods());
  98. $this->assertEquals(['https'], $routes->get('lol')->getSchemes());
  99. }
  100. public function testInvokableLocalizedControllerLoading()
  101. {
  102. $routes = $this->loader->load(InvokableLocalizedController::class);
  103. $this->assertCount(2, $routes);
  104. $this->assertEquals('/here', $routes->get('action.en')->getPath());
  105. $this->assertEquals('/hier', $routes->get('action.nl')->getPath());
  106. }
  107. public function testLocalizedPathRoutes()
  108. {
  109. $routes = $this->loader->load(LocalizedActionPathController::class);
  110. $this->assertCount(2, $routes);
  111. $this->assertEquals('/path', $routes->get('action.en')->getPath());
  112. $this->assertEquals('/pad', $routes->get('action.nl')->getPath());
  113. }
  114. public function testLocalizedPathRoutesWithExplicitPathPropety()
  115. {
  116. $routes = $this->loader->load(ExplicitLocalizedActionPathController::class);
  117. $this->assertCount(2, $routes);
  118. $this->assertEquals('/path', $routes->get('action.en')->getPath());
  119. $this->assertEquals('/pad', $routes->get('action.nl')->getPath());
  120. }
  121. public function testDefaultValuesForMethods()
  122. {
  123. $routes = $this->loader->load(DefaultValueController::class);
  124. $this->assertCount(1, $routes);
  125. $this->assertEquals('/{default}/path', $routes->get('action')->getPath());
  126. $this->assertEquals('value', $routes->get('action')->getDefault('default'));
  127. }
  128. public function testMethodActionControllers()
  129. {
  130. $routes = $this->loader->load(MethodActionControllers::class);
  131. $this->assertCount(2, $routes);
  132. $this->assertEquals('/the/path', $routes->get('put')->getPath());
  133. $this->assertEquals('/the/path', $routes->get('post')->getPath());
  134. }
  135. public function testInvokableClassRouteLoadWithMethodAnnotation()
  136. {
  137. $routes = $this->loader->load(LocalizedMethodActionControllers::class);
  138. $this->assertCount(4, $routes);
  139. $this->assertEquals('/the/path', $routes->get('put.en')->getPath());
  140. $this->assertEquals('/the/path', $routes->get('post.en')->getPath());
  141. }
  142. public function testRouteWithPathWithPrefix()
  143. {
  144. $routes = $this->loader->load(PrefixedActionPathController::class);
  145. $this->assertCount(1, $routes);
  146. $route = $routes->get('action');
  147. $this->assertEquals('/prefix/path', $route->getPath());
  148. $this->assertEquals('lol=fun', $route->getCondition());
  149. $this->assertEquals('frankdejonge.nl', $route->getHost());
  150. }
  151. public function testLocalizedRouteWithPathWithPrefix()
  152. {
  153. $routes = $this->loader->load(PrefixedActionLocalizedRouteController::class);
  154. $this->assertCount(2, $routes);
  155. $this->assertEquals('/prefix/path', $routes->get('action.en')->getPath());
  156. $this->assertEquals('/prefix/pad', $routes->get('action.nl')->getPath());
  157. }
  158. public function testLocalizedPrefixLocalizedRoute()
  159. {
  160. $routes = $this->loader->load(LocalizedPrefixLocalizedActionController::class);
  161. $this->assertCount(2, $routes);
  162. $this->assertEquals('/nl/actie', $routes->get('action.nl')->getPath());
  163. $this->assertEquals('/en/action', $routes->get('action.en')->getPath());
  164. }
  165. public function testInvokableClassMultipleRouteLoad()
  166. {
  167. $classRouteData1 = [
  168. 'name' => 'route1',
  169. 'path' => '/1',
  170. 'schemes' => ['https'],
  171. 'methods' => ['GET'],
  172. ];
  173. $classRouteData2 = [
  174. 'name' => 'route2',
  175. 'path' => '/2',
  176. 'schemes' => ['https'],
  177. 'methods' => ['GET'],
  178. ];
  179. $reader = $this->getReader();
  180. $reader
  181. ->expects($this->exactly(1))
  182. ->method('getClassAnnotations')
  183. ->will($this->returnValue([new RouteAnnotation($classRouteData1), new RouteAnnotation($classRouteData2)]))
  184. ;
  185. $reader
  186. ->expects($this->once())
  187. ->method('getMethodAnnotations')
  188. ->will($this->returnValue([]))
  189. ;
  190. $loader = new class($reader) extends AnnotationClassLoader {
  191. protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
  192. {
  193. }
  194. };
  195. $routeCollection = $loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass');
  196. $route = $routeCollection->get($classRouteData1['name']);
  197. $this->assertSame($classRouteData1['path'], $route->getPath(), '->load preserves class route path');
  198. $this->assertEquals($classRouteData1['schemes'], $route->getSchemes(), '->load preserves class route schemes');
  199. $this->assertEquals($classRouteData1['methods'], $route->getMethods(), '->load preserves class route methods');
  200. $route = $routeCollection->get($classRouteData2['name']);
  201. $this->assertSame($classRouteData2['path'], $route->getPath(), '->load preserves class route path');
  202. $this->assertEquals($classRouteData2['schemes'], $route->getSchemes(), '->load preserves class route schemes');
  203. $this->assertEquals($classRouteData2['methods'], $route->getMethods(), '->load preserves class route methods');
  204. }
  205. public function testMissingPrefixLocale()
  206. {
  207. $this->expectException(\LogicException::class);
  208. $this->loader->load(LocalizedPrefixMissingLocaleActionController::class);
  209. }
  210. public function testMissingRouteLocale()
  211. {
  212. $this->expectException(\LogicException::class);
  213. $this->loader->load(LocalizedPrefixMissingRouteLocaleActionController::class);
  214. }
  215. public function testRouteWithoutName()
  216. {
  217. $routes = $this->loader->load(MissingRouteNameController::class)->all();
  218. $this->assertCount(1, $routes);
  219. $this->assertEquals('/path', reset($routes)->getPath());
  220. }
  221. public function testNothingButName()
  222. {
  223. $routes = $this->loader->load(NothingButNameController::class)->all();
  224. $this->assertCount(1, $routes);
  225. $this->assertEquals('/', reset($routes)->getPath());
  226. }
  227. public function testNonExistingClass()
  228. {
  229. $this->expectException(\LogicException::class);
  230. $this->loader->load('ClassThatDoesNotExist');
  231. }
  232. public function testLoadingAbstractClass()
  233. {
  234. $this->expectException(\LogicException::class);
  235. $this->loader->load(AbstractClassController::class);
  236. }
  237. public function testLocalizedPrefixWithoutRouteLocale()
  238. {
  239. $routes = $this->loader->load(LocalizedPrefixWithRouteWithoutLocale::class);
  240. $this->assertCount(2, $routes);
  241. $this->assertEquals('/en/suffix', $routes->get('action.en')->getPath());
  242. $this->assertEquals('/nl/suffix', $routes->get('action.nl')->getPath());
  243. }
  244. public function testLoadingRouteWithPrefix()
  245. {
  246. $routes = $this->loader->load(RouteWithPrefixController::class);
  247. $this->assertCount(1, $routes);
  248. $this->assertEquals('/prefix/path', $routes->get('action')->getPath());
  249. }
  250. }