ApacheMatcherDumperTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. static protected $fixturesPath;
  17. static public 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. // method requirement
  62. $collection->add('bar', new Route(
  63. '/bar/{foo}',
  64. array(),
  65. array('_method' => 'GET|head')
  66. ));
  67. // method requirement (again)
  68. $collection->add('baragain', new Route(
  69. '/baragain/{foo}',
  70. array(),
  71. array('_method' => 'get|post')
  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
  96. $collection->add('baz6', new Route(
  97. '/test/baz',
  98. array('foo' => 'bar baz')
  99. ));
  100. // space in path
  101. $collection->add('baz7', new Route(
  102. '/te st/baz'
  103. ));
  104. return $collection;
  105. }
  106. }