Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cc1b50

Browse files
Pierstovalnicolas-grekas
authored andcommitted
[HttpFoundation] Add SessionHasFlashMessage test constraint
1 parent 409109c commit 2cc1b50

5 files changed

Lines changed: 180 additions & 0 deletions

File tree

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CHANGELOG
1717
* Enable mocking non-shared services in tests
1818
* Add support for setting `mock_response_factory` per scoped HTTP client
1919
* Add `ConsoleCommandAssertionsTrait` to `KernelTestCase` for running a command and asserting the result
20+
* Add `assertSessionHasFlashMessage()` to `BrowserKitAssertionsTrait`
2021
* Add `framework.html_sanitizer.sanitizers.*.default_action` config option
2122
* Deprecate parameters `router.request_context.scheme` and `router.request_context.host`;
2223
use the `router.request_context.base_url` parameter or the `framework.router.default_uri` config option instead

src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ public static function assertRouteSame(string $expectedRoute, array $parameters
180180
self::assertThat(self::getRequest(), $constraint, $message);
181181
}
182182

183+
public static function assertSessionHasFlashMessage(string $messageType, string|array $messages = ''): void
184+
{
185+
static::assertThat(self::getRequest(), new ResponseConstraint\SessionHasFlashMessage($messageType, $messages));
186+
}
187+
183188
public static function assertThatForResponse(Constraint $constraint, string $message = ''): void
184189
{
185190
try {

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `BinaryFileResponse::shouldDeleteFileAfterSend()`
88
* Deprecate setting public properties of `Request` and `Response` objects directly; use setters or constructor arguments instead
9+
* Add `SessionHasFlashMessage` test constraint
910

1011
8.0
1112
---
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
17+
18+
final class SessionHasFlashMessage extends Constraint
19+
{
20+
public function __construct(
21+
private readonly string $messageType,
22+
private readonly mixed $messages,
23+
) {
24+
}
25+
26+
public function toString(): string
27+
{
28+
return \sprintf('session has flash message of type "%s" containing: %s', $this->messageType, implode(', ', $this->getExpectedMessages()));
29+
}
30+
31+
protected function getExpectedMessages(): array
32+
{
33+
return \is_array($this->messages) ? $this->messages : [(string) $this->messages];
34+
}
35+
36+
protected function matches(mixed $other): bool
37+
{
38+
if (!$other instanceof Request) {
39+
return false;
40+
}
41+
42+
if (!$other->hasSession()) {
43+
return false;
44+
}
45+
46+
$session = $other->getSession();
47+
48+
if (!$session instanceof FlashBagAwareSessionInterface) {
49+
return false;
50+
}
51+
52+
$flashbag = $session->getFlashBag();
53+
$flashMessages = $flashbag->peek($this->messageType);
54+
$expectedMessages = $this->getExpectedMessages();
55+
56+
return array_any($flashMessages, static fn (mixed $message) => \in_array($message, $expectedMessages, true));
57+
}
58+
59+
protected function failureDescription(mixed $other): string
60+
{
61+
if (!$other instanceof Request) {
62+
return 'because the constraint was not configured with a Request object';
63+
}
64+
65+
$message = $this->toString();
66+
67+
if (!$other->hasSession()) {
68+
return $message.', because the Request does not have a Session';
69+
}
70+
71+
$session = $other->getSession();
72+
if (!$session instanceof FlashBagAwareSessionInterface) {
73+
return $message.', because the Session does not have a FlashBag';
74+
}
75+
76+
return $message;
77+
}
78+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\ExpectationFailedException;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Session\Session;
19+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
20+
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
21+
use Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage;
22+
23+
class SessionHasFlashMessageTest extends TestCase
24+
{
25+
#[DataProvider('provideMessages')]
26+
public function testSuccessfulCase(string|array $flashMessages)
27+
{
28+
$request = new Request();
29+
$session = new Session(new MockArraySessionStorage());
30+
$request->setSession($session);
31+
32+
$session->getFlashBag()->set('foo', 'bar');
33+
34+
$constraint = new SessionHasFlashMessage('foo', $flashMessages);
35+
$this->assertTrue($constraint->evaluate($request, '', true));
36+
}
37+
38+
#[DataProvider('provideMessages')]
39+
public function testNoMessage(string|array $flashMessages)
40+
{
41+
$request = new Request();
42+
$session = new Session(new MockArraySessionStorage());
43+
$request->setSession($session);
44+
45+
$constraint = new SessionHasFlashMessage('foo', $flashMessages);
46+
47+
$this->expectException(ExpectationFailedException::class);
48+
$this->expectExceptionMessage(\sprintf(
49+
'Failed asserting that session has flash message of type "foo" containing: %s.',
50+
\is_string($flashMessages) ? $flashMessages : implode(', ', $flashMessages),
51+
));
52+
53+
$constraint->evaluate($request);
54+
}
55+
56+
#[DataProvider('provideMessages')]
57+
public function testNoSession(string|array $flashMessages)
58+
{
59+
$request = new Request();
60+
61+
$constraint = new SessionHasFlashMessage('foo', $flashMessages);
62+
63+
$this->expectException(ExpectationFailedException::class);
64+
$this->expectExceptionMessage(\sprintf(
65+
'Failed asserting that session has flash message of type "foo" containing: %s, because the Request does not have a Session.',
66+
\is_string($flashMessages) ? $flashMessages : implode(', ', $flashMessages),
67+
));
68+
69+
$constraint->evaluate($request);
70+
}
71+
72+
#[DataProvider('provideMessages')]
73+
public function testNoFlashBag(string|array $flashMessages)
74+
{
75+
$request = new Request();
76+
$session = $this->createStub(SessionInterface::class);
77+
$request->setSession($session);
78+
79+
$constraint = new SessionHasFlashMessage('foo', $flashMessages);
80+
81+
$this->expectException(ExpectationFailedException::class);
82+
$this->expectExceptionMessage(\sprintf(
83+
'Failed asserting that session has flash message of type "foo" containing: %s, because the Session does not have a FlashBag',
84+
\is_string($flashMessages) ? $flashMessages : implode(', ', $flashMessages),
85+
));
86+
87+
$constraint->evaluate($request);
88+
}
89+
90+
public static function provideMessages(): iterable
91+
{
92+
yield ['bar'];
93+
yield [['bar', 'baz']];
94+
}
95+
}

0 commit comments

Comments
 (0)