]> git.immae.eu Git - github/wallabag/wallabag.git/blame - vendor/symfony/routing/Symfony/Component/Routing/Tests/Matcher/Dumper/ApacheMatcherDumperTest.php
gitignore vendor
[github/wallabag/wallabag.git] / vendor / symfony / routing / Symfony / Component / Routing / Tests / Matcher / Dumper / ApacheMatcherDumperTest.php
CommitLineData
4f5b44bd
NL
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
13
14use Symfony\Component\Routing\Route;
15use Symfony\Component\Routing\RouteCollection;
16use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
17
18class ApacheMatcherDumperTest extends \PHPUnit_Framework_TestCase
19{
20 protected static $fixturesPath;
21
22 public static function setUpBeforeClass()
23 {
24 self::$fixturesPath = realpath(__DIR__.'/../../Fixtures/');
25 }
26
27 public function testDump()
28 {
29 $dumper = new ApacheMatcherDumper($this->getRouteCollection());
30
31 $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.apache', $dumper->dump(), '->dump() dumps basic routes to the correct apache format.');
32 }
33
34 /**
35 * @dataProvider provideEscapeFixtures
36 */
37 public function testEscapePattern($src, $dest, $char, $with, $message)
38 {
39 $r = new \ReflectionMethod(new ApacheMatcherDumper($this->getRouteCollection()), 'escape');
40 $r->setAccessible(true);
41 $this->assertEquals($dest, $r->invoke(null, $src, $char, $with), $message);
42 }
43
44 public function provideEscapeFixtures()
45 {
46 return array(
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'),
52 );
53 }
54
55 public function testEscapeScriptName()
56 {
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')));
61 }
62
63 private function getRouteCollection()
64 {
65 $collection = new RouteCollection();
66
67 // defaults and requirements
68 $collection->add('foo', new Route(
69 '/foo/{bar}',
70 array('def' => 'test'),
71 array('bar' => 'baz|symfony')
72 ));
73 // defaults parameters in pattern
74 $collection->add('foobar', new Route(
75 '/foo/{bar}',
76 array('bar' => 'toto')
77 ));
78 // method requirement
79 $collection->add('bar', new Route(
80 '/bar/{foo}',
81 array(),
82 array('_method' => 'GET|head')
83 ));
84 // method requirement (again)
85 $collection->add('baragain', new Route(
86 '/baragain/{foo}',
87 array(),
88 array('_method' => '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('_method' => 'get')
111 ));
112 // trailing slash and unsafe method
113 $collection->add('baz5unsafe', new Route(
114 '/testunsafe/{foo}/',
115 array(),
116 array('_method' => 'post')
117 ));
118 // complex
119 $collection->add('baz6', new Route(
120 '/test/baz',
121 array('foo' => 'bar baz')
122 ));
123 // space in path
124 $collection->add('baz7', new Route(
125 '/te st/baz'
126 ));
127 // space preceded with \ in path
128 $collection->add('baz8', new Route(
129 '/te\\ st/baz'
130 ));
131 // space preceded with \ in requirement
132 $collection->add('baz9', new Route(
133 '/test/{baz}',
134 array(),
135 array(
136 'baz' => 'te\\\\ st',
137 )
138 ));
139
140 $collection1 = new RouteCollection();
141
142 $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
143 $collection1->add('route1', $route1);
144
145 $collection2 = new RouteCollection();
146
147 $route2 = new Route('/route2', array(), array(), array(), 'a.example.com');
148 $collection2->add('route2', $route2);
149
150 $route3 = new Route('/route3', array(), array(), array(), 'b.example.com');
151 $collection2->add('route3', $route3);
152
153 $collection2->addPrefix('/c2');
154 $collection1->addCollection($collection2);
155
156 $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
157 $collection1->add('route4', $route4);
158
159 $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
160 $collection1->add('route5', $route5);
161
162 $route6 = new Route('/route6', array(), array(), array(), null);
163 $collection1->add('route6', $route6);
164
165 $collection->addCollection($collection1);
166
167 // host and variables
168
169 $collection1 = new RouteCollection();
170
171 $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
172 $collection1->add('route11', $route11);
173
174 $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
175 $collection1->add('route12', $route12);
176
177 $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
178 $collection1->add('route13', $route13);
179
180 $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
181 $collection1->add('route14', $route14);
182
183 $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
184 $collection1->add('route15', $route15);
185
186 $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
187 $collection1->add('route16', $route16);
188
189 $route17 = new Route('/route17', array(), array(), array(), null);
190 $collection1->add('route17', $route17);
191
192 $collection->addCollection($collection1);
193
194 return $collection;
195 }
196}