/** * Add GET|POST|PUT|DELETE route * * Adds a new route to the router with associated callable. This * route will only be invoked when the HTTP request's method matches * this route's method. * * ARGUMENTS: * * First: string The URL pattern (REQUIRED) * In-Between: mixed Anything that returns TRUE for `is_callable` (OPTIONAL) * Last: mixed Anything that returns TRUE for `is_callable` (REQUIRED) * * The first argument is required and must always be the * route pattern (ie. '/books/:id'). * * The last argument is required and must always be the callable object * to be invoked when the route matches an HTTP request. * * You may also provide an unlimited number of in-between arguments; * each interior argument must be callable and will be invoked in the * order specified before the route's callable is invoked. * * USAGE: * * Slim::get('/foo'[, middleware, middleware, ...], callable); * * @param array (See notes above) * @return Slim_Route */ protected function mapRoute($args) { $pattern = array_shift($args); $callable = array_pop($args); $route = $this->router->map($pattern, $callable); if (count($args) > 0) { $route->setMiddleware($args); } return $route; }
/** * Router::urlFor */ public function testRouterUrlFor() { $_SERVER['REQUEST_METHOD'] = 'GET'; $request = new Slim_Http_Request(); $router = new Slim_Router($request); $route1 = $router->map('/foo/bar', function () { }, 'GET'); $route2 = $router->map('/foo/:one/:two', function () { }, 'GET'); $route3 = $router->map('/foo/:one(/:two)', function () { }, 'GET'); $route4 = $router->map('/foo/:one/(:two/)', function () { }, 'GET'); $route5 = $router->map('/foo/:one/(:two/(:three/))', function () { }, 'GET'); $route1->setName('route1'); $route2->setName('route2'); $route3->setName('route3'); $route4->setName('route4'); $route5->setName('route5'); //Route $this->assertEquals('/foo/bar', $router->urlFor('route1')); //Route with params $this->assertEquals('/foo/foo/bar', $router->urlFor('route2', array('one' => 'foo', 'two' => 'bar'))); $this->assertEquals('/foo/foo/:two', $router->urlFor('route2', array('one' => 'foo'))); $this->assertEquals('/foo/:one/bar', $router->urlFor('route2', array('two' => 'bar'))); //Route with params and optional segments $this->assertEquals('/foo/foo/bar', $router->urlFor('route3', array('one' => 'foo', 'two' => 'bar'))); $this->assertEquals('/foo/foo', $router->urlFor('route3', array('one' => 'foo'))); $this->assertEquals('/foo/:one/bar', $router->urlFor('route3', array('two' => 'bar'))); $this->assertEquals('/foo/:one', $router->urlFor('route3')); //Route with params and optional segments $this->assertEquals('/foo/foo/bar/', $router->urlFor('route4', array('one' => 'foo', 'two' => 'bar'))); $this->assertEquals('/foo/foo/', $router->urlFor('route4', array('one' => 'foo'))); $this->assertEquals('/foo/:one/bar/', $router->urlFor('route4', array('two' => 'bar'))); $this->assertEquals('/foo/:one/', $router->urlFor('route4')); //Route with params and optional segments $this->assertEquals('/foo/foo/bar/what/', $router->urlFor('route5', array('one' => 'foo', 'two' => 'bar', 'three' => 'what'))); $this->assertEquals('/foo/foo/', $router->urlFor('route5', array('one' => 'foo'))); $this->assertEquals('/foo/:one/bar/', $router->urlFor('route5', array('two' => 'bar'))); $this->assertEquals('/foo/:one/bar/what/', $router->urlFor('route5', array('two' => 'bar', 'three' => 'what'))); $this->assertEquals('/foo/:one/', $router->urlFor('route5')); }
/** * Test that Router implements IteratorAggregate interface */ public function testRouterImplementsIteratorAggregate() { $router = new Slim_Router($this->req, $this->res); $router->map('/bar', function () { })->via('GET'); $router->map('/foo1', function () { })->via('POST'); $router->map('/bar', function () { })->via('PUT'); $router->map('/foo/bar/xyz', function () { })->via('DELETE'); $iterator = $router->getIterator(); $this->assertInstanceOf('ArrayIterator', $iterator); $this->assertEquals(2, $iterator->count()); }
/** * Test that router returns matched routes based on URI only, not * based on the HTTP method. */ public function testRouterMatchesRoutesByUriOnly() { Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', 'PATH_INFO' => '/foo', 'QUERY_STRING' => 'one=1&two=2&three=3', 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim')); $this->env = Slim_Environment::getInstance(); $this->req = new Slim_Http_Request($this->env); $this->res = new Slim_Http_Response(); $router = new Slim_Router($this->req, $this->res); $router->map('/foo', function () { })->via('GET'); $router->map('/foo', function () { })->via('POST'); $router->map('/foo', function () { })->via('PUT'); $router->map('/foo/bar/xyz', function () { })->via('DELETE'); $this->assertEquals(3, count($router->getMatchedRoutes())); }
/** * Test that router returns matched routes based on URI only, not * based on the HTTP method. */ public function testRouterMatchesRoutesByUriOnly() { $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/foo'; $request = new Slim_Http_Request(); $router = new Slim_Router($request); $router->map('/foo', function () { })->via('GET'); $router->map('/foo', function () { })->via('POST'); $router->map('/foo', function () { })->via('PUT'); $router->map('/foo/bar/xyz', function () { })->via('DELETE'); $this->assertEquals(3, count($router->getMatchedRoutes())); }