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