From b4bc66d28883d11a1346f165ef71e630a558ee9c Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 25 Nov 2024 22:08:28 -0500 Subject: [PATCH] Test case for the flash message seems to work --- app/src/Controller/Controller.php | 1 - app/src/Middlewares.php | 1 + app/templates/layouts/components/flash.twig | 6 +++--- app/tests/Controller/ControllerTest.php | 14 ++++++++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/src/Controller/Controller.php b/app/src/Controller/Controller.php index fb9b60b..7b61c2e 100644 --- a/app/src/Controller/Controller.php +++ b/app/src/Controller/Controller.php @@ -6,7 +6,6 @@ use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Views\Twig; use Slim\Routing\RouteContext; -use Odan\Session\SessionInterface; abstract class Controller { diff --git a/app/src/Middlewares.php b/app/src/Middlewares.php index 721a2c9..2cefbbb 100644 --- a/app/src/Middlewares.php +++ b/app/src/Middlewares.php @@ -17,6 +17,7 @@ class Middlewares { /** * @param App $app The Slim application + * @return void * @throws NotFoundExceptionInterface * @throws ContainerExceptionInterface * @throws LoaderError diff --git a/app/templates/layouts/components/flash.twig b/app/templates/layouts/components/flash.twig index d4497d6..c61ff02 100644 --- a/app/templates/layouts/components/flash.twig +++ b/app/templates/layouts/components/flash.twig @@ -1,17 +1,17 @@ {% for message in flash.get('success') %} -
+ {% endfor %} {% for message in flash.get('notice') %} -
+ {% endfor %} {% for message in flash.get('error') %} -
+ {% endfor %} diff --git a/app/tests/Controller/ControllerTest.php b/app/tests/Controller/ControllerTest.php index 747f767..43a88c5 100644 --- a/app/tests/Controller/ControllerTest.php +++ b/app/tests/Controller/ControllerTest.php @@ -115,7 +115,7 @@ class ControllerTest extends TestCase public function handle(): Response { $this->session->getFlash()->add("error", "Error message"); - return $this->response; + return $this->render('home.twig'); } }; @@ -124,8 +124,14 @@ class ControllerTest extends TestCase $response = $app->handle($request); $flash = $app->getContainer()->get(SessionInterface::class)->getFlash(); - $errorMessages = $flash->get('error'); - $this->assertEquals('Error message', $errorMessages[0]); - $this->assertEquals(1, count($errorMessages)); + + $body = $response->getBody(); + $body->read($body->getSize()); + $this->assertTrue((bool) preg_match('/class\=\"flash/', $body)); + + //$errorMessages = $flash->get('error'); + //$this->assertNotEmpty($errorMessages); + //$this->assertEquals('Error message', $errorMessages[0]); + //$this->assertEquals(1, count($errorMessages)); } }