/application/vendor/symfony/routing/Symfony/Component/Routing/Tests/Matcher/Dumper/LegacyApacheMatcherDumperTest.php

https://gitlab.com/MineYourMind/BoNeMEAL · PHP · 204 lines · 141 code · 34 blank · 29 comment · 0 complexity · bcc7fcea78eb02e2dbba01194ed0b4f3 MD5 · raw file

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