]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/routing/Symfony/Component/Routing/Tests/RouteCollectionTest.php
3d78adf9232b6576c842bcfa3b5955fc4e024a74
[github/wallabag/wallabag.git] / vendor / symfony / routing / Symfony / Component / Routing / Tests / RouteCollectionTest.php
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;
13
14 use Symfony\Component\Routing\RouteCollection;
15 use Symfony\Component\Routing\Route;
16 use Symfony\Component\Config\Resource\FileResource;
17
18 class RouteCollectionTest extends \PHPUnit_Framework_TestCase
19 {
20 public function testRoute()
21 {
22 $collection = new RouteCollection();
23 $route = new Route('/foo');
24 $collection->add('foo', $route);
25 $this->assertEquals(array('foo' => $route), $collection->all(), '->add() adds a route');
26 $this->assertEquals($route, $collection->get('foo'), '->get() returns a route by name');
27 $this->assertNull($collection->get('bar'), '->get() returns null if a route does not exist');
28 }
29
30 public function testOverriddenRoute()
31 {
32 $collection = new RouteCollection();
33 $collection->add('foo', new Route('/foo'));
34 $collection->add('foo', new Route('/foo1'));
35
36 $this->assertEquals('/foo1', $collection->get('foo')->getPath());
37 }
38
39 public function testDeepOverriddenRoute()
40 {
41 $collection = new RouteCollection();
42 $collection->add('foo', new Route('/foo'));
43
44 $collection1 = new RouteCollection();
45 $collection1->add('foo', new Route('/foo1'));
46
47 $collection2 = new RouteCollection();
48 $collection2->add('foo', new Route('/foo2'));
49
50 $collection1->addCollection($collection2);
51 $collection->addCollection($collection1);
52
53 $this->assertEquals('/foo2', $collection1->get('foo')->getPath());
54 $this->assertEquals('/foo2', $collection->get('foo')->getPath());
55 }
56
57 public function testIterator()
58 {
59 $collection = new RouteCollection();
60 $collection->add('foo', new Route('/foo'));
61
62 $collection1 = new RouteCollection();
63 $collection1->add('bar', $bar = new Route('/bar'));
64 $collection1->add('foo', $foo = new Route('/foo-new'));
65 $collection->addCollection($collection1);
66 $collection->add('last', $last = new Route('/last'));
67
68 $this->assertInstanceOf('\ArrayIterator', $collection->getIterator());
69 $this->assertSame(array('bar' => $bar, 'foo' => $foo, 'last' => $last), $collection->getIterator()->getArrayCopy());
70 }
71
72 public function testCount()
73 {
74 $collection = new RouteCollection();
75 $collection->add('foo', new Route('/foo'));
76
77 $collection1 = new RouteCollection();
78 $collection1->add('bar', new Route('/bar'));
79 $collection->addCollection($collection1);
80
81 $this->assertCount(2, $collection);
82 }
83
84 public function testAddCollection()
85 {
86 $collection = new RouteCollection();
87 $collection->add('foo', new Route('/foo'));
88
89 $collection1 = new RouteCollection();
90 $collection1->add('bar', $bar = new Route('/bar'));
91 $collection1->add('foo', $foo = new Route('/foo-new'));
92
93 $collection2 = new RouteCollection();
94 $collection2->add('grandchild', $grandchild = new Route('/grandchild'));
95
96 $collection1->addCollection($collection2);
97 $collection->addCollection($collection1);
98 $collection->add('last', $last = new Route('/last'));
99
100 $this->assertSame(array('bar' => $bar, 'foo' => $foo, 'grandchild' => $grandchild, 'last' => $last), $collection->all(),
101 '->addCollection() imports routes of another collection, overrides if necessary and adds them at the end');
102 }
103
104 public function testAddCollectionWithResources()
105 {
106 if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
107 $this->markTestSkipped('The "Config" component is not available');
108 }
109
110 $collection = new RouteCollection();
111 $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
112 $collection1 = new RouteCollection();
113 $collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
114 $collection->addCollection($collection1);
115 $this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
116 }
117
118 public function testAddDefaultsAndRequirementsAndOptions()
119 {
120 $collection = new RouteCollection();
121 $collection->add('foo', new Route('/{placeholder}'));
122 $collection1 = new RouteCollection();
123 $collection1->add('bar', new Route('/{placeholder}',
124 array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
125 );
126 $collection->addCollection($collection1);
127
128 $collection->addDefaults(array('placeholder' => 'new-default'));
129 $this->assertEquals(array('placeholder' => 'new-default'), $collection->get('foo')->getDefaults(), '->addDefaults() adds defaults to all routes');
130 $this->assertEquals(array('_controller' => 'fixed', 'placeholder' => 'new-default'), $collection->get('bar')->getDefaults(),
131 '->addDefaults() adds defaults to all routes and overwrites existing ones');
132
133 $collection->addRequirements(array('placeholder' => '\d+'));
134 $this->assertEquals(array('placeholder' => '\d+'), $collection->get('foo')->getRequirements(), '->addRequirements() adds requirements to all routes');
135 $this->assertEquals(array('placeholder' => '\d+'), $collection->get('bar')->getRequirements(),
136 '->addRequirements() adds requirements to all routes and overwrites existing ones');
137
138 $collection->addOptions(array('option' => 'new-value'));
139 $this->assertEquals(
140 array('option' => 'new-value', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
141 $collection->get('bar')->getOptions(), '->addOptions() adds options to all routes and overwrites existing ones'
142 );
143 }
144
145 public function testAddPrefix()
146 {
147 $collection = new RouteCollection();
148 $collection->add('foo', $foo = new Route('/foo'));
149 $collection2 = new RouteCollection();
150 $collection2->add('bar', $bar = new Route('/bar'));
151 $collection->addCollection($collection2);
152 $collection->addPrefix(' / ');
153 $this->assertSame('/foo', $collection->get('foo')->getPattern(), '->addPrefix() trims the prefix and a single slash has no effect');
154 $collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'));
155 $this->assertEquals('/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() adds a prefix to all routes');
156 $this->assertEquals('/{admin}/bar', $collection->get('bar')->getPath(), '->addPrefix() adds a prefix to all routes');
157 $this->assertEquals(array('admin' => 'admin'), $collection->get('foo')->getDefaults(), '->addPrefix() adds defaults to all routes');
158 $this->assertEquals(array('admin' => 'admin'), $collection->get('bar')->getDefaults(), '->addPrefix() adds defaults to all routes');
159 $this->assertEquals(array('admin' => '\d+'), $collection->get('foo')->getRequirements(), '->addPrefix() adds requirements to all routes');
160 $this->assertEquals(array('admin' => '\d+'), $collection->get('bar')->getRequirements(), '->addPrefix() adds requirements to all routes');
161 $collection->addPrefix('0');
162 $this->assertEquals('/0/{admin}/foo', $collection->get('foo')->getPattern(), '->addPrefix() ensures a prefix must start with a slash and must not end with a slash');
163 $collection->addPrefix('/ /');
164 $this->assertSame('/ /0/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() can handle spaces if desired');
165 $this->assertSame('/ /0/{admin}/bar', $collection->get('bar')->getPath(), 'the route pattern of an added collection is in synch with the added prefix');
166 }
167
168 public function testAddPrefixOverridesDefaultsAndRequirements()
169 {
170 $collection = new RouteCollection();
171 $collection->add('foo', $foo = new Route('/foo'));
172 $collection->add('bar', $bar = new Route('/bar', array(), array('_scheme' => 'http')));
173 $collection->addPrefix('/admin', array(), array('_scheme' => 'https'));
174
175 $this->assertEquals('https', $collection->get('foo')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
176 $this->assertEquals('https', $collection->get('bar')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
177 }
178
179 public function testResource()
180 {
181 if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
182 $this->markTestSkipped('The "Config" component is not available');
183 }
184
185 $collection = new RouteCollection();
186 $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
187 $collection->addResource($bar = new FileResource(__DIR__.'/Fixtures/bar.xml'));
188 $collection->addResource(new FileResource(__DIR__.'/Fixtures/foo.xml'));
189
190 $this->assertEquals(array($foo, $bar), $collection->getResources(),
191 '->addResource() adds a resource and getResources() only returns unique ones by comparing the string representation');
192 }
193
194 public function testUniqueRouteWithGivenName()
195 {
196 $collection1 = new RouteCollection();
197 $collection1->add('foo', new Route('/old'));
198 $collection2 = new RouteCollection();
199 $collection3 = new RouteCollection();
200 $collection3->add('foo', $new = new Route('/new'));
201
202 $collection2->addCollection($collection3);
203 $collection1->addCollection($collection2);
204
205 $this->assertSame($new, $collection1->get('foo'), '->get() returns new route that overrode previous one');
206 // size of 1 because collection1 contains /new but not /old anymore
207 $this->assertCount(1, $collection1->getIterator(), '->addCollection() removes previous routes when adding new routes with the same name');
208 }
209
210 public function testGet()
211 {
212 $collection1 = new RouteCollection();
213 $collection1->add('a', $a = new Route('/a'));
214 $collection2 = new RouteCollection();
215 $collection2->add('b', $b = new Route('/b'));
216 $collection1->addCollection($collection2);
217 $collection1->add('$péß^a|', $c = new Route('/special'));
218
219 $this->assertSame($b, $collection1->get('b'), '->get() returns correct route in child collection');
220 $this->assertSame($c, $collection1->get('$péß^a|'), '->get() can handle special characters');
221 $this->assertNull($collection2->get('a'), '->get() does not return the route defined in parent collection');
222 $this->assertNull($collection1->get('non-existent'), '->get() returns null when route does not exist');
223 $this->assertNull($collection1->get(0), '->get() does not disclose internal child RouteCollection');
224 }
225
226 public function testRemove()
227 {
228 $collection = new RouteCollection();
229 $collection->add('foo', $foo = new Route('/foo'));
230
231 $collection1 = new RouteCollection();
232 $collection1->add('bar', $bar = new Route('/bar'));
233 $collection->addCollection($collection1);
234 $collection->add('last', $last = new Route('/last'));
235
236 $collection->remove('foo');
237 $this->assertSame(array('bar' => $bar, 'last' => $last), $collection->all(), '->remove() can remove a single route');
238 $collection->remove(array('bar', 'last'));
239 $this->assertSame(array(), $collection->all(), '->remove() accepts an array and can remove multiple routes at once');
240 }
241
242 public function testSetHost()
243 {
244 $collection = new RouteCollection();
245 $routea = new Route('/a');
246 $routeb = new Route('/b', array(), array(), array(), '{locale}.example.net');
247 $collection->add('a', $routea);
248 $collection->add('b', $routeb);
249
250 $collection->setHost('{locale}.example.com');
251
252 $this->assertEquals('{locale}.example.com', $routea->getHost());
253 $this->assertEquals('{locale}.example.com', $routeb->getHost());
254 }
255 }