diff --git a/Routes.php b/Routes.php index 9723f04..5264e94 100644 --- a/Routes.php +++ b/Routes.php @@ -444,4 +444,16 @@ public function middleware(array $data) : self { $this->middlewares = array_merge($this->middlewares, $data); return $this; } + + /** + * Method used to append more routes to the main route handler + * + * @param array $routes List of routes from other route classes + * + * @return Routes Returns an instance of itself so that other methods could be chained onto it + */ + public function append(array $routes): self { + $this->routes = array_merge_recursive($routes, $this->routes); + return $this; + } } \ No newline at end of file diff --git a/composer.json b/composer.json index f4d77d9..5c23054 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "autoload": { "psr-4": { "Gac\\Routing\\": "./", - "Gac\\Routing\\Exceptions\\": "./" + "Gac\\Routing\\Exceptions\\": "./Exceptions" } }, "require-dev": { diff --git a/phpunit.xml b/phpunit.xml index 8df3d89..047921a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,7 +6,7 @@ beStrictAboutOutputDuringTests="true" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - bootstrap="tests/bootstrap.php" executionOrder="random"> + bootstrap="vendor/autoload.php" executionOrder="random"> diff --git a/sample/index.php b/sample/index.php index 3c2fead..caaa887 100644 --- a/sample/index.php +++ b/sample/index.php @@ -133,6 +133,16 @@ print_r([ $_REQUEST, $_FILES ]); }, [ Routes::PUT ]); + + //This $otherRoutes variable could be defined in a different file and included here only via require or include + $otherRoutes = new Routes(); + $otherRoutes->prefix("/sample") + ->get("/", function (Request $request){}) + ->get("/sample1", function (Request $request){}) + ->get("/sample2", function (Request $request){}) + ->save(); + + $routes->append($otherRoutes->get_routes()); $routes->handle(); } catch ( RouteNotFoundException $ex ) { $routes->request->status(404, 'Route not found')->send([ 'error' => [ 'message' => $ex->getMessage() ] ]); diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index ddfe10b..a5bc0e5 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -7,38 +7,62 @@ class RoutesTest extends TestCase { private Routes $routes; - public function setUp(): void - { + public function setUp() : void { parent::setUp(); $this->routes = new Routes(); } - public function testCanAddNewRoute() - { + public function testCanAddNewRoute() { $this->routes->add("/", []); $this->assertTrue(isset($this->routes->get_routes()["GET"]["/"]), "Unable to add new route"); } - public function testCanAddMultipleRequestMethodRoutes() - { + public function testCanAddMultipleRequestMethodRoutes() { $this->routes->add("/test", [], [ Routes::GET, Routes::POST ]); $routes = $this->routes->get_routes(); $this->assertTrue(isset($routes["GET"]["/test"]) && isset($routes["POST"]["/test"]), "Unable to add new route"); } - public function testCannAddMiddleware() - { + public function testCannAddMiddleware() { $this->routes->middleware([ "test" ])->add("/middleware", []); $this->assertTrue($this->routes->get_routes()["GET"]["/middleware"]["middlewares"][0] == "test", "Unable to add middleware"); } - public function testCannAddPrefix() - { + public function testCannAddPrefix() { $this->routes->prefix("/testing")->add("/test", []); $routes = $this->routes->get_routes(); $this->assertTrue(isset($routes["GET"]["/testing/test"]), "Unable to add prefix to routes"); } + + public function testSave() { + $this->routes->prefix('/testing')->add('/test', []); + $routes = $this->routes->get_routes(); + + $this->assertCount(1, $routes["GET"]); + } + + public function testAdd() { + $this->routes->prefix('/testing')->add('/test', []); + $routes = $this->routes->get_routes(); + + $this->assertCount(1, $routes['GET']); + } + + public function testAppendNewRoutes() { + $this->routes->add('/', []); + + $appendedRoutes = new Routes(); + $appendedRoutes->prefix("/test") + ->get("/appended", function () { }) + ->get("/appended_sample", function () { }) + ->save(); + + $newRoutes = $appendedRoutes->get_routes(); + $this->routes->append($newRoutes); + + $this->assertCount(3, $this->routes->get_routes()["GET"]); + } }