PhpMatcherDumperTest.php 4.7 KB

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