From 019ceba8b06a47bc72c04932924871382ab9475c Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 28 Jan 2024 21:37:04 +0100 Subject: [PATCH] Extend unittesting, add apidoc reference --- src/main/php/web/frontend/HtmxFlow.class.php | 1 + .../frontend/unittest/HtmxFlowTest.class.php | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/main/php/web/frontend/HtmxFlow.class.php b/src/main/php/web/frontend/HtmxFlow.class.php index 9557fad..b2338f6 100755 --- a/src/main/php/web/frontend/HtmxFlow.class.php +++ b/src/main/php/web/frontend/HtmxFlow.class.php @@ -8,6 +8,7 @@ * an `authenticationexpired` event is triggered instead of redirecting. * * @see https://htmx.org/reference/#headers + * @test web.frontend.unittest.HtmxFlowTest */ class HtmxFlow extends Flow { private $delegate; diff --git a/src/test/php/web/frontend/unittest/HtmxFlowTest.class.php b/src/test/php/web/frontend/unittest/HtmxFlowTest.class.php index 5f79185..ef233d8 100755 --- a/src/test/php/web/frontend/unittest/HtmxFlowTest.class.php +++ b/src/test/php/web/frontend/unittest/HtmxFlowTest.class.php @@ -1,11 +1,20 @@ authenticate(new Request(new TestInput('GET', '/', $headers)), $res, null); + return $res; + } + #[Test] public function can_create() { new HtmxFlow(new class() extends Flow { @@ -14,4 +23,29 @@ public function authenticate($request, $response, $session) { } }); } + + #[Test] + public function delegates_authentication() { + $res= $this->authenticate([], new HtmxFlow(new class() extends Flow { + public function authenticate($request, $response, $session) { + $response->answer(302); + $response->header('Location', '/login'); + } + })); + + Assert::equals(302, $res->status()); + Assert::equals('/login', $res->headers()['Location']); + } + + #[Test] + public function returns_error_code_and_triggers_authenticationexpired_event() { + $res= $this->authenticate(['Hx-Request' => 'true'], new HtmxFlow(new class() extends Flow { + public function authenticate($request, $response, $session) { + throw new IllegalStateException('Never called'); + } + })); + + Assert::equals(401, $res->status()); + Assert::equals('authenticationexpired', $res->headers()['HX-Trigger']); + } } \ No newline at end of file