LegacyApacheMatcherDumperTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\Component\Routing\Tests\Matcher\Dumper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. /**
  16. * @group legacy
  17. */
  18. class LegacyApacheMatcherDumperTest extends TestCase
  19. {
  20. protected static $fixturesPath;
  21. public static function setUpBeforeClass()
  22. {
  23. self::$fixturesPath = realpath(__DIR__.'/../../Fixtures/');
  24. }
  25. public function testDump()
  26. {
  27. $dumper = new ApacheMatcherDumper($this->getRouteCollection());
  28. $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.apache', $dumper->dump(), '->dump() dumps basic routes to the correct apache format.');
  29. }
  30. /**
  31. * @dataProvider provideEscapeFixtures
  32. */
  33. public function testEscapePattern($src, $dest, $char, $with, $message)
  34. {
  35. $r = new \ReflectionMethod(new ApacheMatcherDumper($this->getRouteCollection()), 'escape');
  36. $r->setAccessible(true);
  37. $this->assertEquals($dest, $r->invoke(null, $src, $char, $with), $message);
  38. }
  39. public function provideEscapeFixtures()
  40. {
  41. return array(
  42. array('foo', 'foo', ' ', '-', 'Preserve string that should not be escaped'),
  43. array('fo-o', 'fo-o', ' ', '-', 'Preserve string that should not be escaped'),
  44. array('fo o', 'fo- o', ' ', '-', 'Escape special characters'),
  45. array('fo-- o', 'fo--- o', ' ', '-', 'Escape special characters'),
  46. array('fo- o', 'fo- o', ' ', '-', 'Do not escape already escaped string'),
  47. );
  48. }
  49. public function testEscapeScriptName()
  50. {
  51. $collection = new RouteCollection();
  52. $collection->add('foo', new Route('/foo'));
  53. $dumper = new ApacheMatcherDumper($collection);
  54. $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher2.apache', $dumper->dump(array('script_name' => 'ap p_d\ ev.php')));
  55. }
  56. private function getRouteCollection()
  57. {
  58. $collection = new RouteCollection();
  59. // defaults and requirements
  60. $collection->add('foo', new Route(
  61. '/foo/{bar}',
  62. array('def' => 'test'),
  63. array('bar' => 'baz|symfony')
  64. ));
  65. // defaults parameters in pattern
  66. $collection->add('foobar', new Route(
  67. '/foo/{bar}',
  68. array('bar' => 'toto')
  69. ));
  70. // method requirement
  71. $collection->add('bar', new Route(
  72. '/bar/{foo}',
  73. array(),
  74. array(),
  75. array(),
  76. '',
  77. array(),
  78. array('GET', 'head')
  79. ));
  80. // method requirement (again)
  81. $collection->add('baragain', new Route(
  82. '/baragain/{foo}',
  83. array(),
  84. array(),
  85. array(),
  86. '',
  87. array(),
  88. array('get', 'post')
  89. ));
  90. // simple
  91. $collection->add('baz', new Route(
  92. '/test/baz'
  93. ));
  94. // simple with extension
  95. $collection->add('baz2', new Route(
  96. '/test/baz.html'
  97. ));
  98. // trailing slash
  99. $collection->add('baz3', new Route(
  100. '/test/baz3/'
  101. ));
  102. // trailing slash with variable
  103. $collection->add('baz4', new Route(
  104. '/test/{foo}/'
  105. ));
  106. // trailing slash and safe method
  107. $collection->add('baz5', new Route(
  108. '/test/{foo}/',
  109. array(),
  110. array(),
  111. array(),
  112. '',
  113. array(),
  114. array('GET')
  115. ));
  116. // trailing slash and unsafe method
  117. $collection->add('baz5unsafe', new Route(
  118. '/testunsafe/{foo}/',
  119. array(),
  120. array(),
  121. array(),
  122. '',
  123. array(),
  124. array('post')
  125. ));
  126. // complex
  127. $collection->add('baz6', new Route(
  128. '/test/baz',
  129. array('foo' => 'bar baz')
  130. ));
  131. // space in path
  132. $collection->add('baz7', new Route(
  133. '/te st/baz'
  134. ));
  135. // space preceded with \ in path
  136. $collection->add('baz8', new Route(
  137. '/te\\ st/baz'
  138. ));
  139. // space preceded with \ in requirement
  140. $collection->add('baz9', new Route(
  141. '/test/{baz}',
  142. array(),
  143. array(
  144. 'baz' => 'te\\\\ st',
  145. )
  146. ));
  147. $collection1 = new RouteCollection();
  148. $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
  149. $collection1->add('route1', $route1);
  150. $collection2 = new RouteCollection();
  151. $route2 = new Route('/route2', array(), array(), array(), 'a.example.com');
  152. $collection2->add('route2', $route2);
  153. $route3 = new Route('/route3', array(), array(), array(), 'b.example.com');
  154. $collection2->add('route3', $route3);
  155. $collection2->addPrefix('/c2');
  156. $collection1->addCollection($collection2);
  157. $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
  158. $collection1->add('route4', $route4);
  159. $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
  160. $collection1->add('route5', $route5);
  161. $route6 = new Route('/route6', array(), array(), array(), null);
  162. $collection1->add('route6', $route6);
  163. $collection->addCollection($collection1);
  164. // host and variables
  165. $collection1 = new RouteCollection();
  166. $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
  167. $collection1->add('route11', $route11);
  168. $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
  169. $collection1->add('route12', $route12);
  170. $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
  171. $collection1->add('route13', $route13);
  172. $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
  173. $collection1->add('route14', $route14);
  174. $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
  175. $collection1->add('route15', $route15);
  176. $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
  177. $collection1->add('route16', $route16);
  178. $route17 = new Route('/route17', array(), array(), array(), null);
  179. $collection1->add('route17', $route17);
  180. $collection->addCollection($collection1);
  181. return $collection;
  182. }
  183. }