diff options
| author | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
| commit | b899a995e2fd977fbf7497b36b2dbc8d641ef398 (patch) | |
| tree | e35680ea490fb486da43012bbfaaee8b7ee27f8c /tests | |
| parent | 57e101668e62aab7d332c1c2f403eee9511de626 (diff) | |
Started structure for middleware
Diffstat (limited to 'tests')
| -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 |
4 files changed, 52 insertions, 2 deletions
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); + } +} |
