Skip to content

Commit

Permalink
Add append method and update sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
gigili committed Mar 19, 2022
1 parent df4e4fd commit 64dad8a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
12 changes: 12 additions & 0 deletions Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"autoload": {
"psr-4": {
"Gac\\Routing\\": "./",
"Gac\\Routing\\Exceptions\\": "./"
"Gac\\Routing\\Exceptions\\": "./Exceptions"
}
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<coverage processUncoveredFiles="true">
<report>
<html outputDirectory="./coverage" lowUpperBound="20" highLowerBound="50"/>
Expand Down
10 changes: 10 additions & 0 deletions sample/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ] ]);
Expand Down
44 changes: 34 additions & 10 deletions tests/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
}
}

0 comments on commit 64dad8a

Please sign in to comment.