From 1914885d049478dd3ba1feacd0e71d3e092164fe Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Tue, 4 Jun 2024 21:16:59 -0400 Subject: [PATCH] Add some Flash message tests and the flashes to the skeleton template. --- app/composer.lock | 12 +++---- app/src/Bootstrap.php | 25 ++++++--------- .../Controller/User/RegisterUserAction.php | 1 + app/templates/layouts/skeleton.twig | 5 +++ app/tests/Controller/ControllerTest.php | 31 +++++++++++++++++++ .../User/RegisterUserActionTest.php | 5 ++- 6 files changed, 56 insertions(+), 23 deletions(-) diff --git a/app/composer.lock b/app/composer.lock index f470900..c83e3a0 100644 --- a/app/composer.lock +++ b/app/composer.lock @@ -2743,16 +2743,16 @@ }, { "name": "phpstan/phpdoc-parser", - "version": "1.29.0", + "version": "1.29.1", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "536889f2b340489d328f5ffb7b02bb6b183ddedc" + "reference": "fcaefacf2d5c417e928405b71b400d4ce10daaf4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/536889f2b340489d328f5ffb7b02bb6b183ddedc", - "reference": "536889f2b340489d328f5ffb7b02bb6b183ddedc", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/fcaefacf2d5c417e928405b71b400d4ce10daaf4", + "reference": "fcaefacf2d5c417e928405b71b400d4ce10daaf4", "shasum": "" }, "require": { @@ -2784,9 +2784,9 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.29.0" + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.29.1" }, - "time": "2024-05-06T12:04:23+00:00" + "time": "2024-05-31T08:52:43+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index ff42027..a0903c0 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -73,9 +73,8 @@ class Bootstrap $containerBuilder->addDefinitions([ 'config' => $config, // more DI stuff - 'flash' => function () { - $messages = []; - return new \Slim\Flash\Messages($messages); + 'flash' => function (ContainerInterface $container) { + return $container->get(SessionInterface::class)->getFlash(); }, LoggerInterface::class => function() { $logger = new Logger(); @@ -116,10 +115,13 @@ class Bootstrap */ protected static function establishMiddleware(App $app): void { + /** @var ContainerInterface $container */ + $container = $app->getContainer(); + /** * @var Configuration */ - $config = $app->getContainer()->get('config'); + $config = $container->get('config'); $app->addErrorMiddleware(true, true, true); // Twig @@ -132,19 +134,10 @@ class Bootstrap ->addGlobal('site_name', $config->get('site.name')); $twig->getEnvironment() ->addGlobal('site_description', $config->get('site.description')); + $flash = $container->get(SessionInterface::class)->getFlash(); + $twig->getEnvironment() + ->addGlobal('flash', $flash); $app->add(TwigMiddleware::create($app, $twig)); - - // Flash Messages - $app->add(function ($req, $next) { - if (session_status() !== PHP_SESSION_ACTIVE) { - session_start(); - } - - /** @var $this ContainerInterface */ - $this->get('flash')->__construct($_SESSION); - - return $next->handle($req); - }); } /** diff --git a/app/src/Controller/User/RegisterUserAction.php b/app/src/Controller/User/RegisterUserAction.php index 4648b55..7e7c006 100644 --- a/app/src/Controller/User/RegisterUserAction.php +++ b/app/src/Controller/User/RegisterUserAction.php @@ -21,6 +21,7 @@ class RegisterUserAction extends Controller $requestData = $this->request->getParsedBody(); if ($requestData['password'] !== $requestData['checked_password']) { + $this->session->getFlash()->add('error', "Passwords do not match."); $response = $this->render('user/register.twig'); return $response->withStatus(400); } diff --git a/app/templates/layouts/skeleton.twig b/app/templates/layouts/skeleton.twig index 0fcfb47..4bbb17f 100644 --- a/app/templates/layouts/skeleton.twig +++ b/app/templates/layouts/skeleton.twig @@ -11,6 +11,11 @@
+ {% for message in flash.get('error') %} +
+ {{ message }} +
+ {% endfor %} {% block content %}{% endblock %}
diff --git a/app/tests/Controller/ControllerTest.php b/app/tests/Controller/ControllerTest.php index 55f936c..1309e1e 100644 --- a/app/tests/Controller/ControllerTest.php +++ b/app/tests/Controller/ControllerTest.php @@ -5,6 +5,7 @@ namespace Slovocast\Tests\Controller; use Slovocast\Tests\TestCase; use Slovocast\Controller\Controller; use Psr\Http\Message\ResponseInterface as Response; +use Odan\Session\SessionInterface; class ControllerTest extends TestCase { @@ -97,4 +98,34 @@ class ControllerTest extends TestCase $this->assertEquals('

Slovocast

', $response->getBody()); } + + public function testFlashMessages(): void + { + $app = $this->getAppInstance(); + $session = $app->getContainer()->get(SessionInterface::class); + + $testController = new class($session) extends Controller { + protected SessionInterface $session; + + public function __construct(SessionInterface $session) + { + $this->session = $session; + } + + public function respond(): Response + { + $this->session->getFlash()->add("error", "Error message"); + return $this->response; + } + }; + + $app->get('/test', $testController); + $request = $this->createRequest('GET', '/test'); + + $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)); + } } diff --git a/app/tests/Controller/User/RegisterUserActionTest.php b/app/tests/Controller/User/RegisterUserActionTest.php index b106017..539ba1c 100644 --- a/app/tests/Controller/User/RegisterUserActionTest.php +++ b/app/tests/Controller/User/RegisterUserActionTest.php @@ -73,6 +73,9 @@ class RegisterUserActionTest extends TestCase $this->assertEquals(400, $response->getStatusCode()); $flash = $container->get(SessionInterface::class)->getFlash(); - $this->assertEquals('Unable to register user.', $flash->get('error')[0]); + $errorMessages = $flash->get('error'); + $this->assertIsArray($errorMessages); + $this->assertNotNull($errorMessages); + $this->assertEquals('Unable to register user.', $errorMessage[0]); } }