PhpMatcherDumperTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Tests\Component\Routing;
  11. use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Routing\RouteCollection;
  14. use Symfony\Component\Routing\RequestContext;
  15. class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testDump()
  18. {
  19. $dumper = new PhpMatcherDumper($this->getRouteCollection(), new RequestContext());
  20. $this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');
  21. $collection = $this->getRouteCollection();
  22. // force HTTPS redirection
  23. $collection->add('secure', new Route(
  24. '/secure',
  25. array(),
  26. array('_scheme' => 'https')
  27. ));
  28. // force HTTP redirection
  29. $collection->add('nonsecure', new Route(
  30. '/nonsecure',
  31. array(),
  32. array('_scheme' => 'http')
  33. ));
  34. $dumper = new PhpMatcherDumper($collection, new RequestContext());
  35. $this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher2.php', $dumper->dump(array('base_class' => 'Symfony\Tests\Component\Routing\Fixtures\RedirectableUrlMatcher')), '->dump() dumps basic routes to the correct PHP file.');
  36. }
  37. /**
  38. * @expectedException \LogicException
  39. */
  40. public function testDumpWhenSchemeIsUsedWithoutAProperDumper()
  41. {
  42. $collection = new RouteCollection();
  43. $collection->add('secure', new Route(
  44. '/secure',
  45. array(),
  46. array('_scheme' => 'https')
  47. ));
  48. $dumper = new PhpMatcherDumper($collection, new RequestContext());
  49. $dumper->dump();
  50. }
  51. protected function getRouteCollection()
  52. {
  53. $collection = new RouteCollection();
  54. $collection->add('overriden', new Route('/overriden'));
  55. // defaults and requirements
  56. $collection->add('foo', new Route(
  57. '/foo/{bar}',
  58. array('def' => 'test'),
  59. array('bar' => 'baz|symfony')
  60. ));
  61. // method requirement
  62. $collection->add('bar', new Route(
  63. '/bar/{foo}',
  64. array(),
  65. array('_method' => 'GET|head')
  66. ));
  67. // GET method requirement automatically adds HEAD as valid
  68. $collection->add('barhead', new Route(
  69. '/barhead/{foo}',
  70. array(),
  71. array('_method' => 'GET')
  72. ));
  73. // simple
  74. $collection->add('baz', new Route(
  75. '/test/baz'
  76. ));
  77. // simple with extension
  78. $collection->add('baz2', new Route(
  79. '/test/baz.html'
  80. ));
  81. // trailing slash
  82. $collection->add('baz3', new Route(
  83. '/test/baz3/'
  84. ));
  85. // trailing slash with variable
  86. $collection->add('baz4', new Route(
  87. '/test/{foo}/'
  88. ));
  89. // trailing slash and method
  90. $collection->add('baz5', new Route(
  91. '/test/{foo}/',
  92. array(),
  93. array('_method' => 'post')
  94. ));
  95. // complex name
  96. $collection->add('baz.baz6', new Route(
  97. '/test/{foo}/',
  98. array(),
  99. array('_method' => 'put')
  100. ));
  101. // defaults without variable
  102. $collection->add('foofoo', new Route(
  103. '/foofoo',
  104. array('def' => 'test')
  105. ));
  106. // pattern with quotes
  107. $collection->add('quoter', new Route(
  108. '/{quoter}',
  109. array(),
  110. array('quoter' => '[\']+')
  111. ));
  112. // prefixes
  113. $collection1 = new RouteCollection();
  114. $collection1->add('overriden', new Route('/overriden1'));
  115. $collection1->add('foo1', new Route('/{foo}'));
  116. $collection1->add('bar1', new Route('/{bar}'));
  117. $collection2 = new RouteCollection();
  118. $collection2->addCollection($collection1, '/b\'b');
  119. $collection2->add('overriden', new Route('/overriden2'));
  120. $collection1 = new RouteCollection();
  121. $collection1->add('foo2', new Route('/{foo1}'));
  122. $collection1->add('bar2', new Route('/{bar1}'));
  123. $collection2->addCollection($collection1, '/b\'b');
  124. $collection->addCollection($collection2, '/a');
  125. // "dynamic" prefix
  126. $collection1 = new RouteCollection();
  127. $collection1->add('foo3', new Route('/{foo}'));
  128. $collection1->add('bar3', new Route('/{bar}'));
  129. $collection2 = new RouteCollection();
  130. $collection2->addCollection($collection1, '/b');
  131. $collection->addCollection($collection2, '/{_locale}');
  132. $collection->add('ababa', new Route('/ababa'));
  133. // some more prefixes
  134. $collection1 = new RouteCollection();
  135. $collection1->add('foo4', new Route('/{foo}'));
  136. $collection->addCollection($collection1, '/aba');
  137. return $collection;
  138. }
  139. }