/websocket/vendor/symfony/routing/Symfony/Component/Routing/Tests/Matcher/Dumper/ApacheMatcherDumperTest.php

https://github.com/ivebeenlinuxed/Boiler · PHP · 196 lines · 137 code · 33 blank · 26 comment · 0 complexity · 0642be5053da073e465f18f1b8fd887d 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. 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 safe method
  95. $collection->add('baz5', new Route(
  96. '/test/{foo}/',
  97. array(),
  98. array('_method' => 'get')
  99. ));
  100. // trailing slash and unsafe method
  101. $collection->add('baz5unsafe', new Route(
  102. '/testunsafe/{foo}/',
  103. array(),
  104. array('_method' => 'post')
  105. ));
  106. // complex
  107. $collection->add('baz6', new Route(
  108. '/test/baz',
  109. array('foo' => 'bar baz')
  110. ));
  111. // space in path
  112. $collection->add('baz7', new Route(
  113. '/te st/baz'
  114. ));
  115. // space preceded with \ in path
  116. $collection->add('baz8', new Route(
  117. '/te\\ st/baz'
  118. ));
  119. // space preceded with \ in requirement
  120. $collection->add('baz9', new Route(
  121. '/test/{baz}',
  122. array(),
  123. array(
  124. 'baz' => 'te\\\\ st',
  125. )
  126. ));
  127. $collection1 = new RouteCollection();
  128. $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
  129. $collection1->add('route1', $route1);
  130. $collection2 = new RouteCollection();
  131. $route2 = new Route('/route2', array(), array(), array(), 'a.example.com');
  132. $collection2->add('route2', $route2);
  133. $route3 = new Route('/route3', array(), array(), array(), 'b.example.com');
  134. $collection2->add('route3', $route3);
  135. $collection2->addPrefix('/c2');
  136. $collection1->addCollection($collection2);
  137. $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
  138. $collection1->add('route4', $route4);
  139. $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
  140. $collection1->add('route5', $route5);
  141. $route6 = new Route('/route6', array(), array(), array(), null);
  142. $collection1->add('route6', $route6);
  143. $collection->addCollection($collection1);
  144. // host and variables
  145. $collection1 = new RouteCollection();
  146. $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
  147. $collection1->add('route11', $route11);
  148. $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
  149. $collection1->add('route12', $route12);
  150. $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
  151. $collection1->add('route13', $route13);
  152. $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
  153. $collection1->add('route14', $route14);
  154. $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
  155. $collection1->add('route15', $route15);
  156. $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
  157. $collection1->add('route16', $route16);
  158. $route17 = new Route('/route17', array(), array(), array(), null);
  159. $collection1->add('route17', $route17);
  160. $collection->addCollection($collection1);
  161. return $collection;
  162. }
  163. }