public function testAddRoute() { // Create Router, add some Routes $router = new Router(); $routes = [new Route('/foo'), new Route('/bar'), new Route('/baz')]; foreach ($routes as $route) { $router->addRoute($route); } $this->assertSame($routes, $router->getRoutes()); // Create a RouteCollection, add some Routes $col = new RouteCollection(new Route()); $col->get('/x', function () { }); $col->get('/y', function () { }); $col->get('/z', function () { }); // Add it to the router $router->addRouteCollection($col); $allRoutes = array_merge($routes, $col->getRoutes()); $this->assertSame($allRoutes, $router->getRoutes()); // Anything added to the collection after adding the collection to the // router should not be registered. $col->get('/a', function () { }); $col->get('/b', function () { }); $col->get('/c', function () { }); $this->assertSame($allRoutes, $router->getRoutes()); }
public function testForwardToExistingRoutes() { $callback = function () { }; $before = function () { }; $after = function () { }; $path = "/foo"; // Create a collection $baseRoute = new Route(); $collection = new RouteCollection($baseRoute); // Add some routes $routes = []; $routes[] = $collection->get($path, $callback); $routes[] = $collection->get($path, $callback); $routes[] = $collection->get($path, $callback); // Now add before and after callbacks to the collection $collection->before($before); $collection->after($after); // And check they are present on all routes in collection foreach ($routes as $route) { $this->assertInternalType('array', $route->getBefore()); $this->assertCount(1, $route->getBefore()); $this->assertSame($before, $route->getBefore()[0]); $this->assertInternalType('array', $route->getAfter()); $this->assertCount(1, $route->getAfter()); $this->assertSame($after, $route->getAfter()[0]); } // Now add more routes to the collection and check if it has the same // before and after $routes = []; $routes[] = $collection->get($path, $callback); $routes[] = $collection->get($path, $callback); $routes[] = $collection->get($path, $callback); foreach ($routes as $route) { $this->assertInternalType('array', $route->getBefore()); $this->assertCount(1, $route->getBefore()); $this->assertSame($before, $route->getBefore()[0]); $this->assertInternalType('array', $route->getAfter()); $this->assertCount(1, $route->getAfter()); $this->assertSame($after, $route->getAfter()[0]); } }