Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ public static function assertRouteSame(string $expectedRoute, array $parameters
self::assertThat(self::getRequest(), $constraint, $message);
}

public static function assertSessionHasFlashMessage(string $messageType, string $message = ''): void
{
static::assertThat(self::getRequest(), new ResponseConstraint\SessionHasFlashMessage($messageType, $message));
}

public static function assertThatForResponse(Constraint $constraint, string $message = ''): void
{
try {
Expand Down
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,
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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

    /**
     * Adds a flash message for the given type.
     */
    public function add(string $type, mixed $message): void;

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.

Copy link
Contributor

@Spomky Spomky Apr 18, 2025

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 a Translatable object is used as well.
@Pierstoval would you mind to relax the message type?

) {
}

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

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php:34:32: ParamNameMismatch: Argument 1 of Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage::matches has wrong name $request, expecting $other as defined by PHPUnit\Framework\Constraint\Constraint::matches (see https://psalm.dev/230)

Check failure on line 34 in src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificImplementedParamType

src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php:34:32: MoreSpecificImplementedParamType: Argument 1 of Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage::matches has the more specific type 'Symfony\Component\HttpFoundation\Request', expecting 'mixed' as defined by PHPUnit\Framework\Constraint\Constraint::matches (see https://psalm.dev/140)

Check failure on line 34 in src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php:34:32: ParamNameMismatch: Argument 1 of Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage::matches has wrong name $request, expecting $other as defined by PHPUnit\Framework\Constraint\Constraint::matches (see https://psalm.dev/230)

Check failure on line 34 in src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificImplementedParamType

src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php:34:32: MoreSpecificImplementedParamType: Argument 1 of Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage::matches has the more specific type 'Symfony\Component\HttpFoundation\Request', expecting 'mixed' as defined by PHPUnit\Framework\Constraint\Constraint::matches (see https://psalm.dev/140)
{
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

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php:51:43: ParamNameMismatch: Argument 1 of Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage::failureDescription has wrong name $request, expecting $other as defined by PHPUnit\Framework\Constraint\Constraint::failureDescription (see https://psalm.dev/230)

Check failure on line 51 in src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificImplementedParamType

src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php:51:43: MoreSpecificImplementedParamType: Argument 1 of Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage::failureDescription has the more specific type 'Symfony\Component\HttpFoundation\Request', expecting 'mixed' as defined by PHPUnit\Framework\Constraint\Constraint::failureDescription (see https://psalm.dev/140)

Check failure on line 51 in src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php:51:43: ParamNameMismatch: Argument 1 of Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage::failureDescription has wrong name $request, expecting $other as defined by PHPUnit\Framework\Constraint\Constraint::failureDescription (see https://psalm.dev/230)

Check failure on line 51 in src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificImplementedParamType

src/Symfony/Component/HttpFoundation/Test/Constraint/SessionHasFlashMessage.php:51:43: MoreSpecificImplementedParamType: Argument 1 of Symfony\Component\HttpFoundation\Test\Constraint\SessionHasFlashMessage::failureDescription has the more specific type 'Symfony\Component\HttpFoundation\Request', expecting 'mixed' as defined by PHPUnit\Framework\Constraint\Constraint::failureDescription (see https://psalm.dev/140)
{
$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);
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.