AnnotationClassLoaderTestCase.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Routing\Loader\AnnotationClassLoader;
  13. use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\AbstractClassController;
  14. abstract class AnnotationClassLoaderTestCase extends TestCase
  15. {
  16. /**
  17. * @var AnnotationClassLoader
  18. */
  19. protected $loader;
  20. /**
  21. * @dataProvider provideTestSupportsChecksResource
  22. */
  23. public function testSupportsChecksResource($resource, $expectedSupports)
  24. {
  25. $this->assertSame($expectedSupports, $this->loader->supports($resource), '->supports() returns true if the resource is loadable');
  26. }
  27. public static function provideTestSupportsChecksResource()
  28. {
  29. return [
  30. ['class', true],
  31. ['\fully\qualified\class\name', true],
  32. ['namespaced\class\without\leading\slash', true],
  33. ['ÿClassWithLegalSpecialCharacters', true],
  34. ['5', false],
  35. ['foo.foo', false],
  36. [null, false],
  37. ];
  38. }
  39. public function testSupportsChecksTypeIfSpecified()
  40. {
  41. $this->assertTrue($this->loader->supports('class', 'annotation'), '->supports() checks the resource type if specified');
  42. $this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
  43. }
  44. public function testSimplePathRoute()
  45. {
  46. $routes = $this->loader->load($this->getNamespace().'\ActionPathController');
  47. $this->assertCount(1, $routes);
  48. $this->assertEquals('/path', $routes->get('action')->getPath());
  49. }
  50. public function testRequirementsWithoutPlaceholderName()
  51. {
  52. $this->expectException(\InvalidArgumentException::class);
  53. $this->expectExceptionMessage('A placeholder name must be a string (0 given). Did you forget to specify the placeholder key for the requirement "foo"');
  54. $this->loader->load($this->getNamespace().'\RequirementsWithoutPlaceholderNameController');
  55. }
  56. public function testInvokableControllerLoader()
  57. {
  58. $routes = $this->loader->load($this->getNamespace().'\InvokableController');
  59. $this->assertCount(1, $routes);
  60. $this->assertEquals('/here', $routes->get('lol')->getPath());
  61. $this->assertEquals(['GET', 'POST'], $routes->get('lol')->getMethods());
  62. $this->assertEquals(['https'], $routes->get('lol')->getSchemes());
  63. }
  64. public function testInvokableLocalizedControllerLoading()
  65. {
  66. $routes = $this->loader->load($this->getNamespace().'\InvokableLocalizedController');
  67. $this->assertCount(2, $routes);
  68. $this->assertEquals('/here', $routes->get('action.en')->getPath());
  69. $this->assertEquals('/hier', $routes->get('action.nl')->getPath());
  70. }
  71. public function testLocalizedPathRoutes()
  72. {
  73. $routes = $this->loader->load($this->getNamespace().'\LocalizedActionPathController');
  74. $this->assertCount(2, $routes);
  75. $this->assertEquals('/path', $routes->get('action.en')->getPath());
  76. $this->assertEquals('/pad', $routes->get('action.nl')->getPath());
  77. $this->assertEquals('nl', $routes->get('action.nl')->getRequirement('_locale'));
  78. $this->assertEquals('en', $routes->get('action.en')->getRequirement('_locale'));
  79. }
  80. public function testLocalizedPathRoutesWithExplicitPathPropety()
  81. {
  82. $routes = $this->loader->load($this->getNamespace().'\ExplicitLocalizedActionPathController');
  83. $this->assertCount(2, $routes);
  84. $this->assertEquals('/path', $routes->get('action.en')->getPath());
  85. $this->assertEquals('/pad', $routes->get('action.nl')->getPath());
  86. }
  87. public function testDefaultValuesForMethods()
  88. {
  89. $routes = $this->loader->load($this->getNamespace().'\DefaultValueController');
  90. $this->assertCount(3, $routes);
  91. $this->assertEquals('/{default}/path', $routes->get('action')->getPath());
  92. $this->assertEquals('value', $routes->get('action')->getDefault('default'));
  93. $this->assertEquals('Symfony', $routes->get('hello_with_default')->getDefault('name'));
  94. $this->assertEquals('World', $routes->get('hello_without_default')->getDefault('name'));
  95. }
  96. public function testMethodActionControllers()
  97. {
  98. $routes = $this->loader->load($this->getNamespace().'\MethodActionControllers');
  99. $this->assertSame(['put', 'post'], array_keys($routes->all()));
  100. $this->assertEquals('/the/path', $routes->get('put')->getPath());
  101. $this->assertEquals('/the/path', $routes->get('post')->getPath());
  102. }
  103. public function testInvokableClassRouteLoadWithMethodAnnotation()
  104. {
  105. $routes = $this->loader->load($this->getNamespace().'\LocalizedMethodActionControllers');
  106. $this->assertCount(4, $routes);
  107. $this->assertEquals('/the/path', $routes->get('put.en')->getPath());
  108. $this->assertEquals('/the/path', $routes->get('post.en')->getPath());
  109. }
  110. public function testGlobalDefaultsRoutesLoadWithAnnotation()
  111. {
  112. $routes = $this->loader->load($this->getNamespace().'\GlobalDefaultsClass');
  113. $this->assertCount(2, $routes);
  114. $specificLocaleRoute = $routes->get('specific_locale');
  115. $this->assertSame('/defaults/specific-locale', $specificLocaleRoute->getPath());
  116. $this->assertSame('s_locale', $specificLocaleRoute->getDefault('_locale'));
  117. $this->assertSame('g_format', $specificLocaleRoute->getDefault('_format'));
  118. $specificFormatRoute = $routes->get('specific_format');
  119. $this->assertSame('/defaults/specific-format', $specificFormatRoute->getPath());
  120. $this->assertSame('g_locale', $specificFormatRoute->getDefault('_locale'));
  121. $this->assertSame('s_format', $specificFormatRoute->getDefault('_format'));
  122. }
  123. public function testUtf8RoutesLoadWithAnnotation()
  124. {
  125. $routes = $this->loader->load($this->getNamespace().'\Utf8ActionControllers');
  126. $this->assertSame(['one', 'two'], array_keys($routes->all()));
  127. $this->assertTrue($routes->get('one')->getOption('utf8'), 'The route must accept utf8');
  128. $this->assertFalse($routes->get('two')->getOption('utf8'), 'The route must not accept utf8');
  129. }
  130. public function testRouteWithPathWithPrefix()
  131. {
  132. $routes = $this->loader->load($this->getNamespace().'\PrefixedActionPathController');
  133. $this->assertCount(1, $routes);
  134. $route = $routes->get('action');
  135. $this->assertEquals('/prefix/path', $route->getPath());
  136. $this->assertEquals('lol=fun', $route->getCondition());
  137. $this->assertEquals('frankdejonge.nl', $route->getHost());
  138. }
  139. public function testLocalizedRouteWithPathWithPrefix()
  140. {
  141. $routes = $this->loader->load($this->getNamespace().'\PrefixedActionLocalizedRouteController');
  142. $this->assertCount(2, $routes);
  143. $this->assertEquals('/prefix/path', $routes->get('action.en')->getPath());
  144. $this->assertEquals('/prefix/pad', $routes->get('action.nl')->getPath());
  145. }
  146. public function testLocalizedPrefixLocalizedRoute()
  147. {
  148. $routes = $this->loader->load($this->getNamespace().'\LocalizedPrefixLocalizedActionController');
  149. $this->assertCount(2, $routes);
  150. $this->assertEquals('/nl/actie', $routes->get('action.nl')->getPath());
  151. $this->assertEquals('/en/action', $routes->get('action.en')->getPath());
  152. }
  153. public function testInvokableClassMultipleRouteLoad()
  154. {
  155. $routeCollection = $this->loader->load($this->getNamespace().'\BazClass');
  156. $route = $routeCollection->get('route1');
  157. $this->assertSame('/1', $route->getPath(), '->load preserves class route path');
  158. $this->assertSame(['https'], $route->getSchemes(), '->load preserves class route schemes');
  159. $this->assertSame(['GET'], $route->getMethods(), '->load preserves class route methods');
  160. $route = $routeCollection->get('route2');
  161. $this->assertSame('/2', $route->getPath(), '->load preserves class route path');
  162. $this->assertEquals(['https'], $route->getSchemes(), '->load preserves class route schemes');
  163. $this->assertEquals(['GET'], $route->getMethods(), '->load preserves class route methods');
  164. }
  165. public function testMissingPrefixLocale()
  166. {
  167. $this->expectException(\LogicException::class);
  168. $this->expectExceptionMessage(sprintf('Route to "action" with locale "en" is missing a corresponding prefix in class "%s\LocalizedPrefixMissingLocaleActionController".', $this->getNamespace()));
  169. $this->loader->load($this->getNamespace().'\LocalizedPrefixMissingLocaleActionController');
  170. }
  171. public function testMissingRouteLocale()
  172. {
  173. $this->expectException(\LogicException::class);
  174. $this->expectExceptionMessage(sprintf('Route to "%s\LocalizedPrefixMissingRouteLocaleActionController::action" is missing paths for locale(s) "en".', $this->getNamespace()));
  175. $this->loader->load($this->getNamespace().'\LocalizedPrefixMissingRouteLocaleActionController');
  176. }
  177. public function testRouteWithoutName()
  178. {
  179. $routes = $this->loader->load($this->getNamespace().'\MissingRouteNameController')->all();
  180. $this->assertCount(1, $routes);
  181. $this->assertEquals('/path', reset($routes)->getPath());
  182. }
  183. public function testNothingButName()
  184. {
  185. $routes = $this->loader->load($this->getNamespace().'\NothingButNameController')->all();
  186. $this->assertCount(1, $routes);
  187. $this->assertEquals('/', reset($routes)->getPath());
  188. }
  189. public function testNonExistingClass()
  190. {
  191. $this->expectException(\LogicException::class);
  192. $this->loader->load('ClassThatDoesNotExist');
  193. }
  194. public function testLoadingAbstractClass()
  195. {
  196. $this->expectException(\LogicException::class);
  197. $this->loader->load(AbstractClassController::class);
  198. }
  199. public function testLocalizedPrefixWithoutRouteLocale()
  200. {
  201. $routes = $this->loader->load($this->getNamespace().'\LocalizedPrefixWithRouteWithoutLocale');
  202. $this->assertCount(2, $routes);
  203. $this->assertEquals('/en/suffix', $routes->get('action.en')->getPath());
  204. $this->assertEquals('/nl/suffix', $routes->get('action.nl')->getPath());
  205. }
  206. public function testLoadingRouteWithPrefix()
  207. {
  208. $routes = $this->loader->load($this->getNamespace().'\RouteWithPrefixController');
  209. $this->assertCount(1, $routes);
  210. $this->assertEquals('/prefix/path', $routes->get('action')->getPath());
  211. }
  212. public function testWhenEnv()
  213. {
  214. $routes = $this->loader->load($this->getNamespace().'\RouteWithEnv');
  215. $this->assertCount(0, $routes);
  216. $this->setUp('some-env');
  217. $routes = $this->loader->load($this->getNamespace().'\RouteWithEnv');
  218. $this->assertCount(1, $routes);
  219. $this->assertSame('/path', $routes->get('action')->getPath());
  220. }
  221. public function testMethodsAndSchemes()
  222. {
  223. $routes = $this->loader->load($this->getNamespace().'\MethodsAndSchemes');
  224. $this->assertSame(['GET', 'POST'], $routes->get('array_many')->getMethods());
  225. $this->assertSame(['http', 'https'], $routes->get('array_many')->getSchemes());
  226. $this->assertSame(['GET'], $routes->get('array_one')->getMethods());
  227. $this->assertSame(['http'], $routes->get('array_one')->getSchemes());
  228. $this->assertSame(['POST'], $routes->get('string')->getMethods());
  229. $this->assertSame(['https'], $routes->get('string')->getSchemes());
  230. }
  231. abstract protected function getNamespace(): string;
  232. }