Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
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
        $collection = new RouteCollection();
107
        $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
108
        $collection1 = new RouteCollection();
109
        $collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
110
        $collection->addCollection($collection1);
111
        $this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
112
    }
113
 
114
    public function testAddDefaultsAndRequirementsAndOptions()
115
    {
116
        $collection = new RouteCollection();
117
        $collection->add('foo', new Route('/{placeholder}'));
118
        $collection1 = new RouteCollection();
119
        $collection1->add('bar', new Route('/{placeholder}',
120
            array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
121
        );
122
        $collection->addCollection($collection1);
123
 
124
        $collection->addDefaults(array('placeholder' => 'new-default'));
125
        $this->assertEquals(array('placeholder' => 'new-default'), $collection->get('foo')->getDefaults(), '->addDefaults() adds defaults to all routes');
126
        $this->assertEquals(array('_controller' => 'fixed', 'placeholder' => 'new-default'), $collection->get('bar')->getDefaults(),
127
            '->addDefaults() adds defaults to all routes and overwrites existing ones');
128
 
129
        $collection->addRequirements(array('placeholder' => '\d+'));
130
        $this->assertEquals(array('placeholder' => '\d+'), $collection->get('foo')->getRequirements(), '->addRequirements() adds requirements to all routes');
131
        $this->assertEquals(array('placeholder' => '\d+'), $collection->get('bar')->getRequirements(),
132
            '->addRequirements() adds requirements to all routes and overwrites existing ones');
133
 
134
        $collection->addOptions(array('option' => 'new-value'));
135
        $this->assertEquals(
136
            array('option' => 'new-value', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
137
            $collection->get('bar')->getOptions(), '->addOptions() adds options to all routes and overwrites existing ones'
138
        );
139
    }
140
 
141
    public function testAddPrefix()
142
    {
143
        $collection = new RouteCollection();
144
        $collection->add('foo', $foo = new Route('/foo'));
145
        $collection2 = new RouteCollection();
146
        $collection2->add('bar', $bar = new Route('/bar'));
147
        $collection->addCollection($collection2);
148
        $collection->addPrefix(' / ');
149
        $this->assertSame('/foo', $collection->get('foo')->getPath(), '->addPrefix() trims the prefix and a single slash has no effect');
150
        $collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'));
151
        $this->assertEquals('/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() adds a prefix to all routes');
152
        $this->assertEquals('/{admin}/bar', $collection->get('bar')->getPath(), '->addPrefix() adds a prefix to all routes');
153
        $this->assertEquals(array('admin' => 'admin'), $collection->get('foo')->getDefaults(), '->addPrefix() adds defaults to all routes');
154
        $this->assertEquals(array('admin' => 'admin'), $collection->get('bar')->getDefaults(), '->addPrefix() adds defaults to all routes');
155
        $this->assertEquals(array('admin' => '\d+'), $collection->get('foo')->getRequirements(), '->addPrefix() adds requirements to all routes');
156
        $this->assertEquals(array('admin' => '\d+'), $collection->get('bar')->getRequirements(), '->addPrefix() adds requirements to all routes');
157
        $collection->addPrefix('0');
158
        $this->assertEquals('/0/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() ensures a prefix must start with a slash and must not end with a slash');
159
        $collection->addPrefix('/ /');
160
        $this->assertSame('/ /0/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() can handle spaces if desired');
161
        $this->assertSame('/ /0/{admin}/bar', $collection->get('bar')->getPath(), 'the route pattern of an added collection is in synch with the added prefix');
162
    }
163
 
164
    public function testAddPrefixOverridesDefaultsAndRequirements()
165
    {
166
        $collection = new RouteCollection();
167
        $collection->add('foo', $foo = new Route('/foo.{_format}'));
168
        $collection->add('bar', $bar = new Route('/bar.{_format}', array(), array('_format' => 'json')));
169
        $collection->addPrefix('/admin', array(), array('_format' => 'html'));
170
 
171
        $this->assertEquals('html', $collection->get('foo')->getRequirement('_format'), '->addPrefix() overrides existing requirements');
172
        $this->assertEquals('html', $collection->get('bar')->getRequirement('_format'), '->addPrefix() overrides existing requirements');
173
    }
174
 
175
    public function testResource()
176
    {
177
        $collection = new RouteCollection();
178
        $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
179
        $collection->addResource($bar = new FileResource(__DIR__.'/Fixtures/bar.xml'));
180
        $collection->addResource(new FileResource(__DIR__.'/Fixtures/foo.xml'));
181
 
182
        $this->assertEquals(array($foo, $bar), $collection->getResources(),
183
            '->addResource() adds a resource and getResources() only returns unique ones by comparing the string representation');
184
    }
185
 
186
    public function testUniqueRouteWithGivenName()
187
    {
188
        $collection1 = new RouteCollection();
189
        $collection1->add('foo', new Route('/old'));
190
        $collection2 = new RouteCollection();
191
        $collection3 = new RouteCollection();
192
        $collection3->add('foo', $new = new Route('/new'));
193
 
194
        $collection2->addCollection($collection3);
195
        $collection1->addCollection($collection2);
196
 
197
        $this->assertSame($new, $collection1->get('foo'), '->get() returns new route that overrode previous one');
198
        // size of 1 because collection1 contains /new but not /old anymore
199
        $this->assertCount(1, $collection1->getIterator(), '->addCollection() removes previous routes when adding new routes with the same name');
200
    }
201
 
202
    public function testGet()
203
    {
204
        $collection1 = new RouteCollection();
205
        $collection1->add('a', $a = new Route('/a'));
206
        $collection2 = new RouteCollection();
207
        $collection2->add('b', $b = new Route('/b'));
208
        $collection1->addCollection($collection2);
209
        $collection1->add('$péß^a|', $c = new Route('/special'));
210
 
211
        $this->assertSame($b, $collection1->get('b'), '->get() returns correct route in child collection');
212
        $this->assertSame($c, $collection1->get('$péß^a|'), '->get() can handle special characters');
213
        $this->assertNull($collection2->get('a'), '->get() does not return the route defined in parent collection');
214
        $this->assertNull($collection1->get('non-existent'), '->get() returns null when route does not exist');
215
        $this->assertNull($collection1->get(0), '->get() does not disclose internal child RouteCollection');
216
    }
217
 
218
    public function testRemove()
219
    {
220
        $collection = new RouteCollection();
221
        $collection->add('foo', $foo = new Route('/foo'));
222
 
223
        $collection1 = new RouteCollection();
224
        $collection1->add('bar', $bar = new Route('/bar'));
225
        $collection->addCollection($collection1);
226
        $collection->add('last', $last = new Route('/last'));
227
 
228
        $collection->remove('foo');
229
        $this->assertSame(array('bar' => $bar, 'last' => $last), $collection->all(), '->remove() can remove a single route');
230
        $collection->remove(array('bar', 'last'));
231
        $this->assertSame(array(), $collection->all(), '->remove() accepts an array and can remove multiple routes at once');
232
    }
233
 
234
    public function testSetHost()
235
    {
236
        $collection = new RouteCollection();
237
        $routea = new Route('/a');
238
        $routeb = new Route('/b', array(), array(), array(), '{locale}.example.net');
239
        $collection->add('a', $routea);
240
        $collection->add('b', $routeb);
241
 
242
        $collection->setHost('{locale}.example.com');
243
 
244
        $this->assertEquals('{locale}.example.com', $routea->getHost());
245
        $this->assertEquals('{locale}.example.com', $routeb->getHost());
246
    }
247
 
248
    public function testSetCondition()
249
    {
250
        $collection = new RouteCollection();
251
        $routea = new Route('/a');
252
        $routeb = new Route('/b', array(), array(), array(), '{locale}.example.net', array(), array(), 'context.getMethod() == "GET"');
253
        $collection->add('a', $routea);
254
        $collection->add('b', $routeb);
255
 
256
        $collection->setCondition('context.getMethod() == "POST"');
257
 
258
        $this->assertEquals('context.getMethod() == "POST"', $routea->getCondition());
259
        $this->assertEquals('context.getMethod() == "POST"', $routeb->getCondition());
260
    }
261
 
262
    public function testClone()
263
    {
264
        $collection = new RouteCollection();
265
        $collection->add('a', new Route('/a'));
266
        $collection->add('b', new Route('/b', array('placeholder' => 'default'), array('placeholder' => '.+')));
267
 
268
        $clonedCollection = clone $collection;
269
 
270
        $this->assertCount(2, $clonedCollection);
271
        $this->assertEquals($collection->get('a'), $clonedCollection->get('a'));
272
        $this->assertNotSame($collection->get('a'), $clonedCollection->get('a'));
273
        $this->assertEquals($collection->get('b'), $clonedCollection->get('b'));
274
        $this->assertNotSame($collection->get('b'), $clonedCollection->get('b'));
275
    }
276
 
277
    public function testSetSchemes()
278
    {
279
        $collection = new RouteCollection();
280
        $routea = new Route('/a', array(), array(), array(), '', 'http');
281
        $routeb = new Route('/b');
282
        $collection->add('a', $routea);
283
        $collection->add('b', $routeb);
284
 
285
        $collection->setSchemes(array('http', 'https'));
286
 
287
        $this->assertEquals(array('http', 'https'), $routea->getSchemes());
288
        $this->assertEquals(array('http', 'https'), $routeb->getSchemes());
289
    }
290
 
291
    public function testSetMethods()
292
    {
293
        $collection = new RouteCollection();
294
        $routea = new Route('/a', array(), array(), array(), '', array(), array('GET', 'POST'));
295
        $routeb = new Route('/b');
296
        $collection->add('a', $routea);
297
        $collection->add('b', $routeb);
298
 
299
        $collection->setMethods('PUT');
300
 
301
        $this->assertEquals(array('PUT'), $routea->getMethods());
302
        $this->assertEquals(array('PUT'), $routeb->getMethods());
303
    }
304
}