]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | namespace Symfony\Component\Routing\Tests\Matcher\Dumper; | |
13 | ||
14 | use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; | |
15 | use Symfony\Component\Routing\Route; | |
16 | use Symfony\Component\Routing\RouteCollection; | |
17 | ||
18 | class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase | |
19 | { | |
20 | /** | |
21 | * @expectedException \LogicException | |
22 | */ | |
23 | public function testDumpWhenSchemeIsUsedWithoutAProperDumper() | |
24 | { | |
25 | $collection = new RouteCollection(); | |
26 | $collection->add('secure', new Route( | |
27 | '/secure', | |
28 | array(), | |
29 | array('_scheme' => 'https') | |
30 | )); | |
31 | $dumper = new PhpMatcherDumper($collection); | |
32 | $dumper->dump(); | |
33 | } | |
34 | ||
35 | /** | |
36 | * @dataProvider getRouteCollections | |
37 | */ | |
38 | public function testDump(RouteCollection $collection, $fixture, $options = array()) | |
39 | { | |
40 | $basePath = __DIR__.'/../../Fixtures/dumper/'; | |
41 | ||
42 | $dumper = new PhpMatcherDumper($collection); | |
43 | $this->assertStringEqualsFile($basePath.$fixture, $dumper->dump($options), '->dump() correctly dumps routes as optimized PHP code.'); | |
44 | } | |
45 | ||
46 | public function getRouteCollections() | |
47 | { | |
48 | /* test case 1 */ | |
49 | ||
50 | $collection = new RouteCollection(); | |
51 | ||
52 | $collection->add('overridden', new Route('/overridden')); | |
53 | ||
54 | // defaults and requirements | |
55 | $collection->add('foo', new Route( | |
56 | '/foo/{bar}', | |
57 | array('def' => 'test'), | |
58 | array('bar' => 'baz|symfony') | |
59 | )); | |
60 | // method requirement | |
61 | $collection->add('bar', new Route( | |
62 | '/bar/{foo}', | |
63 | array(), | |
64 | array('_method' => 'GET|head') | |
65 | )); | |
66 | // GET method requirement automatically adds HEAD as valid | |
67 | $collection->add('barhead', new Route( | |
68 | '/barhead/{foo}', | |
69 | array(), | |
70 | array('_method' => 'GET') | |
71 | )); | |
72 | // simple | |
73 | $collection->add('baz', new Route( | |
74 | '/test/baz' | |
75 | )); | |
76 | // simple with extension | |
77 | $collection->add('baz2', new Route( | |
78 | '/test/baz.html' | |
79 | )); | |
80 | // trailing slash | |
81 | $collection->add('baz3', new Route( | |
82 | '/test/baz3/' | |
83 | )); | |
84 | // trailing slash with variable | |
85 | $collection->add('baz4', new Route( | |
86 | '/test/{foo}/' | |
87 | )); | |
88 | // trailing slash and method | |
89 | $collection->add('baz5', new Route( | |
90 | '/test/{foo}/', | |
91 | array(), | |
92 | array('_method' => 'post') | |
93 | )); | |
94 | // complex name | |
95 | $collection->add('baz.baz6', new Route( | |
96 | '/test/{foo}/', | |
97 | array(), | |
98 | array('_method' => 'put') | |
99 | )); | |
100 | // defaults without variable | |
101 | $collection->add('foofoo', new Route( | |
102 | '/foofoo', | |
103 | array('def' => 'test') | |
104 | )); | |
105 | // pattern with quotes | |
106 | $collection->add('quoter', new Route( | |
107 | '/{quoter}', | |
108 | array(), | |
109 | array('quoter' => '[\']+') | |
110 | )); | |
111 | // space in pattern | |
112 | $collection->add('space', new Route( | |
113 | '/spa ce' | |
114 | )); | |
115 | ||
116 | // prefixes | |
117 | $collection1 = new RouteCollection(); | |
118 | $collection1->add('overridden', new Route('/overridden1')); | |
119 | $collection1->add('foo1', new Route('/{foo}')); | |
120 | $collection1->add('bar1', new Route('/{bar}')); | |
121 | $collection1->addPrefix('/b\'b'); | |
122 | $collection2 = new RouteCollection(); | |
123 | $collection2->addCollection($collection1); | |
124 | $collection2->add('overridden', new Route('/{var}', array(), array('var' => '.*'))); | |
125 | $collection1 = new RouteCollection(); | |
126 | $collection1->add('foo2', new Route('/{foo1}')); | |
127 | $collection1->add('bar2', new Route('/{bar1}')); | |
128 | $collection1->addPrefix('/b\'b'); | |
129 | $collection2->addCollection($collection1); | |
130 | $collection2->addPrefix('/a'); | |
131 | $collection->addCollection($collection2); | |
132 | ||
133 | // overridden through addCollection() and multiple sub-collections with no own prefix | |
134 | $collection1 = new RouteCollection(); | |
135 | $collection1->add('overridden2', new Route('/old')); | |
136 | $collection1->add('helloWorld', new Route('/hello/{who}', array('who' => 'World!'))); | |
137 | $collection2 = new RouteCollection(); | |
138 | $collection3 = new RouteCollection(); | |
139 | $collection3->add('overridden2', new Route('/new')); | |
140 | $collection3->add('hey', new Route('/hey/')); | |
141 | $collection2->addCollection($collection3); | |
142 | $collection1->addCollection($collection2); | |
143 | $collection1->addPrefix('/multi'); | |
144 | $collection->addCollection($collection1); | |
145 | ||
146 | // "dynamic" prefix | |
147 | $collection1 = new RouteCollection(); | |
148 | $collection1->add('foo3', new Route('/{foo}')); | |
149 | $collection1->add('bar3', new Route('/{bar}')); | |
150 | $collection1->addPrefix('/b'); | |
151 | $collection1->addPrefix('{_locale}'); | |
152 | $collection->addCollection($collection1); | |
153 | ||
154 | // route between collections | |
155 | $collection->add('ababa', new Route('/ababa')); | |
156 | ||
157 | // collection with static prefix but only one route | |
158 | $collection1 = new RouteCollection(); | |
159 | $collection1->add('foo4', new Route('/{foo}')); | |
160 | $collection1->addPrefix('/aba'); | |
161 | $collection->addCollection($collection1); | |
162 | ||
163 | // prefix and host | |
164 | ||
165 | $collection1 = new RouteCollection(); | |
166 | ||
167 | $route1 = new Route('/route1', array(), array(), array(), 'a.example.com'); | |
168 | $collection1->add('route1', $route1); | |
169 | ||
170 | $collection2 = new RouteCollection(); | |
171 | ||
172 | $route2 = new Route('/c2/route2', array(), array(), array(), 'a.example.com'); | |
173 | $collection1->add('route2', $route2); | |
174 | ||
175 | $route3 = new Route('/c2/route3', array(), array(), array(), 'b.example.com'); | |
176 | $collection1->add('route3', $route3); | |
177 | ||
178 | $route4 = new Route('/route4', array(), array(), array(), 'a.example.com'); | |
179 | $collection1->add('route4', $route4); | |
180 | ||
181 | $route5 = new Route('/route5', array(), array(), array(), 'c.example.com'); | |
182 | $collection1->add('route5', $route5); | |
183 | ||
184 | $route6 = new Route('/route6', array(), array(), array(), null); | |
185 | $collection1->add('route6', $route6); | |
186 | ||
187 | $collection->addCollection($collection1); | |
188 | ||
189 | // host and variables | |
190 | ||
191 | $collection1 = new RouteCollection(); | |
192 | ||
193 | $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com'); | |
194 | $collection1->add('route11', $route11); | |
195 | ||
196 | $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com'); | |
197 | $collection1->add('route12', $route12); | |
198 | ||
199 | $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com'); | |
200 | $collection1->add('route13', $route13); | |
201 | ||
202 | $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com'); | |
203 | $collection1->add('route14', $route14); | |
204 | ||
205 | $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com'); | |
206 | $collection1->add('route15', $route15); | |
207 | ||
208 | $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null); | |
209 | $collection1->add('route16', $route16); | |
210 | ||
211 | $route17 = new Route('/route17', array(), array(), array(), null); | |
212 | $collection1->add('route17', $route17); | |
213 | ||
214 | $collection->addCollection($collection1); | |
215 | ||
216 | // multiple sub-collections with a single route and a prefix each | |
217 | $collection1 = new RouteCollection(); | |
218 | $collection1->add('a', new Route('/a...')); | |
219 | $collection2 = new RouteCollection(); | |
220 | $collection2->add('b', new Route('/{var}')); | |
221 | $collection3 = new RouteCollection(); | |
222 | $collection3->add('c', new Route('/{var}')); | |
223 | $collection3->addPrefix('/c'); | |
224 | $collection2->addCollection($collection3); | |
225 | $collection2->addPrefix('/b'); | |
226 | $collection1->addCollection($collection2); | |
227 | $collection1->addPrefix('/a'); | |
228 | $collection->addCollection($collection1); | |
229 | ||
230 | /* test case 2 */ | |
231 | ||
232 | $redirectCollection = clone $collection; | |
233 | ||
234 | // force HTTPS redirection | |
235 | $redirectCollection->add('secure', new Route( | |
236 | '/secure', | |
237 | array(), | |
238 | array('_scheme' => 'https') | |
239 | )); | |
240 | ||
241 | // force HTTP redirection | |
242 | $redirectCollection->add('nonsecure', new Route( | |
243 | '/nonsecure', | |
244 | array(), | |
245 | array('_scheme' => 'http') | |
246 | )); | |
247 | ||
248 | /* test case 3 */ | |
249 | ||
250 | $rootprefixCollection = new RouteCollection(); | |
251 | $rootprefixCollection->add('static', new Route('/test')); | |
252 | $rootprefixCollection->add('dynamic', new Route('/{var}')); | |
253 | $rootprefixCollection->addPrefix('rootprefix'); | |
254 | ||
255 | return array( | |
256 | array($collection, 'url_matcher1.php', array()), | |
257 | array($redirectCollection, 'url_matcher2.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')), | |
258 | array($rootprefixCollection, 'url_matcher3.php', array()) | |
259 | ); | |
260 | } | |
261 | } |