summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/BasicStrategyTest.php6
-rw-r--r--tests/Unit/MiddlewareTest.php19
-rw-r--r--tests/Utils/TestCallable.php2
-rw-r--r--tests/Utils/TestMiddleware.php27
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);
+ }
+}