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 Symfony\Component\Routing\Route
;
15 use Symfony\Component\Routing\RouteCollection
;
16 use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper
;
18 class ApacheMatcherDumperTest
extends \PHPUnit_Framework_TestCase
20 protected static $fixturesPath;
22 public static function setUpBeforeClass()
24 self
::$fixturesPath = realpath(__DIR__
.'/../../Fixtures/');
27 public function testDump()
29 $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.');
35 * @dataProvider provideEscapeFixtures
37 public function testEscapePattern($src, $dest, $char, $with, $message)
39 $r = new \
ReflectionMethod(new ApacheMatcherDumper($this->getRouteCollection()), 'escape');
40 $r->setAccessible(true);
41 $this->assertEquals($dest, $r->invoke(null, $src, $char, $with), $message);
44 public function provideEscapeFixtures()
47 array('foo', 'foo', ' ', '-', 'Preserve string that should not be escaped'),
48 array('fo-o', 'fo-o', ' ', '-', 'Preserve string that should not be escaped'),
49 array('fo o', 'fo- o', ' ', '-', 'Escape special characters'),
50 array('fo-- o', 'fo--- o', ' ', '-', 'Escape special characters'),
51 array('fo- o', 'fo- o', ' ', '-', 'Do not escape already escaped string'),
55 public function testEscapeScriptName()
57 $collection = new RouteCollection();
58 $collection->add('foo', new Route('/foo'));
59 $dumper = new ApacheMatcherDumper($collection);
60 $this->assertStringEqualsFile(self
::$fixturesPath.'/dumper/url_matcher2.apache', $dumper->dump(array('script_name' => 'ap p_d\ ev.php')));
63 private function getRouteCollection()
65 $collection = new RouteCollection();
67 // defaults and requirements
68 $collection->add('foo', new Route(
70 array('def' => 'test'),
71 array('bar' => 'baz|symfony')
73 // defaults parameters in pattern
74 $collection->add('foobar', new Route(
76 array('bar' => 'toto')
79 $collection->add('bar', new Route(
82 array('_method' => 'GET|head')
84 // method requirement (again)
85 $collection->add('baragain', new Route(
88 array('_method' => 'get|post')
91 $collection->add('baz', new Route(
94 // simple with extension
95 $collection->add('baz2', new Route(
99 $collection->add('baz3', new Route(
102 // trailing slash with variable
103 $collection->add('baz4', new Route(
106 // trailing slash and safe method
107 $collection->add('baz5', new Route(
110 array('_method' => 'get')
112 // trailing slash and unsafe method
113 $collection->add('baz5unsafe', new Route(
114 '/testunsafe/{foo}/',
116 array('_method' => 'post')
119 $collection->add('baz6', new Route(
121 array('foo' => 'bar baz')
124 $collection->add('baz7', new Route(
127 // space preceded with \ in path
128 $collection->add('baz8', new Route(
131 // space preceded with \ in requirement
132 $collection->add('baz9', new Route(
136 'baz' => 'te\\\\ st',
140 $collection1 = new RouteCollection();
142 $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
143 $collection1->add('route1', $route1);
145 $collection2 = new RouteCollection();
147 $route2 = new Route('/route2', array(), array(), array(), 'a.example.com');
148 $collection2->add('route2', $route2);
150 $route3 = new Route('/route3', array(), array(), array(), 'b.example.com');
151 $collection2->add('route3', $route3);
153 $collection2->addPrefix('/c2');
154 $collection1->addCollection($collection2);
156 $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
157 $collection1->add('route4', $route4);
159 $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
160 $collection1->add('route5', $route5);
162 $route6 = new Route('/route6', array(), array(), array(), null);
163 $collection1->add('route6', $route6);
165 $collection->addCollection($collection1);
167 // host and variables
169 $collection1 = new RouteCollection();
171 $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
172 $collection1->add('route11', $route11);
174 $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
175 $collection1->add('route12', $route12);
177 $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
178 $collection1->add('route13', $route13);
180 $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
181 $collection1->add('route14', $route14);
183 $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
184 $collection1->add('route15', $route15);
186 $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
187 $collection1->add('route16', $route16);
189 $route17 = new Route('/route17', array(), array(), array(), null);
190 $collection1->add('route17', $route17);
192 $collection->addCollection($collection1);