]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/routing/Symfony/Component/Routing/Tests/RouteTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / routing / Symfony / Component / Routing / Tests / RouteTest.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\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'));
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
31 $route = new Route('/', array(), array(), array(), '', 'Https', 'Post');
32 $this->assertEquals(array('https'), $route->getSchemes(), '__construct() takes a single scheme as its sixth argument');
33 $this->assertEquals(array('POST'), $route->getMethods(), '__construct() takes a single method as its seventh argument');
34 }
35
36 public function testPath()
37 {
38 $route = new Route('/{foo}');
39 $route->setPath('/{bar}');
40 $this->assertEquals('/{bar}', $route->getPath(), '->setPath() sets the path');
41 $route->setPath('');
42 $this->assertEquals('/', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
43 $route->setPath('bar');
44 $this->assertEquals('/bar', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
45 $this->assertEquals($route, $route->setPath(''), '->setPath() implements a fluent interface');
46 $route->setPath('//path');
47 $this->assertEquals('/path', $route->getPath(), '->setPath() does not allow two slahes "//" at the beginning of the path as it would be confused with a network path when generating the path from the route');
48 }
49
50 public function testOptions()
51 {
52 $route = new Route('/{foo}');
53 $route->setOptions(array('foo' => 'bar'));
54 $this->assertEquals(array_merge(array(
55 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
56 ), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
57 $this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
58
59 $route->setOptions(array('foo' => 'foo'));
60 $route->addOptions(array('bar' => 'bar'));
61 $this->assertEquals($route, $route->addOptions(array()), '->addOptions() implements a fluent interface');
62 $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $route->getOptions(), '->addDefaults() keep previous defaults');
63 }
64
65 public function testDefaults()
66 {
67 $route = new Route('/{foo}');
68 $route->setDefaults(array('foo' => 'bar'));
69 $this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
70 $this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
71
72 $route->setDefault('foo', 'bar');
73 $this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');
74
75 $route->setDefault('foo2', 'bar2');
76 $this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
77 $this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not setted');
78
79 $route->setDefault('_controller', $closure = function () { return 'Hello'; });
80 $this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
81
82 $route->setDefaults(array('foo' => 'foo'));
83 $route->addDefaults(array('bar' => 'bar'));
84 $this->assertEquals($route, $route->addDefaults(array()), '->addDefaults() implements a fluent interface');
85 $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $route->getDefaults(), '->addDefaults() keep previous defaults');
86 }
87
88 public function testRequirements()
89 {
90 $route = new Route('/{foo}');
91 $route->setRequirements(array('foo' => '\d+'));
92 $this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
93 $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
94 $this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
95 $route->setRequirements(array('foo' => '^\d+$'));
96 $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the path');
97 $this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
98
99 $route->setRequirements(array('foo' => '\d+'));
100 $route->addRequirements(array('bar' => '\d+'));
101 $this->assertEquals($route, $route->addRequirements(array()), '->addRequirements() implements a fluent interface');
102 $this->assertEquals(array('foo' => '\d+', 'bar' => '\d+'), $route->getRequirements(), '->addRequirement() keep previous requirements');
103 }
104
105 public function testRequirement()
106 {
107 $route = new Route('/{foo}');
108 $route->setRequirement('foo', '^\d+$');
109 $this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the path');
110 }
111
112 /**
113 * @dataProvider getInvalidRequirements
114 * @expectedException \InvalidArgumentException
115 */
116 public function testSetInvalidRequirement($req)
117 {
118 $route = new Route('/{foo}');
119 $route->setRequirement('foo', $req);
120 }
121
122 public function getInvalidRequirements()
123 {
124 return array(
125 array(''),
126 array(array()),
127 array('^$'),
128 array('^'),
129 array('$')
130 );
131 }
132
133 public function testHost()
134 {
135 $route = new Route('/');
136 $route->setHost('{locale}.example.net');
137 $this->assertEquals('{locale}.example.net', $route->getHost(), '->setHost() sets the host pattern');
138 }
139
140 public function testScheme()
141 {
142 $route = new Route('/');
143 $this->assertEquals(array(), $route->getSchemes(), 'schemes is initialized with array()');
144 $route->setSchemes('hTTp');
145 $this->assertEquals(array('http'), $route->getSchemes(), '->setSchemes() accepts a single scheme string and lowercases it');
146 $route->setSchemes(array('HttpS', 'hTTp'));
147 $this->assertEquals(array('https', 'http'), $route->getSchemes(), '->setSchemes() accepts an array of schemes and lowercases them');
148 }
149
150 public function testSchemeIsBC()
151 {
152 $route = new Route('/');
153 $route->setRequirement('_scheme', 'http|https');
154 $this->assertEquals('http|https', $route->getRequirement('_scheme'));
155 $this->assertEquals(array('http', 'https'), $route->getSchemes());
156 $route->setSchemes(array('hTTp'));
157 $this->assertEquals('http', $route->getRequirement('_scheme'));
158 $route->setSchemes(array());
159 $this->assertNull($route->getRequirement('_scheme'));
160 }
161
162 public function testMethod()
163 {
164 $route = new Route('/');
165 $this->assertEquals(array(), $route->getMethods(), 'methods is initialized with array()');
166 $route->setMethods('gEt');
167 $this->assertEquals(array('GET'), $route->getMethods(), '->setMethods() accepts a single method string and uppercases it');
168 $route->setMethods(array('gEt', 'PosT'));
169 $this->assertEquals(array('GET', 'POST'), $route->getMethods(), '->setMethods() accepts an array of methods and uppercases them');
170 }
171
172 public function testMethodIsBC()
173 {
174 $route = new Route('/');
175 $route->setRequirement('_method', 'GET|POST');
176 $this->assertEquals('GET|POST', $route->getRequirement('_method'));
177 $this->assertEquals(array('GET', 'POST'), $route->getMethods());
178 $route->setMethods(array('gEt'));
179 $this->assertEquals('GET', $route->getRequirement('_method'));
180 $route->setMethods(array());
181 $this->assertNull($route->getRequirement('_method'));
182 }
183
184 public function testCompile()
185 {
186 $route = new Route('/{foo}');
187 $this->assertInstanceOf('Symfony\Component\Routing\CompiledRoute', $compiled = $route->compile(), '->compile() returns a compiled route');
188 $this->assertSame($compiled, $route->compile(), '->compile() only compiled the route once if unchanged');
189 $route->setRequirement('foo', '.*');
190 $this->assertNotSame($compiled, $route->compile(), '->compile() recompiles if the route was modified');
191 }
192 }