Skip to content
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

Update MailerAssertionsTrait.php: Adding Mailpit #204

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
13 changes: 9 additions & 4 deletions src/Codeception/Module/Symfony/MailerAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function assertQueuedEmailCount(int $count, ?string $transport = null, st
/**
* Checks that no email was sent.
* The check is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means:
* If your app performs an HTTP redirect, you need to suppress it using [stopFollowingRedirects()](https://codeception.com/docs/modules/Symfony#stopFollowingRedirects) first; otherwise this check will *always* pass.
* If your app performs an HTTP redirect, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first; otherwise this check will *always* pass.
*/
public function dontSeeEmailIsSent(): void
{
Expand All @@ -60,7 +60,7 @@ public function dontSeeEmailIsSent(): void
/**
* Returns the last sent email.
* The function is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means:
* If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](https://codeception.com/docs/modules/Symfony#stopFollowingRedirects) first.
* If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first.
* See also: [grabSentEmails()](https://codeception.com/docs/modules/Symfony#grabSentEmails)
*
* ```php
Expand All @@ -82,7 +82,7 @@ public function grabLastSentEmail(): ?Email
/**
* Returns an array of all sent emails.
* The function is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means:
* If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](https://codeception.com/docs/modules/Symfony#stopFollowingRedirects) first.
* If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first.
* See also: [grabLastSentEmail()](https://codeception.com/docs/modules/Symfony#grabLastSentEmail)
*
* ```php
Expand All @@ -100,7 +100,12 @@ public function grabSentEmails(): array
/**
* Checks if the given number of emails was sent (default `$expectedCount`: 1).
* The check is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means:
* If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](https://codeception.com/docs/modules/Symfony#stopFollowingRedirects) first.
* If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first.
*
* Limitation:
* If your mail is sent in a Symfony console command and you start that command in your test with [$I->runShellCommand()](https://codeception.com/docs/modules/Cli#runShellCommand),
Copy link
Member

Choose a reason for hiding this comment

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

please clarify if this problem is also present in runSymfonyConsoleCommand, because when reading it seems to be exclusive of runShellCommand.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if I tried. But the mail assertions are working by looking at the web debug toolbar, aren't they?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't try runSymfonyConsoleCommand(). Is there a way to pipe input to the command?
Right now, I'm doing:

$I->runShellCommand('printf "foobar" | php bin/console app:my_command --env=test');

Copy link
Contributor

Choose a reason for hiding this comment

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

@ThomasLandauer yes, you can use the third parameter $consoleInputs for that. So the equivalent to the above would be

$I->runSymfonyConsoleCommand('app:my_command', ['--env' => 'test'], ['foobar']);

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, then the test executions hangs at this line - looks like the console is waiting for something. I didn't look into it further - are you sure that this works?

Copy link
Contributor

@burned42 burned42 Mar 10, 2025

Choose a reason for hiding this comment

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

@ThomasLandauer I tested it before writing that comment just to be sure, so yes, it does work in general. Maybe we're talking about different things, so I created Codeception/symfony-module-tests#28 to demonstrate what I'm talking about.

Copy link
Member Author

Choose a reason for hiding this comment

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

The test is a cool idea!
My command doesn't ask questions (i.e. doesn't wait for input), it reads input from STDIN:

$fread = fread(STDIN, $length);

Copy link
Contributor

Choose a reason for hiding this comment

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

@ThomasLandauer oh, I see. I didn't have such a use case yet, so I don't know, but the console input parameter might indeed not work with STDIN then.

* Codeception will not notice it.
* As a more professional alternative, we recommend Mailpit, which also lets you test the content of the mail.
*
* ```php
* <?php
Expand Down
Loading