/** * Maps the route into routing table * * @param Route $route A route instance * @return Router Returns current route instance */ public function map(Route $route) { list($path, $groupMiddleware) = $this->processed; if ($path != '') { $route->setPath($path . $route->getPath()); } if (!empty($groupMiddleware)) { $route->addMiddleware($groupMiddleware); } //Appends route to routing table. //We are using Trie search data structure. $this->routingTable->appendRoute($route); if ($route->getName() !== null) { //This is the named route if (isset($this->namedRoutes[$route->getName()])) { throw new \RuntimeException(sprintf("The Route with the name '%s' alredy exists", $route->getName())); } else { $this->namedRoutes[$route->getName()] = $route; } } return $this; }
/** * @test * @depends testAppendRoute * @param Table $table */ public function testGetMatchedRoutes($table) { $this->assertEmpty($table->getMatchedRoutes(Request::METHOD_GET, '/nothing')); //Existing routes $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v12/hotels'); $this->assertNotEmpty($res); $this->assertEquals(1, count($res)); //Trailing slash should not be a trouble $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v12/hotels/'); $this->assertNotEmpty($res); $this->assertEquals(1, count($res)); $route = current($res); $this->assertInstanceOf('Scalr\\Api\\Rest\\Routing\\Route', $route); $this->assertEquals(['apiversion' => 12], $route->getParams()); $res = $table->getMatchedRoutes(Request::METHOD_POST, '/api/users/v1/flights'); $this->assertNotEmpty($res); $this->assertEquals(1, count($res)); $route = current($res); $this->assertInstanceOf('Scalr\\Api\\Rest\\Routing\\Route', $route); $this->assertEquals(['apiversion' => 1], $route->getParams()); //Missing method on existing route $this->assertEmpty($table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v1/flights')); //Not complete path $this->assertEmpty($table->getMatchedRoutes(Request::METHOD_POST, '/api/users/v1')); //Two different routes on the same path with different methods $getFlightsRoute = (new Route('/api/users/v:apiversion/flights', function () { }, ['apiversion' => '[\\d]+']))->setMethods([Request::METHOD_GET]); $table->appendRoute($getFlightsRoute); $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v1/flights'); $this->assertNotEmpty($res); $this->assertEquals(1, count($res)); $route = current($res); $this->assertSame($getFlightsRoute, $route); //Two different routes on the same path with different requirements and handlers $getFlightsRoute2 = (new Route('/api/users/v:apiversion/flights', function () { }, ['apiversion' => '[\\w]+']))->setMethods([Request::METHOD_GET]); $table->appendRoute($getFlightsRoute2); //It matches the first $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v1/flights'); $this->assertEquals(1, count($res)); $this->assertSame($getFlightsRoute, $res[0]); //It does not matches the first but matches the second $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/version2/flights'); $this->assertEquals(1, count($res)); $this->assertSame($getFlightsRoute2, $res[0]); //Two different routes on the same path with the same requirements, same methods but with different handlers $getFlightsRoute3 = (new Route('/api/users/v:apiversion/flights', function () { /* handler 2 */ }, ['apiversion' => '[\\w]+']))->setMethods([Request::METHOD_GET]); $table->appendRoute($getFlightsRoute3); $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/version2/flights'); $this->assertEquals(2, count($res)); $this->assertContains($getFlightsRoute2, $res); $this->assertContains($getFlightsRoute3, $res); }