ApacheMatcherDumperTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 testEscape($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. private function getRouteCollection()
  46. {
  47. $collection = new RouteCollection();
  48. // defaults and requirements
  49. $collection->add('foo', new Route(
  50. '/foo/{bar}',
  51. array('def' => 'test'),
  52. array('bar' => 'baz|symfony')
  53. ));
  54. // method requirement
  55. $collection->add('bar', new Route(
  56. '/bar/{foo}',
  57. array(),
  58. array('_method' => 'GET|head')
  59. ));
  60. // method requirement (again)
  61. $collection->add('baragain', new Route(
  62. '/baragain/{foo}',
  63. array(),
  64. array('_method' => 'get|post')
  65. ));
  66. // simple
  67. $collection->add('baz', new Route(
  68. '/test/baz'
  69. ));
  70. // simple with extension
  71. $collection->add('baz2', new Route(
  72. '/test/baz.html'
  73. ));
  74. // trailing slash
  75. $collection->add('baz3', new Route(
  76. '/test/baz3/'
  77. ));
  78. // trailing slash with variable
  79. $collection->add('baz4', new Route(
  80. '/test/{foo}/'
  81. ));
  82. // trailing slash and method
  83. $collection->add('baz5', new Route(
  84. '/test/{foo}/',
  85. array(),
  86. array('_method' => 'post')
  87. ));
  88. // complex
  89. $collection->add('baz6', new Route(
  90. '/test/baz',
  91. array('foo' => 'bar baz')
  92. ));
  93. // space in path
  94. $collection->add('baz7', new Route(
  95. '/te st/baz'
  96. ));
  97. return $collection;
  98. }
  99. }