@@ -10,51 +10,51 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | class RouterDebug extends Router |
| 12 | 12 | { |
| 13 | - public function getNamedRoutes() |
|
| 14 | - { |
|
| 15 | - return $this->namedRoutes; |
|
| 16 | - } |
|
| 13 | + public function getNamedRoutes() |
|
| 14 | + { |
|
| 15 | + return $this->namedRoutes; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - public function getBasePath() |
|
| 19 | - { |
|
| 20 | - return $this->basePath; |
|
| 21 | - } |
|
| 18 | + public function getBasePath() |
|
| 19 | + { |
|
| 20 | + return $this->basePath; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - public function setServer($server) |
|
| 24 | - { |
|
| 25 | - $this->server = $server; |
|
| 26 | - } |
|
| 23 | + public function setServer($server) |
|
| 24 | + { |
|
| 25 | + $this->server = $server; |
|
| 26 | + } |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | class SimpleTraversable implements Iterator |
| 30 | 30 | { |
| 31 | - protected $_position = 0; |
|
| 31 | + protected $_position = 0; |
|
| 32 | 32 | |
| 33 | - protected $_data = array( |
|
| 34 | - array('GET', '/foo', 'foo_action', null), |
|
| 35 | - array('POST', '/bar', 'bar_action', 'second_route') |
|
| 36 | - ); |
|
| 33 | + protected $_data = array( |
|
| 34 | + array('GET', '/foo', 'foo_action', null), |
|
| 35 | + array('POST', '/bar', 'bar_action', 'second_route') |
|
| 36 | + ); |
|
| 37 | 37 | |
| 38 | - public function current() |
|
| 39 | - { |
|
| 40 | - return $this->_data[$this->_position]; |
|
| 41 | - } |
|
| 42 | - public function key() |
|
| 43 | - { |
|
| 44 | - return $this->_position; |
|
| 45 | - } |
|
| 46 | - public function next() |
|
| 47 | - { |
|
| 48 | - ++$this->_position; |
|
| 49 | - } |
|
| 50 | - public function rewind() |
|
| 51 | - { |
|
| 52 | - $this->_position = 0; |
|
| 53 | - } |
|
| 54 | - public function valid() |
|
| 55 | - { |
|
| 56 | - return isset($this->_data[$this->_position]); |
|
| 57 | - } |
|
| 38 | + public function current() |
|
| 39 | + { |
|
| 40 | + return $this->_data[$this->_position]; |
|
| 41 | + } |
|
| 42 | + public function key() |
|
| 43 | + { |
|
| 44 | + return $this->_position; |
|
| 45 | + } |
|
| 46 | + public function next() |
|
| 47 | + { |
|
| 48 | + ++$this->_position; |
|
| 49 | + } |
|
| 50 | + public function rewind() |
|
| 51 | + { |
|
| 52 | + $this->_position = 0; |
|
| 53 | + } |
|
| 54 | + public function valid() |
|
| 55 | + { |
|
| 56 | + return isset($this->_data[$this->_position]); |
|
| 57 | + } |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /** |
@@ -62,399 +62,399 @@ discard block |
||
| 62 | 62 | */ |
| 63 | 63 | class AltoRouterTest extends PHPUnit_Framework_TestCase |
| 64 | 64 | { |
| 65 | - /** |
|
| 66 | - * @var RouterDebug |
|
| 67 | - */ |
|
| 68 | - protected $router; |
|
| 65 | + /** |
|
| 66 | + * @var RouterDebug |
|
| 67 | + */ |
|
| 68 | + protected $router; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Sets up the fixture, for example, opens a network connection. |
|
| 72 | - * This method is called before a test is executed. |
|
| 73 | - */ |
|
| 74 | - protected function setUp() |
|
| 75 | - { |
|
| 76 | - $parser = new RouterParser(); |
|
| 77 | - $this->router = new RouterDebug($parser, [], '', $_SERVER); |
|
| 78 | - } |
|
| 70 | + /** |
|
| 71 | + * Sets up the fixture, for example, opens a network connection. |
|
| 72 | + * This method is called before a test is executed. |
|
| 73 | + */ |
|
| 74 | + protected function setUp() |
|
| 75 | + { |
|
| 76 | + $parser = new RouterParser(); |
|
| 77 | + $this->router = new RouterDebug($parser, [], '', $_SERVER); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @covers Router::getRoutes |
|
| 82 | - */ |
|
| 83 | - public function testGetRoutes() |
|
| 84 | - { |
|
| 85 | - $method = 'POST'; |
|
| 86 | - $route = '/[:controller]/[:action]'; |
|
| 87 | - $target = function () { |
|
| 88 | - }; |
|
| 80 | + /** |
|
| 81 | + * @covers Router::getRoutes |
|
| 82 | + */ |
|
| 83 | + public function testGetRoutes() |
|
| 84 | + { |
|
| 85 | + $method = 'POST'; |
|
| 86 | + $route = '/[:controller]/[:action]'; |
|
| 87 | + $target = function () { |
|
| 88 | + }; |
|
| 89 | 89 | |
| 90 | - $this->assertInternalType('array', $this->router->getRoutes()); |
|
| 91 | - $this->router->map($method, $route, $target); |
|
| 92 | - $this->assertEquals(array(array($method, $route, $target, null)), $this->router->getRoutes()); |
|
| 93 | - } |
|
| 90 | + $this->assertInternalType('array', $this->router->getRoutes()); |
|
| 91 | + $this->router->map($method, $route, $target); |
|
| 92 | + $this->assertEquals(array(array($method, $route, $target, null)), $this->router->getRoutes()); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * @covers Router::setRoutes |
|
| 97 | - */ |
|
| 98 | - public function testAddRoutes() |
|
| 99 | - { |
|
| 100 | - $method = 'POST'; |
|
| 101 | - $route = '/[:controller]/[:action]'; |
|
| 102 | - $target = function () { |
|
| 103 | - }; |
|
| 104 | - |
|
| 105 | - $this->router->setRoutes(array( |
|
| 106 | - array($method, $route, $target), |
|
| 107 | - array($method, $route, $target, 'second_route') |
|
| 108 | - )); |
|
| 109 | - |
|
| 110 | - $routes = $this->router->getRoutes(); |
|
| 111 | - |
|
| 112 | - $this->assertEquals(array($method, $route, $target, null), $routes[0]); |
|
| 113 | - $this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]); |
|
| 114 | - } |
|
| 95 | + /** |
|
| 96 | + * @covers Router::setRoutes |
|
| 97 | + */ |
|
| 98 | + public function testAddRoutes() |
|
| 99 | + { |
|
| 100 | + $method = 'POST'; |
|
| 101 | + $route = '/[:controller]/[:action]'; |
|
| 102 | + $target = function () { |
|
| 103 | + }; |
|
| 104 | + |
|
| 105 | + $this->router->setRoutes(array( |
|
| 106 | + array($method, $route, $target), |
|
| 107 | + array($method, $route, $target, 'second_route') |
|
| 108 | + )); |
|
| 109 | + |
|
| 110 | + $routes = $this->router->getRoutes(); |
|
| 111 | + |
|
| 112 | + $this->assertEquals(array($method, $route, $target, null), $routes[0]); |
|
| 113 | + $this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * @covers Router::setRoutes |
|
| 118 | - */ |
|
| 119 | - public function testAddRoutesAcceptsTraverable() |
|
| 120 | - { |
|
| 121 | - $traversable = new SimpleTraversable(); |
|
| 122 | - $this->router->setRoutes($traversable); |
|
| 116 | + /** |
|
| 117 | + * @covers Router::setRoutes |
|
| 118 | + */ |
|
| 119 | + public function testAddRoutesAcceptsTraverable() |
|
| 120 | + { |
|
| 121 | + $traversable = new SimpleTraversable(); |
|
| 122 | + $this->router->setRoutes($traversable); |
|
| 123 | 123 | |
| 124 | - $traversable->rewind(); |
|
| 124 | + $traversable->rewind(); |
|
| 125 | 125 | |
| 126 | - $first = $traversable->current(); |
|
| 127 | - $traversable->next(); |
|
| 128 | - $second = $traversable->current(); |
|
| 126 | + $first = $traversable->current(); |
|
| 127 | + $traversable->next(); |
|
| 128 | + $second = $traversable->current(); |
|
| 129 | 129 | |
| 130 | - $routes = $this->router->getRoutes(); |
|
| 130 | + $routes = $this->router->getRoutes(); |
|
| 131 | 131 | |
| 132 | - $this->assertEquals($first, $routes[0]); |
|
| 133 | - $this->assertEquals($second, $routes[1]); |
|
| 134 | - } |
|
| 132 | + $this->assertEquals($first, $routes[0]); |
|
| 133 | + $this->assertEquals($second, $routes[1]); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * @covers Router::setRoutes |
|
| 138 | - * @expectedException Exception |
|
| 139 | - */ |
|
| 140 | - public function testAddRoutesThrowsExceptionOnInvalidArgument() |
|
| 141 | - { |
|
| 142 | - $this->router->setRoutes(new stdClass); |
|
| 143 | - } |
|
| 136 | + /** |
|
| 137 | + * @covers Router::setRoutes |
|
| 138 | + * @expectedException Exception |
|
| 139 | + */ |
|
| 140 | + public function testAddRoutesThrowsExceptionOnInvalidArgument() |
|
| 141 | + { |
|
| 142 | + $this->router->setRoutes(new stdClass); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * @covers AltoRouter::setBasePath |
|
| 147 | - */ |
|
| 148 | - public function testSetBasePath() |
|
| 149 | - { |
|
| 150 | - $this->router->setBasePath('/some/path'); |
|
| 151 | - $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
| 152 | - |
|
| 153 | - $this->router->setBasePath('/some/path'); |
|
| 154 | - $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
| 155 | - } |
|
| 145 | + /** |
|
| 146 | + * @covers AltoRouter::setBasePath |
|
| 147 | + */ |
|
| 148 | + public function testSetBasePath() |
|
| 149 | + { |
|
| 150 | + $this->router->setBasePath('/some/path'); |
|
| 151 | + $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
| 152 | + |
|
| 153 | + $this->router->setBasePath('/some/path'); |
|
| 154 | + $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - /** |
|
| 158 | - * @covers Router::map |
|
| 159 | - */ |
|
| 160 | - public function testMap() |
|
| 161 | - { |
|
| 162 | - $method = 'POST'; |
|
| 163 | - $route = '/[:controller]/[:action]'; |
|
| 164 | - $target = function () { |
|
| 165 | - }; |
|
| 166 | - |
|
| 167 | - $this->router->map($method, $route, $target); |
|
| 168 | - |
|
| 169 | - $routes = $this->router->getRoutes(); |
|
| 170 | - |
|
| 171 | - $this->assertEquals(array($method, $route, $target, null), $routes[0]); |
|
| 172 | - } |
|
| 157 | + /** |
|
| 158 | + * @covers Router::map |
|
| 159 | + */ |
|
| 160 | + public function testMap() |
|
| 161 | + { |
|
| 162 | + $method = 'POST'; |
|
| 163 | + $route = '/[:controller]/[:action]'; |
|
| 164 | + $target = function () { |
|
| 165 | + }; |
|
| 166 | + |
|
| 167 | + $this->router->map($method, $route, $target); |
|
| 168 | + |
|
| 169 | + $routes = $this->router->getRoutes(); |
|
| 170 | + |
|
| 171 | + $this->assertEquals(array($method, $route, $target, null), $routes[0]); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * @covers Router::map |
|
| 176 | - */ |
|
| 177 | - public function testMapWithName() |
|
| 178 | - { |
|
| 179 | - $method = 'POST'; |
|
| 180 | - $route = '/[:controller]/[:action]'; |
|
| 181 | - $target = function () { |
|
| 182 | - }; |
|
| 183 | - $name = 'myroute'; |
|
| 184 | - |
|
| 185 | - $this->router->map($method, $route, $target, $name); |
|
| 186 | - |
|
| 187 | - $routes = $this->router->getRoutes(); |
|
| 188 | - $this->assertEquals(array($method, $route, $target, $name), $routes[0]); |
|
| 189 | - |
|
| 190 | - $named_routes = $this->router->getNamedRoutes(); |
|
| 191 | - $this->assertEquals($route, $named_routes[$name]); |
|
| 192 | - |
|
| 193 | - try { |
|
| 194 | - $this->router->map($method, $route, $target, $name); |
|
| 195 | - $this->fail('Should not be able to add existing named route'); |
|
| 196 | - } catch (Exception $e) { |
|
| 197 | - $this->assertEquals("Can not redeclare route '{$name}'", $e->getMessage()); |
|
| 198 | - } |
|
| 199 | - } |
|
| 174 | + /** |
|
| 175 | + * @covers Router::map |
|
| 176 | + */ |
|
| 177 | + public function testMapWithName() |
|
| 178 | + { |
|
| 179 | + $method = 'POST'; |
|
| 180 | + $route = '/[:controller]/[:action]'; |
|
| 181 | + $target = function () { |
|
| 182 | + }; |
|
| 183 | + $name = 'myroute'; |
|
| 184 | + |
|
| 185 | + $this->router->map($method, $route, $target, $name); |
|
| 186 | + |
|
| 187 | + $routes = $this->router->getRoutes(); |
|
| 188 | + $this->assertEquals(array($method, $route, $target, $name), $routes[0]); |
|
| 189 | + |
|
| 190 | + $named_routes = $this->router->getNamedRoutes(); |
|
| 191 | + $this->assertEquals($route, $named_routes[$name]); |
|
| 192 | + |
|
| 193 | + try { |
|
| 194 | + $this->router->map($method, $route, $target, $name); |
|
| 195 | + $this->fail('Should not be able to add existing named route'); |
|
| 196 | + } catch (Exception $e) { |
|
| 197 | + $this->assertEquals("Can not redeclare route '{$name}'", $e->getMessage()); |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * @covers Router::generate |
|
| 204 | - */ |
|
| 205 | - public function testGenerate() |
|
| 206 | - { |
|
| 207 | - $params = array( |
|
| 208 | - 'controller' => 'test', |
|
| 209 | - 'action' => 'someaction' |
|
| 210 | - ); |
|
| 202 | + /** |
|
| 203 | + * @covers Router::generate |
|
| 204 | + */ |
|
| 205 | + public function testGenerate() |
|
| 206 | + { |
|
| 207 | + $params = array( |
|
| 208 | + 'controller' => 'test', |
|
| 209 | + 'action' => 'someaction' |
|
| 210 | + ); |
|
| 211 | 211 | |
| 212 | - $this->router->map('GET', '/[:controller]/[:action]', function () { |
|
| 213 | - }, 'foo_route'); |
|
| 212 | + $this->router->map('GET', '/[:controller]/[:action]', function () { |
|
| 213 | + }, 'foo_route'); |
|
| 214 | 214 | |
| 215 | - $this->assertEquals( |
|
| 215 | + $this->assertEquals( |
|
| 216 | 216 | |
| 217 | - '/test/someaction', |
|
| 218 | - $this->router->generate('foo_route', $params) |
|
| 217 | + '/test/someaction', |
|
| 218 | + $this->router->generate('foo_route', $params) |
|
| 219 | 219 | |
| 220 | - ); |
|
| 220 | + ); |
|
| 221 | 221 | |
| 222 | - $params = array( |
|
| 223 | - 'controller' => 'test', |
|
| 224 | - 'action' => 'someaction', |
|
| 225 | - 'type' => 'json' |
|
| 226 | - ); |
|
| 222 | + $params = array( |
|
| 223 | + 'controller' => 'test', |
|
| 224 | + 'action' => 'someaction', |
|
| 225 | + 'type' => 'json' |
|
| 226 | + ); |
|
| 227 | 227 | |
| 228 | - $this->assertEquals( |
|
| 228 | + $this->assertEquals( |
|
| 229 | 229 | |
| 230 | - '/test/someaction', |
|
| 231 | - $this->router->generate('foo_route', $params) |
|
| 230 | + '/test/someaction', |
|
| 231 | + $this->router->generate('foo_route', $params) |
|
| 232 | 232 | |
| 233 | - ); |
|
| 234 | - } |
|
| 233 | + ); |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - public function testGenerateWithOptionalUrlParts() |
|
| 237 | - { |
|
| 238 | - $this->router->map('GET', '/[:controller]/[:action].[:type]?', function () { |
|
| 239 | - }, 'bar_route'); |
|
| 236 | + public function testGenerateWithOptionalUrlParts() |
|
| 237 | + { |
|
| 238 | + $this->router->map('GET', '/[:controller]/[:action].[:type]?', function () { |
|
| 239 | + }, 'bar_route'); |
|
| 240 | 240 | |
| 241 | - $params = array( |
|
| 242 | - 'controller' => 'test', |
|
| 243 | - 'action' => 'someaction' |
|
| 244 | - ); |
|
| 241 | + $params = array( |
|
| 242 | + 'controller' => 'test', |
|
| 243 | + 'action' => 'someaction' |
|
| 244 | + ); |
|
| 245 | 245 | |
| 246 | - $this->assertEquals( |
|
| 246 | + $this->assertEquals( |
|
| 247 | 247 | |
| 248 | - '/test/someaction', |
|
| 249 | - $this->router->generate('bar_route', $params) |
|
| 248 | + '/test/someaction', |
|
| 249 | + $this->router->generate('bar_route', $params) |
|
| 250 | 250 | |
| 251 | - ); |
|
| 251 | + ); |
|
| 252 | 252 | |
| 253 | - $params = array( |
|
| 254 | - 'controller' => 'test', |
|
| 255 | - 'action' => 'someaction', |
|
| 256 | - 'type' => 'json' |
|
| 257 | - ); |
|
| 253 | + $params = array( |
|
| 254 | + 'controller' => 'test', |
|
| 255 | + 'action' => 'someaction', |
|
| 256 | + 'type' => 'json' |
|
| 257 | + ); |
|
| 258 | 258 | |
| 259 | - $this->assertEquals( |
|
| 259 | + $this->assertEquals( |
|
| 260 | 260 | |
| 261 | - '/test/someaction.json', |
|
| 262 | - $this->router->generate('bar_route', $params) |
|
| 261 | + '/test/someaction.json', |
|
| 262 | + $this->router->generate('bar_route', $params) |
|
| 263 | 263 | |
| 264 | - ); |
|
| 265 | - } |
|
| 264 | + ); |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - public function testGenerateWithNonexistingRoute() |
|
| 268 | - { |
|
| 269 | - try { |
|
| 270 | - $this->router->generate('nonexisting_route'); |
|
| 271 | - $this->fail('Should trigger an exception on nonexisting named route'); |
|
| 272 | - } catch (Exception $e) { |
|
| 273 | - $this->assertEquals("Route 'nonexisting_route' does not exist.", $e->getMessage()); |
|
| 274 | - } |
|
| 275 | - } |
|
| 267 | + public function testGenerateWithNonexistingRoute() |
|
| 268 | + { |
|
| 269 | + try { |
|
| 270 | + $this->router->generate('nonexisting_route'); |
|
| 271 | + $this->fail('Should trigger an exception on nonexisting named route'); |
|
| 272 | + } catch (Exception $e) { |
|
| 273 | + $this->assertEquals("Route 'nonexisting_route' does not exist.", $e->getMessage()); |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | 276 | |
| 277 | - /** |
|
| 278 | - * @covers Router::match |
|
| 279 | - * @covers Router::compileRoute |
|
| 280 | - */ |
|
| 281 | - public function testMatch() |
|
| 282 | - { |
|
| 283 | - $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
| 284 | - |
|
| 285 | - $this->assertEquals(array( |
|
| 286 | - 'target' => 'foo_action', |
|
| 287 | - 'params' => array( |
|
| 288 | - 'controller' => 'test', |
|
| 289 | - 'action' => 'do' |
|
| 290 | - ), |
|
| 291 | - 'name' => 'foo_route' |
|
| 292 | - ), $this->router->match('/foo/test/do', 'GET')); |
|
| 293 | - |
|
| 294 | - $this->assertFalse($this->router->match('/foo/test/do', 'POST')); |
|
| 295 | - |
|
| 296 | - $this->assertEquals(array( |
|
| 297 | - 'target' => 'foo_action', |
|
| 298 | - 'params' => array( |
|
| 299 | - 'controller' => 'test', |
|
| 300 | - 'action' => 'do' |
|
| 301 | - ), |
|
| 302 | - 'name' => 'foo_route' |
|
| 303 | - ), $this->router->match('/foo/test/do?param=value', 'GET')); |
|
| 304 | - } |
|
| 277 | + /** |
|
| 278 | + * @covers Router::match |
|
| 279 | + * @covers Router::compileRoute |
|
| 280 | + */ |
|
| 281 | + public function testMatch() |
|
| 282 | + { |
|
| 283 | + $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
| 284 | + |
|
| 285 | + $this->assertEquals(array( |
|
| 286 | + 'target' => 'foo_action', |
|
| 287 | + 'params' => array( |
|
| 288 | + 'controller' => 'test', |
|
| 289 | + 'action' => 'do' |
|
| 290 | + ), |
|
| 291 | + 'name' => 'foo_route' |
|
| 292 | + ), $this->router->match('/foo/test/do', 'GET')); |
|
| 293 | + |
|
| 294 | + $this->assertFalse($this->router->match('/foo/test/do', 'POST')); |
|
| 295 | + |
|
| 296 | + $this->assertEquals(array( |
|
| 297 | + 'target' => 'foo_action', |
|
| 298 | + 'params' => array( |
|
| 299 | + 'controller' => 'test', |
|
| 300 | + 'action' => 'do' |
|
| 301 | + ), |
|
| 302 | + 'name' => 'foo_route' |
|
| 303 | + ), $this->router->match('/foo/test/do?param=value', 'GET')); |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - public function testMatchWithFixedParamValues() |
|
| 307 | - { |
|
| 308 | - $this->router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); |
|
| 309 | - |
|
| 310 | - $this->assertEquals(array( |
|
| 311 | - 'target' => 'usersController#doAction', |
|
| 312 | - 'params' => array( |
|
| 313 | - 'id' => 1, |
|
| 314 | - 'action' => 'delete' |
|
| 315 | - ), |
|
| 316 | - 'name' => 'users_do' |
|
| 317 | - ), $this->router->match('/users/1/delete', 'POST')); |
|
| 318 | - |
|
| 319 | - $this->assertFalse($this->router->match('/users/1/delete', 'GET')); |
|
| 320 | - $this->assertFalse($this->router->match('/users/abc/delete', 'POST')); |
|
| 321 | - $this->assertFalse($this->router->match('/users/1/create', 'GET')); |
|
| 322 | - } |
|
| 306 | + public function testMatchWithFixedParamValues() |
|
| 307 | + { |
|
| 308 | + $this->router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); |
|
| 309 | + |
|
| 310 | + $this->assertEquals(array( |
|
| 311 | + 'target' => 'usersController#doAction', |
|
| 312 | + 'params' => array( |
|
| 313 | + 'id' => 1, |
|
| 314 | + 'action' => 'delete' |
|
| 315 | + ), |
|
| 316 | + 'name' => 'users_do' |
|
| 317 | + ), $this->router->match('/users/1/delete', 'POST')); |
|
| 318 | + |
|
| 319 | + $this->assertFalse($this->router->match('/users/1/delete', 'GET')); |
|
| 320 | + $this->assertFalse($this->router->match('/users/abc/delete', 'POST')); |
|
| 321 | + $this->assertFalse($this->router->match('/users/1/create', 'GET')); |
|
| 322 | + } |
|
| 323 | 323 | |
| 324 | - public function testMatchWithServerVars() |
|
| 325 | - { |
|
| 326 | - $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
| 324 | + public function testMatchWithServerVars() |
|
| 325 | + { |
|
| 326 | + $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
| 327 | 327 | |
| 328 | - $_SERVER['REQUEST_URI'] = '/foo/test/do'; |
|
| 329 | - $_SERVER['REQUEST_METHOD'] = 'GET'; |
|
| 328 | + $_SERVER['REQUEST_URI'] = '/foo/test/do'; |
|
| 329 | + $_SERVER['REQUEST_METHOD'] = 'GET'; |
|
| 330 | 330 | |
| 331 | - $this->router->setServer($_SERVER); |
|
| 332 | - |
|
| 333 | - $this->assertEquals(array( |
|
| 334 | - 'target' => 'foo_action', |
|
| 335 | - 'params' => array( |
|
| 336 | - 'controller' => 'test', |
|
| 337 | - 'action' => 'do' |
|
| 338 | - ), |
|
| 339 | - 'name' => 'foo_route' |
|
| 340 | - ), $this->router->match()); |
|
| 341 | - } |
|
| 331 | + $this->router->setServer($_SERVER); |
|
| 332 | + |
|
| 333 | + $this->assertEquals(array( |
|
| 334 | + 'target' => 'foo_action', |
|
| 335 | + 'params' => array( |
|
| 336 | + 'controller' => 'test', |
|
| 337 | + 'action' => 'do' |
|
| 338 | + ), |
|
| 339 | + 'name' => 'foo_route' |
|
| 340 | + ), $this->router->match()); |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | - public function testMatchWithOptionalUrlParts() |
|
| 344 | - { |
|
| 345 | - $this->router->map('GET', '/bar/[:controller]/[:action].[:type]?', 'bar_action', 'bar_route'); |
|
| 346 | - |
|
| 347 | - $this->assertEquals(array( |
|
| 348 | - 'target' => 'bar_action', |
|
| 349 | - 'params' => array( |
|
| 350 | - 'controller' => 'test', |
|
| 351 | - 'action' => 'do', |
|
| 352 | - 'type' => 'json' |
|
| 353 | - ), |
|
| 354 | - 'name' => 'bar_route' |
|
| 355 | - ), $this->router->match('/bar/test/do.json', 'GET')); |
|
| 356 | - } |
|
| 343 | + public function testMatchWithOptionalUrlParts() |
|
| 344 | + { |
|
| 345 | + $this->router->map('GET', '/bar/[:controller]/[:action].[:type]?', 'bar_action', 'bar_route'); |
|
| 346 | + |
|
| 347 | + $this->assertEquals(array( |
|
| 348 | + 'target' => 'bar_action', |
|
| 349 | + 'params' => array( |
|
| 350 | + 'controller' => 'test', |
|
| 351 | + 'action' => 'do', |
|
| 352 | + 'type' => 'json' |
|
| 353 | + ), |
|
| 354 | + 'name' => 'bar_route' |
|
| 355 | + ), $this->router->match('/bar/test/do.json', 'GET')); |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - public function testMatchWithWildcard() |
|
| 359 | - { |
|
| 360 | - $this->router->map('GET', '/a', 'foo_action', 'foo_route'); |
|
| 361 | - $this->router->map('GET', '*', 'bar_action', 'bar_route'); |
|
| 362 | - |
|
| 363 | - $this->assertEquals(array( |
|
| 364 | - 'target' => 'bar_action', |
|
| 365 | - 'params' => array(), |
|
| 366 | - 'name' => 'bar_route' |
|
| 367 | - ), $this->router->match('/everything', 'GET')); |
|
| 368 | - } |
|
| 358 | + public function testMatchWithWildcard() |
|
| 359 | + { |
|
| 360 | + $this->router->map('GET', '/a', 'foo_action', 'foo_route'); |
|
| 361 | + $this->router->map('GET', '*', 'bar_action', 'bar_route'); |
|
| 362 | + |
|
| 363 | + $this->assertEquals(array( |
|
| 364 | + 'target' => 'bar_action', |
|
| 365 | + 'params' => array(), |
|
| 366 | + 'name' => 'bar_route' |
|
| 367 | + ), $this->router->match('/everything', 'GET')); |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | - public function testMatchWithCustomRegexp() |
|
| 371 | - { |
|
| 372 | - $this->router->map('GET', '@^/[a-z]*$', 'bar_action', 'bar_route'); |
|
| 373 | - |
|
| 374 | - $this->assertEquals(array( |
|
| 375 | - 'target' => 'bar_action', |
|
| 376 | - 'params' => array(), |
|
| 377 | - 'name' => 'bar_route' |
|
| 378 | - ), $this->router->match('/everything', 'GET')); |
|
| 379 | - |
|
| 380 | - $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
| 381 | - } |
|
| 370 | + public function testMatchWithCustomRegexp() |
|
| 371 | + { |
|
| 372 | + $this->router->map('GET', '@^/[a-z]*$', 'bar_action', 'bar_route'); |
|
| 373 | + |
|
| 374 | + $this->assertEquals(array( |
|
| 375 | + 'target' => 'bar_action', |
|
| 376 | + 'params' => array(), |
|
| 377 | + 'name' => 'bar_route' |
|
| 378 | + ), $this->router->match('/everything', 'GET')); |
|
| 379 | + |
|
| 380 | + $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | - public function testMatchWithUnicodeRegex() |
|
| 384 | - { |
|
| 385 | - $pattern = '/(?<path>[^'; |
|
| 386 | - // Arabic characters |
|
| 387 | - $pattern .= '\x{0600}-\x{06FF}'; |
|
| 388 | - $pattern .= '\x{FB50}-\x{FDFD}'; |
|
| 389 | - $pattern .= '\x{FE70}-\x{FEFF}'; |
|
| 390 | - $pattern .= '\x{0750}-\x{077F}'; |
|
| 391 | - // Alphanumeric, /, _, - and space characters |
|
| 392 | - $pattern .= 'a-zA-Z0-9\/_\-\s'; |
|
| 393 | - // 'ZERO WIDTH NON-JOINER' |
|
| 394 | - $pattern .= '\x{200C}'; |
|
| 395 | - $pattern .= ']+)'; |
|
| 396 | - |
|
| 397 | - $this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route'); |
|
| 398 | - |
|
| 399 | - $this->assertEquals(array( |
|
| 400 | - 'target' => 'unicode_action', |
|
| 401 | - 'name' => 'unicode_route', |
|
| 402 | - 'params' => array( |
|
| 403 | - 'path' => '大家好' |
|
| 404 | - ) |
|
| 405 | - ), $this->router->match('/大家好', 'GET')); |
|
| 406 | - |
|
| 407 | - $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
| 408 | - } |
|
| 383 | + public function testMatchWithUnicodeRegex() |
|
| 384 | + { |
|
| 385 | + $pattern = '/(?<path>[^'; |
|
| 386 | + // Arabic characters |
|
| 387 | + $pattern .= '\x{0600}-\x{06FF}'; |
|
| 388 | + $pattern .= '\x{FB50}-\x{FDFD}'; |
|
| 389 | + $pattern .= '\x{FE70}-\x{FEFF}'; |
|
| 390 | + $pattern .= '\x{0750}-\x{077F}'; |
|
| 391 | + // Alphanumeric, /, _, - and space characters |
|
| 392 | + $pattern .= 'a-zA-Z0-9\/_\-\s'; |
|
| 393 | + // 'ZERO WIDTH NON-JOINER' |
|
| 394 | + $pattern .= '\x{200C}'; |
|
| 395 | + $pattern .= ']+)'; |
|
| 396 | + |
|
| 397 | + $this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route'); |
|
| 398 | + |
|
| 399 | + $this->assertEquals(array( |
|
| 400 | + 'target' => 'unicode_action', |
|
| 401 | + 'name' => 'unicode_route', |
|
| 402 | + 'params' => array( |
|
| 403 | + 'path' => '大家好' |
|
| 404 | + ) |
|
| 405 | + ), $this->router->match('/大家好', 'GET')); |
|
| 406 | + |
|
| 407 | + $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
| 408 | + } |
|
| 409 | 409 | |
| 410 | - /** |
|
| 411 | - * @covers Router::setMatchTypes |
|
| 412 | - */ |
|
| 413 | - public function testMatchWithCustomNamedRegex() |
|
| 414 | - { |
|
| 415 | - $this->router->getParser()->setMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?')); |
|
| 416 | - $this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route'); |
|
| 417 | - |
|
| 418 | - $this->assertEquals(array( |
|
| 419 | - 'target' => 'bar_action', |
|
| 420 | - 'params' => array( |
|
| 421 | - 'customId' => 'AB1', |
|
| 422 | - ), |
|
| 423 | - 'name' => 'bar_route' |
|
| 424 | - ), $this->router->match('/bar/AB1', 'GET')); |
|
| 410 | + /** |
|
| 411 | + * @covers Router::setMatchTypes |
|
| 412 | + */ |
|
| 413 | + public function testMatchWithCustomNamedRegex() |
|
| 414 | + { |
|
| 415 | + $this->router->getParser()->setMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?')); |
|
| 416 | + $this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route'); |
|
| 417 | + |
|
| 418 | + $this->assertEquals(array( |
|
| 419 | + 'target' => 'bar_action', |
|
| 420 | + 'params' => array( |
|
| 421 | + 'customId' => 'AB1', |
|
| 422 | + ), |
|
| 423 | + 'name' => 'bar_route' |
|
| 424 | + ), $this->router->match('/bar/AB1', 'GET')); |
|
| 425 | 425 | |
| 426 | - $this->assertEquals(array( |
|
| 427 | - 'target' => 'bar_action', |
|
| 428 | - 'params' => array( |
|
| 429 | - 'customId' => 'AB1_0123456789', |
|
| 430 | - ), |
|
| 431 | - 'name' => 'bar_route' |
|
| 432 | - ), $this->router->match('/bar/AB1_0123456789', 'GET')); |
|
| 433 | - |
|
| 434 | - $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
| 435 | - } |
|
| 426 | + $this->assertEquals(array( |
|
| 427 | + 'target' => 'bar_action', |
|
| 428 | + 'params' => array( |
|
| 429 | + 'customId' => 'AB1_0123456789', |
|
| 430 | + ), |
|
| 431 | + 'name' => 'bar_route' |
|
| 432 | + ), $this->router->match('/bar/AB1_0123456789', 'GET')); |
|
| 433 | + |
|
| 434 | + $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
| 435 | + } |
|
| 436 | 436 | |
| 437 | - public function testMatchWithCustomNamedUnicodeRegex() |
|
| 438 | - { |
|
| 439 | - $pattern = '[^'; |
|
| 440 | - // Arabic characters |
|
| 441 | - $pattern .= '\x{0600}-\x{06FF}'; |
|
| 442 | - $pattern .= '\x{FB50}-\x{FDFD}'; |
|
| 443 | - $pattern .= '\x{FE70}-\x{FEFF}'; |
|
| 444 | - $pattern .= '\x{0750}-\x{077F}'; |
|
| 445 | - $pattern .= ']+'; |
|
| 446 | - |
|
| 447 | - $this->router->getParser()->setMatchTypes(array('nonArabic' => $pattern)); |
|
| 448 | - $this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route'); |
|
| 437 | + public function testMatchWithCustomNamedUnicodeRegex() |
|
| 438 | + { |
|
| 439 | + $pattern = '[^'; |
|
| 440 | + // Arabic characters |
|
| 441 | + $pattern .= '\x{0600}-\x{06FF}'; |
|
| 442 | + $pattern .= '\x{FB50}-\x{FDFD}'; |
|
| 443 | + $pattern .= '\x{FE70}-\x{FEFF}'; |
|
| 444 | + $pattern .= '\x{0750}-\x{077F}'; |
|
| 445 | + $pattern .= ']+'; |
|
| 446 | + |
|
| 447 | + $this->router->getParser()->setMatchTypes(array('nonArabic' => $pattern)); |
|
| 448 | + $this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route'); |
|
| 449 | 449 | |
| 450 | - $this->assertEquals(array( |
|
| 451 | - 'target' => 'non_arabic_action', |
|
| 452 | - 'name' => 'non_arabic_route', |
|
| 453 | - 'params' => array( |
|
| 454 | - 'string' => 'some-path' |
|
| 455 | - ) |
|
| 456 | - ), $this->router->match('/bar/some-path', 'GET')); |
|
| 457 | - |
|
| 458 | - $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
| 459 | - } |
|
| 450 | + $this->assertEquals(array( |
|
| 451 | + 'target' => 'non_arabic_action', |
|
| 452 | + 'name' => 'non_arabic_route', |
|
| 453 | + 'params' => array( |
|
| 454 | + 'string' => 'some-path' |
|
| 455 | + ) |
|
| 456 | + ), $this->router->match('/bar/some-path', 'GET')); |
|
| 457 | + |
|
| 458 | + $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
| 459 | + } |
|
| 460 | 460 | } |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | { |
| 85 | 85 | $method = 'POST'; |
| 86 | 86 | $route = '/[:controller]/[:action]'; |
| 87 | - $target = function () { |
|
| 87 | + $target = function() { |
|
| 88 | 88 | }; |
| 89 | 89 | |
| 90 | 90 | $this->assertInternalType('array', $this->router->getRoutes()); |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | { |
| 100 | 100 | $method = 'POST'; |
| 101 | 101 | $route = '/[:controller]/[:action]'; |
| 102 | - $target = function () { |
|
| 102 | + $target = function() { |
|
| 103 | 103 | }; |
| 104 | 104 | |
| 105 | 105 | $this->router->setRoutes(array( |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | { |
| 162 | 162 | $method = 'POST'; |
| 163 | 163 | $route = '/[:controller]/[:action]'; |
| 164 | - $target = function () { |
|
| 164 | + $target = function() { |
|
| 165 | 165 | }; |
| 166 | 166 | |
| 167 | 167 | $this->router->map($method, $route, $target); |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | { |
| 179 | 179 | $method = 'POST'; |
| 180 | 180 | $route = '/[:controller]/[:action]'; |
| 181 | - $target = function () { |
|
| 181 | + $target = function() { |
|
| 182 | 182 | }; |
| 183 | 183 | $name = 'myroute'; |
| 184 | 184 | |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | 'action' => 'someaction' |
| 210 | 210 | ); |
| 211 | 211 | |
| 212 | - $this->router->map('GET', '/[:controller]/[:action]', function () { |
|
| 212 | + $this->router->map('GET', '/[:controller]/[:action]', function() { |
|
| 213 | 213 | }, 'foo_route'); |
| 214 | 214 | |
| 215 | 215 | $this->assertEquals( |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | |
| 236 | 236 | public function testGenerateWithOptionalUrlParts() |
| 237 | 237 | { |
| 238 | - $this->router->map('GET', '/[:controller]/[:action].[:type]?', function () { |
|
| 238 | + $this->router->map('GET', '/[:controller]/[:action].[:type]?', function() { |
|
| 239 | 239 | }, 'bar_route'); |
| 240 | 240 | |
| 241 | 241 | $params = array( |