Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnnotationClassLoaderTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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\GlobalDefaultsClass;
  21. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\InvokableController;
  22. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\InvokableLocalizedController;
  23. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedActionPathController;
  24. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedMethodActionControllers;
  25. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedPrefixLocalizedActionController;
  26. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedPrefixMissingLocaleActionController;
  27. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedPrefixMissingRouteLocaleActionController;
  28. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\LocalizedPrefixWithRouteWithoutLocale;
  29. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\MethodActionControllers;
  30. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\MissingRouteNameController;
  31. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\NothingButNameController;
  32. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\PrefixedActionLocalizedRouteController;
  33. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\PrefixedActionPathController;
  34. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController;
  35. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RouteWithPrefixController;
  36. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\Utf8ActionControllers;
  37. class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
  38. {
  39. /**
  40. * @var AnnotationClassLoader
  41. */
  42. private $loader;
  43. protected function setUp()
  44. {
  45. $reader = new AnnotationReader();
  46. $this->loader = new class($reader) extends AnnotationClassLoader {
  47. protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
  48. {
  49. }
  50. };
  51. AnnotationRegistry::registerLoader('class_exists');
  52. }
  53. /**
  54. * @dataProvider provideTestSupportsChecksResource
  55. */
  56. public function testSupportsChecksResource($resource, $expectedSupports)
  57. {
  58. $this->assertSame($expectedSupports, $this->loader->supports($resource), '->supports() returns true if the resource is loadable');
  59. }
  60. public function provideTestSupportsChecksResource()
  61. {
  62. return [
  63. ['class', true],
  64. ['\fully\qualified\class\name', true],
  65. ['namespaced\class\without\leading\slash', true],
  66. ['ÿClassWithLegalSpecialCharacters', true],
  67. ['5', false],
  68. ['foo.foo', false],
  69. [null, false],
  70. ];
  71. }
  72. public function testSupportsChecksTypeIfSpecified()
  73. {
  74. $this->assertTrue($this->loader->supports('class', 'annotation'), '->supports() checks the resource type if specified');
  75. $this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
  76. }
  77. public function testSimplePathRoute()
  78. {
  79. $routes = $this->loader->load(ActionPathController::class);
  80. $this->assertCount(1, $routes);
  81. $this->assertEquals('/path', $routes->get('action')->getPath());
  82. }
  83. /**
  84. * @group legacy
  85. * @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"?
  86. * @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"?
  87. * @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()"?
  88. * @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()"?
  89. */
  90. public function testRequirementsWithoutPlaceholderName()
  91. {
  92. $this->loader->load(RequirementsWithoutPlaceholderNameController::class);
  93. }
  94. public function testInvokableControllerLoader()
  95. {
  96. $routes = $this->loader->load(InvokableController::class);
  97. $this->assertCount(1, $routes);
  98. $this->assertEquals('/here', $routes->get('lol')->getPath());
  99. $this->assertEquals(['GET', 'POST'], $routes->get('lol')->getMethods());
  100. $this->assertEquals(['https'], $routes->get('lol')->getSchemes());
  101. }
  102. public function testInvokableLocalizedControllerLoading()
  103. {
  104. $routes = $this->loader->load(InvokableLocalizedController::class);
  105. $this->assertCount(2, $routes);
  106. $this->assertEquals('/here', $routes->get('action.en')->getPath());
  107. $this->assertEquals('/hier', $routes->get('action.nl')->getPath());
  108. }
  109. public function testLocalizedPathRoutes()
  110. {
  111. $routes = $this->loader->load(LocalizedActionPathController::class);
  112. $this->assertCount(2, $routes);
  113. $this->assertEquals('/path', $routes->get('action.en')->getPath());
  114. $this->assertEquals('/pad', $routes->get('action.nl')->getPath());
  115. }
  116. public function testLocalizedPathRoutesWithExplicitPathPropety()
  117. {
  118. $routes = $this->loader->load(ExplicitLocalizedActionPathController::class);
  119. $this->assertCount(2, $routes);
  120. $this->assertEquals('/path', $routes->get('action.en')->getPath());
  121. $this->assertEquals('/pad', $routes->get('action.nl')->getPath());
  122. }
  123. public function testDefaultValuesForMethods()
  124. {
  125. $routes = $this->loader->load(DefaultValueController::class);
  126. $this->assertCount(3, $routes);
  127. $this->assertEquals('/{default}/path', $routes->get('action')->getPath());
  128. $this->assertEquals('value', $routes->get('action')->getDefault('default'));
  129. $this->assertEquals('Symfony', $routes->get('hello_with_default')->getDefault('name'));
  130. $this->assertEquals('World', $routes->get('hello_without_default')->getDefault('name'));
  131. }
  132. public function testMethodActionControllers()
  133. {
  134. $routes = $this->loader->load(MethodActionControllers::class);
  135. $this->assertCount(2, $routes);
  136. $this->assertEquals('/the/path', $routes->get('put')->getPath());
  137. $this->assertEquals('/the/path', $routes->get('post')->getPath());
  138. }
  139. public function testInvokableClassRouteLoadWithMethodAnnotation()
  140. {
  141. $routes = $this->loader->load(LocalizedMethodActionControllers::class);
  142. $this->assertCount(4, $routes);
  143. $this->assertEquals('/the/path', $routes->get('put.en')->getPath());
  144. $this->assertEquals('/the/path', $routes->get('post.en')->getPath());
  145. }
  146. public function testGlobalDefaultsRoutesLoadWithAnnotation()
  147. {
  148. $routes = $this->loader->load(GlobalDefaultsClass::class);
  149. $this->assertCount(2, $routes);
  150. $specificLocaleRoute = $routes->get('specific_locale');
  151. $this->assertSame('/defaults/specific-locale', $specificLocaleRoute->getPath());
  152. $this->assertSame('s_locale', $specificLocaleRoute->getDefault('_locale'));
  153. $this->assertSame('g_format', $specificLocaleRoute->getDefault('_format'));
  154. $specificFormatRoute = $routes->get('specific_format');
  155. $this->assertSame('/defaults/specific-format', $specificFormatRoute->getPath());
  156. $this->assertSame('g_locale', $specificFormatRoute->getDefault('_locale'));
  157. $this->assertSame('s_format', $specificFormatRoute->getDefault('_format'));
  158. }
  159. public function testUtf8RoutesLoadWithAnnotation()
  160. {
  161. $routes = $this->loader->load(Utf8ActionControllers::class);
  162. $this->assertCount(2, $routes);
  163. $this->assertTrue($routes->get('one')->getOption('utf8'), 'The route must accept utf8');
  164. $this->assertFalse($routes->get('two')->getOption('utf8'), 'The route must not accept utf8');
  165. }
  166. public function testRouteWithPathWithPrefix()
  167. {
  168. $routes = $this->loader->load(PrefixedActionPathController::class);
  169. $this->assertCount(1, $routes);
  170. $route = $routes->get('action');
  171. $this->assertEquals('/prefix/path', $route->getPath());
  172. $this->assertEquals('lol=fun', $route->getCondition());
  173. $this->assertEquals('frankdejonge.nl', $route->getHost());
  174. }
  175. public function testLocalizedRouteWithPathWithPrefix()
  176. {
  177. $routes = $this->loader->load(PrefixedActionLocalizedRouteController::class);
  178. $this->assertCount(2, $routes);
  179. $this->assertEquals('/prefix/path', $routes->get('action.en')->getPath());
  180. $this->assertEquals('/prefix/pad', $routes->get('action.nl')->getPath());
  181. }
  182. public function testLocalizedPrefixLocalizedRoute()
  183. {
  184. $routes = $this->loader->load(LocalizedPrefixLocalizedActionController::class);
  185. $this->assertCount(2, $routes);
  186. $this->assertEquals('/nl/actie', $routes->get('action.nl')->getPath());
  187. $this->assertEquals('/en/action', $routes->get('action.en')->getPath());
  188. }
  189. public function testInvokableClassMultipleRouteLoad()
  190. {
  191. $classRouteData1 = [
  192. 'name' => 'route1',
  193. 'path' => '/1',
  194. 'schemes' => ['https'],
  195. 'methods' => ['GET'],
  196. ];
  197. $classRouteData2 = [
  198. 'name' => 'route2',
  199. 'path' => '/2',
  200. 'schemes' => ['https'],
  201. 'methods' => ['GET'],
  202. ];
  203. $reader = $this->getReader();
  204. $reader
  205. ->expects($this->exactly(1))
  206. ->method('getClassAnnotations')
  207. ->willReturn([new RouteAnnotation($classRouteData1), new RouteAnnotation($classRouteData2)])
  208. ;
  209. $reader
  210. ->expects($this->once())
  211. ->method('getMethodAnnotations')
  212. ->willReturn([])
  213. ;
  214. $loader = new class($reader) extends AnnotationClassLoader {
  215. protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
  216. {
  217. }
  218. };
  219. $routeCollection = $loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass');
  220. $route = $routeCollection->get($classRouteData1['name']);
  221. $this->assertSame($classRouteData1['path'], $route->getPath(), '->load preserves class route path');
  222. $this->assertEquals($classRouteData1['schemes'], $route->getSchemes(), '->load preserves class route schemes');
  223. $this->assertEquals($classRouteData1['methods'], $route->getMethods(), '->load preserves class route methods');
  224. $route = $routeCollection->get($classRouteData2['name']);
  225. $this->assertSame($classRouteData2['path'], $route->getPath(), '->load preserves class route path');
  226. $this->assertEquals($classRouteData2['schemes'], $route->getSchemes(), '->load preserves class route schemes');
  227. $this->assertEquals($classRouteData2['methods'], $route->getMethods(), '->load preserves class route methods');
  228. }
  229. public function testMissingPrefixLocale()
  230. {
  231. $this->expectException(\LogicException::class);
  232. $this->loader->load(LocalizedPrefixMissingLocaleActionController::class);
  233. }
  234. public function testMissingRouteLocale()
  235. {
  236. $this->expectException(\LogicException::class);
  237. $this->loader->load(LocalizedPrefixMissingRouteLocaleActionController::class);
  238. }
  239. public function testRouteWithoutName()
  240. {
  241. $routes = $this->loader->load(MissingRouteNameController::class)->all();
  242. $this->assertCount(1, $routes);
  243. $this->assertEquals('/path', reset($routes)->getPath());
  244. }
  245. public function testNothingButName()
  246. {
  247. $routes = $this->loader->load(NothingButNameController::class)->all();
  248. $this->assertCount(1, $routes);
  249. $this->assertEquals('/', reset($routes)->getPath());
  250. }
  251. public function testNonExistingClass()
  252. {
  253. $this->expectException(\LogicException::class);
  254. $this->loader->load('ClassThatDoesNotExist');
  255. }
  256. public function testLoadingAbstractClass()
  257. {
  258. $this->expectException(\LogicException::class);
  259. $this->loader->load(AbstractClassController::class);
  260. }
  261. public function testLocalizedPrefixWithoutRouteLocale()
  262. {
  263. $routes = $this->loader->load(LocalizedPrefixWithRouteWithoutLocale::class);
  264. $this->assertCount(2, $routes);
  265. $this->assertEquals('/en/suffix', $routes->get('action.en')->getPath());
  266. $this->assertEquals('/nl/suffix', $routes->get('action.nl')->getPath());
  267. }
  268. /**
  269. * @requires function mb_strtolower
  270. */
  271. public function testDefaultRouteName()
  272. {
  273. $methodRouteData = [
  274. 'name' => null,
  275. ];
  276. $reader = $this->getReader();
  277. $reader
  278. ->expects($this->once())
  279. ->method('getMethodAnnotations')
  280. ->willReturn([new RouteAnnotation($methodRouteData)])
  281. ;
  282. $loader = new class($reader) extends AnnotationClassLoader {
  283. protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
  284. {
  285. }
  286. };
  287. $routeCollection = $loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\EncodingClass');
  288. $defaultName = array_keys($routeCollection->all())[0];
  289. $this->assertSame($defaultName, 'symfony_component_routing_tests_fixtures_annotatedclasses_encodingclass_routeàction');
  290. }
  291. public function testLoadingRouteWithPrefix()
  292. {
  293. $routes = $this->loader->load(RouteWithPrefixController::class);
  294. $this->assertCount(1, $routes);
  295. $this->assertEquals('/prefix/path', $routes->get('action')->getPath());
  296. }
  297. }