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

Conversation

Pierstoval
Copy link
Contributor

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
License MIT

Eases the checks on the Session, to avoid the user having to do the checks themselves.

Before:

$request = $client->getRequest();
self::assertTrue($request->hasSession());
$session = $request->getSession();
self::assertInstanceOf(FlashBagAwareSessionInterface::class, $session);
$flashbag = $session->getFlashBag();
self::assertContains($message, $flashbag->peek($messageType));

After:

self::assertSessionHasFlashMessage($messageType, $message);

{
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?

Copy link
Contributor

@Spomky Spomky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks.
No specific remarks for this PR.
👍

{
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 is a string. The method set allows an array of strings. This constraint constructor looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.