RouterTest.php
8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* Slim - a micro PHP 5 framework
*
* @author Josh Lockhart <info@slimframework.com>
* @copyright 2011 Josh Lockhart
* @link http://www.slimframework.com
* @license http://www.slimframework.com/license
* @version 2.6.1
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
class RouterTest extends PHPUnit_Framework_TestCase
{
/**
* Constructor should initialize routes as empty array
*/
public function testConstruct()
{
$router = new \Slim\Router();
$this->assertAttributeEquals(array(), 'routes', $router);
}
/**
* Map should set and return instance of \Slim\Route
*/
public function testMap()
{
$router = new \Slim\Router();
$route = new \Slim\Route('/foo', function() {});
$router->map($route);
$this->assertAttributeContains($route, 'routes', $router);
}
/**
* Named route should be added and indexed by name
*/
public function testAddNamedRoute()
{
$router = new \Slim\Router();
$route = new \Slim\Route('/foo', function () {});
$router->addNamedRoute('foo', $route);
$property = new \ReflectionProperty($router, 'namedRoutes');
$property->setAccessible(true);
$rV = $property->getValue($router);
$this->assertSame($route, $rV['foo']);
}
/**
* Named route should have unique name
*/
public function testAddNamedRouteWithDuplicateKey()
{
$this->setExpectedException('RuntimeException');
$router = new \Slim\Router();
$route = new \Slim\Route('/foo', function () {});
$router->addNamedRoute('foo', $route);
$router->addNamedRoute('foo', $route);
}
/**
* Router should return named route by name, or null if not found
*/
public function testGetNamedRoute()
{
$router = new \Slim\Router();
$route = new \Slim\Route('/foo', function () {});
$property = new \ReflectionProperty($router, 'namedRoutes');
$property->setAccessible(true);
$property->setValue($router, array('foo' => $route));
$this->assertSame($route, $router->getNamedRoute('foo'));
$this->assertNull($router->getNamedRoute('bar'));
}
/**
* Router should determine named routes and cache results
*/
public function testGetNamedRoutes()
{
$router = new \Slim\Router();
$route1 = new \Slim\Route('/foo', function () {});
$route2 = new \Slim\Route('/bar', function () {});
// Init router routes to array
$propertyRouterRoutes = new \ReflectionProperty($router, 'routes');
$propertyRouterRoutes->setAccessible(true);
$propertyRouterRoutes->setValue($router, array($route1, $route2));
// Init router named routes to null
$propertyRouterNamedRoutes = new \ReflectionProperty($router, 'namedRoutes');
$propertyRouterNamedRoutes->setAccessible(true);
$propertyRouterNamedRoutes->setValue($router, null);
// Init route name
$propertyRouteName = new \ReflectionProperty($route2, 'name');
$propertyRouteName->setAccessible(true);
$propertyRouteName->setValue($route2, 'bar');
$namedRoutes = $router->getNamedRoutes();
$this->assertCount(1, $namedRoutes);
$this->assertSame($route2, $namedRoutes['bar']);
}
/**
* Router should detect presence of a named route by name
*/
public function testHasNamedRoute()
{
$router = new \Slim\Router();
$route = new \Slim\Route('/foo', function () {});
$property = new \ReflectionProperty($router, 'namedRoutes');
$property->setAccessible(true);
$property->setValue($router, array('foo' => $route));
$this->assertTrue($router->hasNamedRoute('foo'));
$this->assertFalse($router->hasNamedRoute('bar'));
}
/**
* Router should return current route if set during iteration
*/
public function testGetCurrentRoute()
{
$router = new \Slim\Router();
$route = new \Slim\Route('/foo', function () {});
$property = new \ReflectionProperty($router, 'currentRoute');
$property->setAccessible(true);
$property->setValue($router, $route);
$this->assertSame($route, $router->getCurrentRoute());
}
/**
* Router should return first matching route if current route not set yet by iteration
*/
public function testGetCurrentRouteIfMatchedRoutes()
{
$router = new \Slim\Router();
$route = new \Slim\Route('/foo', function () {});
$propertyMatchedRoutes = new \ReflectionProperty($router, 'matchedRoutes');
$propertyMatchedRoutes->setAccessible(true);
$propertyMatchedRoutes->setValue($router, array($route));
$propertyCurrentRoute = new \ReflectionProperty($router, 'currentRoute');
$propertyCurrentRoute->setAccessible(true);
$propertyCurrentRoute->setValue($router, null);
$this->assertSame($route, $router->getCurrentRoute());
}
/**
* Router should return `null` if current route not set yet and there are no matching routes
*/
public function testGetCurrentRouteIfNoMatchedRoutes()
{
$router = new \Slim\Router();
$propertyMatchedRoutes = new \ReflectionProperty($router, 'matchedRoutes');
$propertyMatchedRoutes->setAccessible(true);
$propertyMatchedRoutes->setValue($router, array());
$propertyCurrentRoute = new \ReflectionProperty($router, 'currentRoute');
$propertyCurrentRoute->setAccessible(true);
$propertyCurrentRoute->setValue($router, null);
$this->assertNull($router->getCurrentRoute());
}
public function testGetMatchedRoutes()
{
$router = new \Slim\Router();
$route1 = new \Slim\Route('/foo', function () {});
$route1 = $route1->via('GET');
$route2 = new \Slim\Route('/foo', function () {});
$route2 = $route2->via('POST');
$route3 = new \Slim\Route('/bar', function () {});
$route3 = $route3->via('PUT');
$routes = new \ReflectionProperty($router, 'routes');
$routes->setAccessible(true);
$routes->setValue($router, array($route1, $route2, $route3));
$matchedRoutes = $router->getMatchedRoutes('GET', '/foo');
$this->assertSame($route1, $matchedRoutes[0]);
}
// Test url for named route
public function testUrlFor()
{
$router = new \Slim\Router();
$route1 = new \Slim\Route('/hello/:first/:last', function () {});
$route1 = $route1->via('GET')->name('hello');
$route2 = new \Slim\Route('/path/(:foo\.:bar)', function () {});
$route2 = $route2->via('GET')->name('regexRoute');
$routes = new \ReflectionProperty($router, 'namedRoutes');
$routes->setAccessible(true);
$routes->setValue($router, array(
'hello' => $route1,
'regexRoute' => $route2
));
$this->assertEquals('/hello/Josh/Lockhart', $router->urlFor('hello', array('first' => 'Josh', 'last' => 'Lockhart')));
$this->assertEquals('/path/Hello.Josh', $router->urlFor('regexRoute', array('foo' => 'Hello', 'bar' => 'Josh')));
}
public function testUrlForIfNoSuchRoute()
{
$this->setExpectedException('RuntimeException');
$router = new \Slim\Router();
$router->urlFor('foo', array('abc' => '123'));
}
}