Add some Flash message tests and the flashes to the skeleton template.

This commit is contained in:
Dave Smith-Hayes 2024-06-04 21:16:59 -04:00
parent 76b526646e
commit 1914885d04
6 changed files with 56 additions and 23 deletions

12
app/composer.lock generated
View File

@ -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",

View File

@ -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);
});
}
/**

View File

@ -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);
}

View File

@ -11,6 +11,11 @@
</header>
<main>
{% for message in flash.get('error') %}
<div class="flash error">
{{ message }}
</div>
{% endfor %}
{% block content %}{% endblock %}
</main>

View File

@ -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('<h1>Slovocast</h1>', $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));
}
}

View File

@ -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]);
}
}