PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/routing/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php

https://gitlab.com/techniconline/kmc
PHP | 264 lines | 182 code | 43 blank | 39 comment | 0 complexity | ae7743769559ca8d2dcb385069af07d8 MD5 | raw file
  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\Matcher\Dumper;
  11. use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Routing\RouteCollection;
  14. class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @expectedException \LogicException
  18. */
  19. public function testDumpWhenSchemeIsUsedWithoutAProperDumper()
  20. {
  21. $collection = new RouteCollection();
  22. $collection->add('secure', new Route(
  23. '/secure',
  24. array(),
  25. array('_scheme' => 'https')
  26. ));
  27. $dumper = new PhpMatcherDumper($collection);
  28. $dumper->dump();
  29. }
  30. /**
  31. * @dataProvider getRouteCollections
  32. */
  33. public function testDump(RouteCollection $collection, $fixture, $options = array())
  34. {
  35. $basePath = __DIR__ . '/../../Fixtures/dumper/';
  36. $dumper = new PhpMatcherDumper($collection);
  37. $this->assertStringEqualsFile($basePath . $fixture, $dumper->dump($options), '->dump() correctly dumps routes as optimized PHP code.');
  38. }
  39. public function getRouteCollections()
  40. {
  41. /* test case 1 */
  42. $collection = new RouteCollection();
  43. $collection->add('overridden', new Route('/overridden'));
  44. // defaults and requirements
  45. $collection->add('foo', new Route(
  46. '/foo/{bar}',
  47. array('def' => 'test'),
  48. array('bar' => 'baz|symfony')
  49. ));
  50. // method requirement
  51. $collection->add('bar', new Route(
  52. '/bar/{foo}',
  53. array(),
  54. array('_method' => 'GET|head')
  55. ));
  56. // GET method requirement automatically adds HEAD as valid
  57. $collection->add('barhead', new Route(
  58. '/barhead/{foo}',
  59. array(),
  60. array('_method' => 'GET')
  61. ));
  62. // simple
  63. $collection->add('baz', new Route(
  64. '/test/baz'
  65. ));
  66. // simple with extension
  67. $collection->add('baz2', new Route(
  68. '/test/baz.html'
  69. ));
  70. // trailing slash
  71. $collection->add('baz3', new Route(
  72. '/test/baz3/'
  73. ));
  74. // trailing slash with variable
  75. $collection->add('baz4', new Route(
  76. '/test/{foo}/'
  77. ));
  78. // trailing slash and method
  79. $collection->add('baz5', new Route(
  80. '/test/{foo}/',
  81. array(),
  82. array('_method' => 'post')
  83. ));
  84. // complex name
  85. $collection->add('baz.baz6', new Route(
  86. '/test/{foo}/',
  87. array(),
  88. array('_method' => 'put')
  89. ));
  90. // defaults without variable
  91. $collection->add('foofoo', new Route(
  92. '/foofoo',
  93. array('def' => 'test')
  94. ));
  95. // pattern with quotes
  96. $collection->add('quoter', new Route(
  97. '/{quoter}',
  98. array(),
  99. array('quoter' => '[\']+')
  100. ));
  101. // space in pattern
  102. $collection->add('space', new Route(
  103. '/spa ce'
  104. ));
  105. // prefixes
  106. $collection1 = new RouteCollection();
  107. $collection1->add('overridden', new Route('/overridden1'));
  108. $collection1->add('foo1', new Route('/{foo}'));
  109. $collection1->add('bar1', new Route('/{bar}'));
  110. $collection1->addPrefix('/b\'b');
  111. $collection2 = new RouteCollection();
  112. $collection2->addCollection($collection1);
  113. $collection2->add('overridden', new Route('/{var}', array(), array('var' => '.*')));
  114. $collection1 = new RouteCollection();
  115. $collection1->add('foo2', new Route('/{foo1}'));
  116. $collection1->add('bar2', new Route('/{bar1}'));
  117. $collection1->addPrefix('/b\'b');
  118. $collection2->addCollection($collection1);
  119. $collection2->addPrefix('/a');
  120. $collection->addCollection($collection2);
  121. // overridden through addCollection() and multiple sub-collections with no own prefix
  122. $collection1 = new RouteCollection();
  123. $collection1->add('overridden2', new Route('/old'));
  124. $collection1->add('helloWorld', new Route('/hello/{who}', array('who' => 'World!')));
  125. $collection2 = new RouteCollection();
  126. $collection3 = new RouteCollection();
  127. $collection3->add('overridden2', new Route('/new'));
  128. $collection3->add('hey', new Route('/hey/'));
  129. $collection2->addCollection($collection3);
  130. $collection1->addCollection($collection2);
  131. $collection1->addPrefix('/multi');
  132. $collection->addCollection($collection1);
  133. // "dynamic" prefix
  134. $collection1 = new RouteCollection();
  135. $collection1->add('foo3', new Route('/{foo}'));
  136. $collection1->add('bar3', new Route('/{bar}'));
  137. $collection1->addPrefix('/b');
  138. $collection1->addPrefix('{_locale}');
  139. $collection->addCollection($collection1);
  140. // route between collections
  141. $collection->add('ababa', new Route('/ababa'));
  142. // collection with static prefix but only one route
  143. $collection1 = new RouteCollection();
  144. $collection1->add('foo4', new Route('/{foo}'));
  145. $collection1->addPrefix('/aba');
  146. $collection->addCollection($collection1);
  147. // prefix and host
  148. $collection1 = new RouteCollection();
  149. $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
  150. $collection1->add('route1', $route1);
  151. $collection2 = new RouteCollection();
  152. $route2 = new Route('/c2/route2', array(), array(), array(), 'a.example.com');
  153. $collection1->add('route2', $route2);
  154. $route3 = new Route('/c2/route3', array(), array(), array(), 'b.example.com');
  155. $collection1->add('route3', $route3);
  156. $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
  157. $collection1->add('route4', $route4);
  158. $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
  159. $collection1->add('route5', $route5);
  160. $route6 = new Route('/route6', array(), array(), array(), null);
  161. $collection1->add('route6', $route6);
  162. $collection->addCollection($collection1);
  163. // host and variables
  164. $collection1 = new RouteCollection();
  165. $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
  166. $collection1->add('route11', $route11);
  167. $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
  168. $collection1->add('route12', $route12);
  169. $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
  170. $collection1->add('route13', $route13);
  171. $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
  172. $collection1->add('route14', $route14);
  173. $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
  174. $collection1->add('route15', $route15);
  175. $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
  176. $collection1->add('route16', $route16);
  177. $route17 = new Route('/route17', array(), array(), array(), null);
  178. $collection1->add('route17', $route17);
  179. $collection->addCollection($collection1);
  180. // multiple sub-collections with a single route and a prefix each
  181. $collection1 = new RouteCollection();
  182. $collection1->add('a', new Route('/a...'));
  183. $collection2 = new RouteCollection();
  184. $collection2->add('b', new Route('/{var}'));
  185. $collection3 = new RouteCollection();
  186. $collection3->add('c', new Route('/{var}'));
  187. $collection3->addPrefix('/c');
  188. $collection2->addCollection($collection3);
  189. $collection2->addPrefix('/b');
  190. $collection1->addCollection($collection2);
  191. $collection1->addPrefix('/a');
  192. $collection->addCollection($collection1);
  193. /* test case 2 */
  194. $redirectCollection = clone $collection;
  195. // force HTTPS redirection
  196. $redirectCollection->add('secure', new Route(
  197. '/secure',
  198. array(),
  199. array('_scheme' => 'https')
  200. ));
  201. // force HTTP redirection
  202. $redirectCollection->add('nonsecure', new Route(
  203. '/nonsecure',
  204. array(),
  205. array('_scheme' => 'http')
  206. ));
  207. /* test case 3 */
  208. $rootprefixCollection = new RouteCollection();
  209. $rootprefixCollection->add('static', new Route('/test'));
  210. $rootprefixCollection->add('dynamic', new Route('/{var}'));
  211. $rootprefixCollection->addPrefix('rootprefix');
  212. $route = new Route('/with-condition');
  213. $route->setCondition('context.getMethod() == "GET"');
  214. $rootprefixCollection->add('with-condition', $route);
  215. return array(
  216. array($collection, 'url_matcher1.php', array()),
  217. array($redirectCollection, 'url_matcher2.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')),
  218. array($rootprefixCollection, 'url_matcher3.php', array()),
  219. );
  220. }
  221. }