ApacheMatcherDumperTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Route;
  12. use Symfony\Component\Routing\RouteCollection;
  13. use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
  14. class ApacheMatcherDumperTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected static $fixturesPath;
  17. public static function setUpBeforeClass()
  18. {
  19. self::$fixturesPath = realpath(__DIR__.'/../../Fixtures/');
  20. }
  21. public function testDump()
  22. {
  23. $dumper = new ApacheMatcherDumper($this->getRouteCollection());
  24. $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.apache', $dumper->dump(), '->dump() dumps basic routes to the correct apache format.');
  25. }
  26. /**
  27. * @dataProvider provideEscapeFixtures
  28. */
  29. public function testEscapePattern($src, $dest, $char, $with, $message)
  30. {
  31. $r = new \ReflectionMethod(new ApacheMatcherDumper($this->getRouteCollection()), 'escape');
  32. $r->setAccessible(true);
  33. $this->assertEquals($dest, $r->invoke(null, $src, $char, $with), $message);
  34. }
  35. public function provideEscapeFixtures()
  36. {
  37. return array(
  38. array('foo', 'foo', ' ', '-', 'Preserve string that should not be escaped'),
  39. array('fo-o', 'fo-o', ' ', '-', 'Preserve string that should not be escaped'),
  40. array('fo o', 'fo- o', ' ', '-', 'Escape special characters'),
  41. array('fo-- o', 'fo--- o', ' ', '-', 'Escape special characters'),
  42. array('fo- o', 'fo- o', ' ', '-', 'Do not escape already escaped string'),
  43. );
  44. }
  45. public function testEscapeScriptName()
  46. {
  47. $collection = new RouteCollection();
  48. $collection->add('foo', new Route('/foo'));
  49. $dumper = new ApacheMatcherDumper($collection);
  50. $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher2.apache', $dumper->dump(array('script_name' => 'ap p_d\ ev.php')));
  51. }
  52. private function getRouteCollection()
  53. {
  54. $collection = new RouteCollection();
  55. // defaults and requirements
  56. $collection->add('foo', new Route(
  57. '/foo/{bar}',
  58. array('def' => 'test'),
  59. array('bar' => 'baz|symfony')
  60. ));
  61. // defaults parameters in pattern
  62. $collection->add('foobar', new Route(
  63. '/foo/{bar}',
  64. array('bar' => 'toto')
  65. ));
  66. // method requirement
  67. $collection->add('bar', new Route(
  68. '/bar/{foo}',
  69. array(),
  70. array('_method' => 'GET|head')
  71. ));
  72. // method requirement (again)
  73. $collection->add('baragain', new Route(
  74. '/baragain/{foo}',
  75. array(),
  76. array('_method' => 'get|post')
  77. ));
  78. // simple
  79. $collection->add('baz', new Route(
  80. '/test/baz'
  81. ));
  82. // simple with extension
  83. $collection->add('baz2', new Route(
  84. '/test/baz.html'
  85. ));
  86. // trailing slash
  87. $collection->add('baz3', new Route(
  88. '/test/baz3/'
  89. ));
  90. // trailing slash with variable
  91. $collection->add('baz4', new Route(
  92. '/test/{foo}/'
  93. ));
  94. // trailing slash and method
  95. $collection->add('baz5', new Route(
  96. '/test/{foo}/',
  97. array(),
  98. array('_method' => 'post')
  99. ));
  100. // complex
  101. $collection->add('baz6', new Route(
  102. '/test/baz',
  103. array('foo' => 'bar baz')
  104. ));
  105. // space in path
  106. $collection->add('baz7', new Route(
  107. '/te st/baz'
  108. ));
  109. return $collection;
  110. }
  111. }