4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Routing\Route;
16 use Symfony\Component\Routing\RouteCollection;
17 use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
22 class LegacyApacheMatcherDumperTest extends TestCase
24 protected static $fixturesPath;
26 public static function setUpBeforeClass()
28 self::$fixturesPath = realpath(__DIR__.'/../../Fixtures/');
31 public function testDump()
33 $dumper = new ApacheMatcherDumper($this->getRouteCollection());
35 $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.apache', $dumper->dump(), '->dump() dumps basic routes to the correct apache format.');
39 * @dataProvider provideEscapeFixtures
41 public function testEscapePattern($src, $dest, $char, $with, $message)
43 $r = new \ReflectionMethod(new ApacheMatcherDumper($this->getRouteCollection()), 'escape');
44 $r->setAccessible(true);
45 $this->assertEquals($dest, $r->invoke(null, $src, $char, $with), $message);
48 public function provideEscapeFixtures()
51 array('foo', 'foo', ' ', '-', 'Preserve string that should not be escaped'),
52 array('fo-o', 'fo-o', ' ', '-', 'Preserve string that should not be escaped'),
53 array('fo o', 'fo- o', ' ', '-', 'Escape special characters'),
54 array('fo-- o', 'fo--- o', ' ', '-', 'Escape special characters'),
55 array('fo- o', 'fo- o', ' ', '-', 'Do not escape already escaped string'),
59 public function testEscapeScriptName()
61 $collection = new RouteCollection();
62 $collection->add('foo', new Route('/foo'));
63 $dumper = new ApacheMatcherDumper($collection);
64 $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher2.apache', $dumper->dump(array('script_name' => 'ap p_d\ ev.php')));
67 private function getRouteCollection()
69 $collection = new RouteCollection();
71 // defaults and requirements
72 $collection->add('foo', new Route(
74 array('def' => 'test'),
75 array('bar' => 'baz|symfony')
77 // defaults parameters in pattern
78 $collection->add('foobar', new Route(
80 array('bar' => 'toto')
83 $collection->add('bar', new Route(
92 // method requirement (again)
93 $collection->add('baragain', new Route(
103 $collection->add('baz', new Route(
106 // simple with extension
107 $collection->add('baz2', new Route(
111 $collection->add('baz3', new Route(
114 // trailing slash with variable
115 $collection->add('baz4', new Route(
118 // trailing slash and safe method
119 $collection->add('baz5', new Route(
128 // trailing slash and unsafe method
129 $collection->add('baz5unsafe', new Route(
130 '/testunsafe/{foo}/',
139 $collection->add('baz6', new Route(
141 array('foo' => 'bar baz')
144 $collection->add('baz7', new Route(
147 // space preceded with \ in path
148 $collection->add('baz8', new Route(
151 // space preceded with \ in requirement
152 $collection->add('baz9', new Route(
156 'baz' => 'te\\\\ st',
160 $collection1 = new RouteCollection();
162 $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
163 $collection1->add('route1', $route1);
165 $collection2 = new RouteCollection();
167 $route2 = new Route('/route2', array(), array(), array(), 'a.example.com');
168 $collection2->add('route2', $route2);
170 $route3 = new Route('/route3', array(), array(), array(), 'b.example.com');
171 $collection2->add('route3', $route3);
173 $collection2->addPrefix('/c2');
174 $collection1->addCollection($collection2);
176 $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
177 $collection1->add('route4', $route4);
179 $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
180 $collection1->add('route5', $route5);
182 $route6 = new Route('/route6', array(), array(), array(), null);
183 $collection1->add('route6', $route6);
185 $collection->addCollection($collection1);
187 // host and variables
189 $collection1 = new RouteCollection();
191 $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
192 $collection1->add('route11', $route11);
194 $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
195 $collection1->add('route12', $route12);
197 $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
198 $collection1->add('route13', $route13);
200 $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
201 $collection1->add('route14', $route14);
203 $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
204 $collection1->add('route15', $route15);
206 $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
207 $collection1->add('route16', $route16);
209 $route17 = new Route('/route17', array(), array(), array(), null);
210 $collection1->add('route17', $route17);
212 $collection->addCollection($collection1);