Skip to content

Commit

Permalink
feat: Change signature of Router::route
Browse files Browse the repository at this point in the history
Part of #7

Now route method takes a ServerRequestInterface as argument and returns
a ResponseInterface which can be handled by ResponseHandler
  • Loading branch information
Gashmob committed May 3, 2024
1 parent fd4e957 commit de92013
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
3 changes: 0 additions & 3 deletions include/HTTP/ResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@

use Psr\Http\Message\ResponseInterface;

/**
* @internal
*/
final class ResponseHandler
{
public function writeResponse(ResponseInterface $response): void
Expand Down
14 changes: 5 additions & 9 deletions include/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
use Archict\Router\Exception\FailedToCreateRouteException;
use Archict\Router\Exception\HTTP\HTTPException;
use Archict\Router\Exception\RouterException;
use Archict\Router\HTTP\ResponseHandler;
use Archict\Router\Route\RouteCollection;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\TreeMapper;
use CuyZ\Valinor\MapperBuilder;
use CuyZ\Valinor\Normalizer\Format;
use CuyZ\Valinor\Normalizer\Normalizer;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\Psr7\ServerRequest;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;

Expand All @@ -42,15 +41,14 @@ public function __construct(
* @throws RouterException
* @throws InvalidArgumentException
*/
public function route(): void
public function route(ServerRequestInterface $request): ResponseInterface
{
$this->loadRoutes();

$factory = new HttpFactory();
try {
$request = ServerRequest::fromGlobals();
$path = $request->getUri()->getPath();
$route = $this->route_collection->getMatchingRoute($path, $request->getMethod());
$path = $request->getUri()->getPath();
$route = $this->route_collection->getMatchingRoute($path, $request->getMethod());

$attributes = [];
assert(preg_match($route->route_regex, $path, $attributes) === 1);
Expand All @@ -69,9 +67,7 @@ public function route(): void
$response = $exception->toResponse();
}

assert($response instanceof ResponseInterface);
$response_handler = new ResponseHandler();
$response_handler->writeResponse($response);
return $response;
}

/**
Expand Down
13 changes: 9 additions & 4 deletions tests/unit/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@

use Archict\Core\Core;
use Archict\Core\Services\ServiceManager;
use GuzzleHttp\Psr7\ServerRequest;
use PHPUnit\Framework\TestCase;

final class RouterTest extends TestCase
{
private ServiceManager $service_manager;
private Router $router;

protected function setUp(): void
{
$core = Core::build();
$core->load();
$this->service_manager = $core->service_manager;

$router = $this->service_manager->get(Router::class);
self::assertNotNull($router);
$this->router = $router;
}

public function testRouterIsLoaded(): void
Expand All @@ -25,10 +31,9 @@ public function testRouterIsLoaded(): void
self::assertInstanceOf(Router::class, $this->service_manager->get(Router::class));
}

public function testItNotThrow(): void
public function testItReturns404(): void
{
$router = $this->service_manager->get(Router::class);
self::assertNotNull($router);
$router->route();
$response = $this->router->route(new ServerRequest('GET', 'route'));
self::assertSame(404, $response->getStatusCode());
}
}

0 comments on commit de92013

Please sign in to comment.