-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] Add SessionHasFlashMessage test constraint #60008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Test\Constraint; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface; | ||
|
||
final class SessionHasFlashMessage extends Constraint | ||
{ | ||
public function __construct( | ||
private readonly string $messageType, | ||
private readonly string $message, | ||
) { | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return \sprintf('has flash message of type "%s" with value "%s"', $this->messageType, $this->message); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
*/ | ||
protected function matches($request): bool | ||
Check failure on line 34 in src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php
|
||
{ | ||
if (!$request->hasSession()) { | ||
return false; | ||
} | ||
$session = $request->getSession(); | ||
if (!$session instanceof FlashBagAwareSessionInterface) { | ||
return false; | ||
} | ||
$flashbag = $session->getFlashBag(); | ||
|
||
return \in_array($this->message, $flashbag->peek($this->messageType), true); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
*/ | ||
protected function failureDescription($request): string | ||
Check failure on line 51 in src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php
|
||
{ | ||
$message = $this->toString(); | ||
|
||
if (!$request->hasSession()) { | ||
return $message.' because the Request does not have a Session'; | ||
} | ||
|
||
$session = $request->getSession(); | ||
if (!$session instanceof FlashBagAwareSessionInterface) { | ||
return $message.' because the Session does not have a FlashBag'; | ||
} | ||
|
||
return $message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Tests\Test\Constraint; | ||
|
||
use PHPUnit\Framework\ExpectationFailedException; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; | ||
use Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage; | ||
|
||
class SessionHasFlashMessageTest extends TestCase | ||
{ | ||
public function testSuccessfulCase() | ||
{ | ||
$request = new Request(); | ||
$session = new Session(new MockArraySessionStorage()); | ||
$request->setSession($session); | ||
|
||
$session->getFlashBag()->set('foo', 'bar'); | ||
|
||
$constraint = new SessionHasFlashMessage('foo', 'bar'); | ||
$this->assertTrue($constraint->evaluate($request, '', true)); | ||
} | ||
|
||
public function testNoMessage() | ||
{ | ||
$request = new Request(); | ||
$session = new Session(new MockArraySessionStorage()); | ||
$request->setSession($session); | ||
|
||
$constraint = new SessionHasFlashMessage('foo', 'bar'); | ||
|
||
$this->expectException(ExpectationFailedException::class); | ||
$this->expectExceptionMessage('has flash message of type "foo" with value "bar"'); | ||
|
||
$constraint->evaluate($request); | ||
} | ||
|
||
public function testNoSession() | ||
{ | ||
$request = new Request(); | ||
|
||
$constraint = new SessionHasFlashMessage('foo', 'bar'); | ||
|
||
$this->expectException(ExpectationFailedException::class); | ||
$this->expectExceptionMessage('has flash message of type "foo" with value "bar" because the Request does not have a Session'); | ||
|
||
$constraint->evaluate($request); | ||
} | ||
|
||
public function testNoFlashBag() | ||
{ | ||
$request = new Request(); | ||
$session = $this->createMock(SessionInterface::class); | ||
$request->setSession($session); | ||
|
||
$constraint = new SessionHasFlashMessage('foo', 'bar'); | ||
|
||
$this->expectException(ExpectationFailedException::class); | ||
$this->expectExceptionMessage('has flash message of type "foo" with value "bar" because the Session does not have a FlashBag'); | ||
|
||
$constraint->evaluate($request); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A message can be of any type (technically) https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php#L31
For the
matches()
part this should use the Constraint comparator.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A message is a string. The method
set
allows an array of strings. This constraint constructor looks good to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, referenced the wrong method. https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php#L26
https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php#L83 (
set()
method implementation).The
set()
method allows to set multiple messages for a type, but the type of a$message
is mixed.As it can also contain a Translatable object (Which I use in some projects), as long as there is a way to serialize the value, anything goes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, if a string is the common type for messages, nothing prevent from using anything else.
I found this thread where it is suggested to use a
Markup
object as message. Also, we can imagine that aTranslatable
object is used as well.@Pierstoval would you mind to relax the message type?