| 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\Route;
|
|
|
15 |
|
|
|
16 |
class RouteTest extends \PHPUnit_Framework_TestCase
|
|
|
17 |
{
|
|
|
18 |
public function testConstructor()
|
|
|
19 |
{
|
|
|
20 |
$route = new Route('/{foo}', array('foo' => 'bar'), array('foo' => '\d+'), array('foo' => 'bar'), '{locale}.example.com');
|
|
|
21 |
$this->assertEquals('/{foo}', $route->getPath(), '__construct() takes a path as its first argument');
|
|
|
22 |
$this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '__construct() takes defaults as its second argument');
|
|
|
23 |
$this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '__construct() takes requirements as its third argument');
|
|
|
24 |
$this->assertEquals('bar', $route->getOption('foo'), '__construct() takes options as its fourth argument');
|
|
|
25 |
$this->assertEquals('{locale}.example.com', $route->getHost(), '__construct() takes a host pattern as its fifth argument');
|
|
|
26 |
|
|
|
27 |
$route = new Route('/', array(), array(), array(), '', array('Https'), array('POST', 'put'), 'context.getMethod() == "GET"');
|
|
|
28 |
$this->assertEquals(array('https'), $route->getSchemes(), '__construct() takes schemes as its sixth argument and lowercases it');
|
|
|
29 |
$this->assertEquals(array('POST', 'PUT'), $route->getMethods(), '__construct() takes methods as its seventh argument and uppercases it');
|
|
|
30 |
$this->assertEquals('context.getMethod() == "GET"', $route->getCondition(), '__construct() takes a condition as its eight argument');
|
|
|
31 |
|
|
|
32 |
$route = new Route('/', array(), array(), array(), '', 'Https', 'Post');
|
|
|
33 |
$this->assertEquals(array('https'), $route->getSchemes(), '__construct() takes a single scheme as its sixth argument');
|
|
|
34 |
$this->assertEquals(array('POST'), $route->getMethods(), '__construct() takes a single method as its seventh argument');
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public function testPath()
|
|
|
38 |
{
|
|
|
39 |
$route = new Route('/{foo}');
|
|
|
40 |
$route->setPath('/{bar}');
|
|
|
41 |
$this->assertEquals('/{bar}', $route->getPath(), '->setPath() sets the path');
|
|
|
42 |
$route->setPath('');
|
|
|
43 |
$this->assertEquals('/', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
|
|
|
44 |
$route->setPath('bar');
|
|
|
45 |
$this->assertEquals('/bar', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
|
|
|
46 |
$this->assertEquals($route, $route->setPath(''), '->setPath() implements a fluent interface');
|
|
|
47 |
$route->setPath('//path');
|
|
|
48 |
$this->assertEquals('/path', $route->getPath(), '->setPath() does not allow two slashes "//" at the beginning of the path as it would be confused with a network path when generating the path from the route');
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function testOptions()
|
|
|
52 |
{
|
|
|
53 |
$route = new Route('/{foo}');
|
|
|
54 |
$route->setOptions(array('foo' => 'bar'));
|
|
|
55 |
$this->assertEquals(array_merge(array(
|
|
|
56 |
'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
|
|
|
57 |
), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
|
|
|
58 |
$this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
|
|
|
59 |
|
|
|
60 |
$route->setOptions(array('foo' => 'foo'));
|
|
|
61 |
$route->addOptions(array('bar' => 'bar'));
|
|
|
62 |
$this->assertEquals($route, $route->addOptions(array()), '->addOptions() implements a fluent interface');
|
|
|
63 |
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $route->getOptions(), '->addDefaults() keep previous defaults');
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public function testOption()
|
|
|
67 |
{
|
|
|
68 |
$route = new Route('/{foo}');
|
|
|
69 |
$this->assertFalse($route->hasOption('foo'), '->hasOption() return false if option is not set');
|
|
|
70 |
$this->assertEquals($route, $route->setOption('foo', 'bar'), '->setOption() implements a fluent interface');
|
|
|
71 |
$this->assertEquals('bar', $route->getOption('foo'), '->setOption() sets the option');
|
|
|
72 |
$this->assertTrue($route->hasOption('foo'), '->hasOption() return true if option is set');
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public function testDefaults()
|
|
|
76 |
{
|
|
|
77 |
$route = new Route('/{foo}');
|
|
|
78 |
$route->setDefaults(array('foo' => 'bar'));
|
|
|
79 |
$this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
|
|
|
80 |
$this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
|
|
|
81 |
|
|
|
82 |
$route->setDefault('foo', 'bar');
|
|
|
83 |
$this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');
|
|
|
84 |
|
|
|
85 |
$route->setDefault('foo2', 'bar2');
|
|
|
86 |
$this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
|
|
|
87 |
$this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not set');
|
|
|
88 |
|
|
|
89 |
$route->setDefault('_controller', $closure = function () { return 'Hello'; });
|
|
|
90 |
$this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
|
|
|
91 |
|
|
|
92 |
$route->setDefaults(array('foo' => 'foo'));
|
|
|
93 |
$route->addDefaults(array('bar' => 'bar'));
|
|
|
94 |
$this->assertEquals($route, $route->addDefaults(array()), '->addDefaults() implements a fluent interface');
|
|
|
95 |
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $route->getDefaults(), '->addDefaults() keep previous defaults');
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
public function testRequirements()
|
|
|
99 |
{
|
|
|
100 |
$route = new Route('/{foo}');
|
|
|
101 |
$route->setRequirements(array('foo' => '\d+'));
|
|
|
102 |
$this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
|
|
|
103 |
$this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
|
|
|
104 |
$this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
|
|
|
105 |
$route->setRequirements(array('foo' => '^\d+$'));
|
|
|
106 |
$this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the path');
|
|
|
107 |
$this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
|
|
|
108 |
|
|
|
109 |
$route->setRequirements(array('foo' => '\d+'));
|
|
|
110 |
$route->addRequirements(array('bar' => '\d+'));
|
|
|
111 |
$this->assertEquals($route, $route->addRequirements(array()), '->addRequirements() implements a fluent interface');
|
|
|
112 |
$this->assertEquals(array('foo' => '\d+', 'bar' => '\d+'), $route->getRequirements(), '->addRequirement() keep previous requirements');
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public function testRequirement()
|
|
|
116 |
{
|
|
|
117 |
$route = new Route('/{foo}');
|
|
|
118 |
$this->assertFalse($route->hasRequirement('foo'), '->hasRequirement() return false if requirement is not set');
|
|
|
119 |
$route->setRequirement('foo', '^\d+$');
|
|
|
120 |
$this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the path');
|
|
|
121 |
$this->assertTrue($route->hasRequirement('foo'), '->hasRequirement() return true if requirement is set');
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* @dataProvider getInvalidRequirements
|
|
|
126 |
* @expectedException \InvalidArgumentException
|
|
|
127 |
*/
|
|
|
128 |
public function testSetInvalidRequirement($req)
|
|
|
129 |
{
|
|
|
130 |
$route = new Route('/{foo}');
|
|
|
131 |
$route->setRequirement('foo', $req);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public function getInvalidRequirements()
|
|
|
135 |
{
|
|
|
136 |
return array(
|
|
|
137 |
array(''),
|
|
|
138 |
array(array()),
|
|
|
139 |
array('^$'),
|
|
|
140 |
array('^'),
|
|
|
141 |
array('$'),
|
|
|
142 |
);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
public function testHost()
|
|
|
146 |
{
|
|
|
147 |
$route = new Route('/');
|
|
|
148 |
$route->setHost('{locale}.example.net');
|
|
|
149 |
$this->assertEquals('{locale}.example.net', $route->getHost(), '->setHost() sets the host pattern');
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
public function testScheme()
|
|
|
153 |
{
|
|
|
154 |
$route = new Route('/');
|
|
|
155 |
$this->assertEquals(array(), $route->getSchemes(), 'schemes is initialized with array()');
|
|
|
156 |
$this->assertFalse($route->hasScheme('http'));
|
|
|
157 |
$route->setSchemes('hTTp');
|
|
|
158 |
$this->assertEquals(array('http'), $route->getSchemes(), '->setSchemes() accepts a single scheme string and lowercases it');
|
|
|
159 |
$this->assertTrue($route->hasScheme('htTp'));
|
|
|
160 |
$this->assertFalse($route->hasScheme('httpS'));
|
|
|
161 |
$route->setSchemes(array('HttpS', 'hTTp'));
|
|
|
162 |
$this->assertEquals(array('https', 'http'), $route->getSchemes(), '->setSchemes() accepts an array of schemes and lowercases them');
|
|
|
163 |
$this->assertTrue($route->hasScheme('htTp'));
|
|
|
164 |
$this->assertTrue($route->hasScheme('httpS'));
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
public function testMethod()
|
|
|
168 |
{
|
|
|
169 |
$route = new Route('/');
|
|
|
170 |
$this->assertEquals(array(), $route->getMethods(), 'methods is initialized with array()');
|
|
|
171 |
$route->setMethods('gEt');
|
|
|
172 |
$this->assertEquals(array('GET'), $route->getMethods(), '->setMethods() accepts a single method string and uppercases it');
|
|
|
173 |
$route->setMethods(array('gEt', 'PosT'));
|
|
|
174 |
$this->assertEquals(array('GET', 'POST'), $route->getMethods(), '->setMethods() accepts an array of methods and uppercases them');
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
public function testCondition()
|
|
|
178 |
{
|
|
|
179 |
$route = new Route('/');
|
|
|
180 |
$this->assertSame('', $route->getCondition());
|
|
|
181 |
$route->setCondition('context.getMethod() == "GET"');
|
|
|
182 |
$this->assertSame('context.getMethod() == "GET"', $route->getCondition());
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
public function testCompile()
|
|
|
186 |
{
|
|
|
187 |
$route = new Route('/{foo}');
|
|
|
188 |
$this->assertInstanceOf('Symfony\Component\Routing\CompiledRoute', $compiled = $route->compile(), '->compile() returns a compiled route');
|
|
|
189 |
$this->assertSame($compiled, $route->compile(), '->compile() only compiled the route once if unchanged');
|
|
|
190 |
$route->setRequirement('foo', '.*');
|
|
|
191 |
$this->assertNotSame($compiled, $route->compile(), '->compile() recompiles if the route was modified');
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
public function testSerialize()
|
|
|
195 |
{
|
|
|
196 |
$route = new Route('/prefix/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
|
|
|
197 |
|
|
|
198 |
$serialized = serialize($route);
|
|
|
199 |
$unserialized = unserialize($serialized);
|
|
|
200 |
|
|
|
201 |
$this->assertEquals($route, $unserialized);
|
|
|
202 |
$this->assertNotSame($route, $unserialized);
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* Tests that the compiled version is also serialized to prevent the overhead
|
|
|
207 |
* of compiling it again after unserialize.
|
|
|
208 |
*/
|
|
|
209 |
public function testSerializeWhenCompiled()
|
|
|
210 |
{
|
|
|
211 |
$route = new Route('/prefix/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
|
|
|
212 |
$route->setHost('{locale}.example.net');
|
|
|
213 |
$route->compile();
|
|
|
214 |
|
|
|
215 |
$serialized = serialize($route);
|
|
|
216 |
$unserialized = unserialize($serialized);
|
|
|
217 |
|
|
|
218 |
$this->assertEquals($route, $unserialized);
|
|
|
219 |
$this->assertNotSame($route, $unserialized);
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Tests that the serialized representation of a route in one symfony version
|
|
|
224 |
* also works in later symfony versions, i.e. the unserialized route is in the
|
|
|
225 |
* same state as another, semantically equivalent, route.
|
|
|
226 |
*/
|
|
|
227 |
public function testSerializedRepresentationKeepsWorking()
|
|
|
228 |
{
|
|
|
229 |
$serialized = 'C:31:"Symfony\Component\Routing\Route":934:{a:8:{s:4:"path";s:13:"/prefix/{foo}";s:4:"host";s:20:"{locale}.example.net";s:8:"defaults";a:1:{s:3:"foo";s:7:"default";}s:12:"requirements";a:1:{s:3:"foo";s:3:"\d+";}s:7:"options";a:1:{s:14:"compiler_class";s:39:"Symfony\Component\Routing\RouteCompiler";}s:7:"schemes";a:0:{}s:7:"methods";a:0:{}s:8:"compiled";C:39:"Symfony\Component\Routing\CompiledRoute":569:{a:8:{s:4:"vars";a:2:{i:0;s:6:"locale";i:1;s:3:"foo";}s:11:"path_prefix";s:7:"/prefix";s:10:"path_regex";s:30:"#^/prefix(?:/(?P<foo>\d+))?$#s";s:11:"path_tokens";a:2:{i:0;a:4:{i:0;s:8:"variable";i:1;s:1:"/";i:2;s:3:"\d+";i:3;s:3:"foo";}i:1;a:2:{i:0;s:4:"text";i:1;s:7:"/prefix";}}s:9:"path_vars";a:1:{i:0;s:3:"foo";}s:10:"host_regex";s:39:"#^(?P<locale>[^\.]++)\.example\.net$#si";s:11:"host_tokens";a:2:{i:0;a:2:{i:0;s:4:"text";i:1;s:12:".example.net";}i:1;a:4:{i:0;s:8:"variable";i:1;s:0:"";i:2;s:7:"[^\.]++";i:3;s:6:"locale";}}s:9:"host_vars";a:1:{i:0;s:6:"locale";}}}}}';
|
|
|
230 |
$unserialized = unserialize($serialized);
|
|
|
231 |
|
|
|
232 |
$route = new Route('/prefix/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
|
|
|
233 |
$route->setHost('{locale}.example.net');
|
|
|
234 |
$route->compile();
|
|
|
235 |
|
|
|
236 |
$this->assertEquals($route, $unserialized);
|
|
|
237 |
$this->assertNotSame($route, $unserialized);
|
|
|
238 |
}
|
|
|
239 |
}
|