diff options
| -rw-r--r-- | src/AbstractStrategy.php | 32 | ||||
| -rw-r--r-- | src/BasicStrategy.php | 3 | ||||
| -rw-r--r-- | src/Contracts/Middleware.php | 16 | ||||
| -rw-r--r-- | src/Contracts/Strategy.php | 10 | ||||
| -rw-r--r-- | tests/Unit/BasicStrategyTest.php | 6 | ||||
| -rw-r--r-- | tests/Unit/MiddlewareTest.php | 19 | ||||
| -rw-r--r-- | tests/Utils/TestCallable.php | 2 | ||||
| -rw-r--r-- | tests/Utils/TestMiddleware.php | 27 |
8 files changed, 111 insertions, 4 deletions
diff --git a/src/AbstractStrategy.php b/src/AbstractStrategy.php new file mode 100644 index 0000000..54b78c1 --- /dev/null +++ b/src/AbstractStrategy.php @@ -0,0 +1,32 @@ +<?php + +declare(strict_types=1); + +namespace Lightscale\Router; + +use Lightscale\Router\Contracts\Middleware; +use Lightscale\Router\Contracts\Strategy; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; + +abstract class AbstractStrategy implements Strategy +{ + /** + * @param Middleware[] $middlewares + * @param callable(RequestInterface): ResponseInterface $handler + */ + public function runMiddleware( + RequestInterface $request, + array $middlewares, + callable $handler, + ): ResponseInterface { + $handler = count($middlewares) > 0 ? array_reduce( + array_reverse($middlewares), + fn (callable $handler, Middleware $middleware) => fn (ServerRequestInterface $request) => $middleware->handle($request, $handler), + $handler + ) : $handler; + + return $handler($request); + } +} diff --git a/src/BasicStrategy.php b/src/BasicStrategy.php index 5043576..d2ee91a 100644 --- a/src/BasicStrategy.php +++ b/src/BasicStrategy.php @@ -4,13 +4,12 @@ declare(strict_types=1); namespace Lightscale\Router; -use Lightscale\Router\Contracts\Strategy; use Lightscale\Router\Exceptions\InvalidResponseException; use Lightscale\Router\Exceptions\NotFoundException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -class BasicStrategy implements Strategy +class BasicStrategy extends AbstractStrategy { public function runRoute(RouteCall $call): ResponseInterface { diff --git a/src/Contracts/Middleware.php b/src/Contracts/Middleware.php new file mode 100644 index 0000000..96037c8 --- /dev/null +++ b/src/Contracts/Middleware.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Lightscale\Router\Contracts; + +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; + +interface Middleware +{ + public function handle( + ServerRequestInterface $request, + callable $next, + ): ResponseInterface; +} diff --git a/src/Contracts/Strategy.php b/src/Contracts/Strategy.php index bb877c1..fa41819 100644 --- a/src/Contracts/Strategy.php +++ b/src/Contracts/Strategy.php @@ -10,6 +10,16 @@ use Psr\Http\Message\ResponseInterface; interface Strategy { + /** + * @param Middleware[] $middlewares + * @param callable(RequestInterface): ResponseInterface $handler + */ + public function runMiddleware( + RequestInterface $request, + array $middlewares, + callable $handler, + ): ResponseInterface; + public function runRoute(RouteCall $call): ResponseInterface; public function notFound(RequestInterface $request): ResponseInterface; diff --git a/tests/Unit/BasicStrategyTest.php b/tests/Unit/BasicStrategyTest.php index 1f1489a..896858d 100644 --- a/tests/Unit/BasicStrategyTest.php +++ b/tests/Unit/BasicStrategyTest.php @@ -37,8 +37,12 @@ it('calls route', function () { [] )); - $cb->assertIsCalled(); + $cb->assertCalled(); $call = $cb->getLastCall(); expect($call->args[0] ?? null)->toBe($rc); expect($res)->toBe($response); }); + +it('runs middleware', function() { + $factory = new +}); diff --git a/tests/Unit/MiddlewareTest.php b/tests/Unit/MiddlewareTest.php new file mode 100644 index 0000000..d81300d --- /dev/null +++ b/tests/Unit/MiddlewareTest.php @@ -0,0 +1,19 @@ +<?php + +declare(strict_types=1); + +use Lightscale\Router\Test\Utils\TestCallable; +use Lightscale\Router\Test\Utils\TestMiddleware; +use Nyholm\Psr7\Factory\Psr17Factory; + +it('calls handler', function () { + $factory = new Psr17Factory; + $request = $factory->createServerRequest('get', 'testing'); + $response = $factory->createResponse(); + $middleware = new TestMiddleware; + $handler = new TestCallable(fn() => $response); + $result = $middleware->handle($request, $handler); + $middleware->assertCalled(); + $handler->assertCalled(); + expect($result)->toBe($response); +}); diff --git a/tests/Utils/TestCallable.php b/tests/Utils/TestCallable.php index 526ddf4..67b3d6c 100644 --- a/tests/Utils/TestCallable.php +++ b/tests/Utils/TestCallable.php @@ -51,7 +51,7 @@ class TestCallable return $this->calls[$this->getCallCount() - 1] ?? null; } - public function assertIsCalled(): void + public function assertCalled(): void { Assert::assertGreaterThan(0, $this->getCallCount(), 'Not been called'); } diff --git a/tests/Utils/TestMiddleware.php b/tests/Utils/TestMiddleware.php new file mode 100644 index 0000000..2f3ed32 --- /dev/null +++ b/tests/Utils/TestMiddleware.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); + +namespace Lightscale\Router\Test\Utils; + +use Lightscale\Router\Contracts\Middleware; +use PHPUnit\Framework\Assert; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; + +class TestMiddleware implements Middleware +{ + private int $calls = 0; + + public function handle(ServerRequestInterface $request, callable $next): ResponseInterface + { + ++$this->calls; + + return $next($request); + } + + public function assertCalled(): void + { + Assert::assertGreaterThan(0, $this->calls); + } +} |
