Skip to content

Commit 097c66c

Browse files
authored
Refactor email assertions (#101)
1 parent 7a16f96 commit 097c66c

File tree

2 files changed

+33
-44
lines changed

2 files changed

+33
-44
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\HttpKernel\Kernel;
3737
use Symfony\Component\HttpKernel\Profiler\Profile;
3838
use Symfony\Component\HttpKernel\Profiler\Profiler;
39+
use Symfony\Component\Mailer\DataCollector\MessageDataCollector;
3940
use Symfony\Component\Routing\Route;
4041
use Symfony\Component\Routing\RouterInterface;
4142
use Symfony\Component\VarDumper\Cloner\Data;
@@ -74,7 +75,6 @@
7475
* * debug: true - turn on/off debug mode
7576
* * cache_router: 'false' - enable router caching between tests in order to [increase performance](http://lakion.com/blog/how-did-we-speed-up-sylius-behat-suite-with-blackfire)
7677
* * rebootable_client: 'true' - reboot client's kernel before each request
77-
* * mailer: 'symfony_mailer' - choose the mailer used by your application
7878
*
7979
* #### Example (`functional.suite.yml`) - Symfony 4 Directory Structure
8080
*
@@ -95,7 +95,6 @@
9595
* * debug: true - turn on/off debug mode
9696
* * cache_router: 'false' - enable router caching between tests in order to [increase performance](http://lakion.com/blog/how-did-we-speed-up-sylius-behat-suite-with-blackfire)
9797
* * rebootable_client: 'true' - reboot client's kernel before each request
98-
* * mailer: 'swiftmailer' - choose the mailer used by your application
9998
*
10099
* #### Example (`functional.suite.yml`) - Symfony 3 Directory Structure
101100
*
@@ -158,10 +157,6 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
158157
SessionAssertionsTrait
159158
;
160159

161-
private const SWIFTMAILER = 'swiftmailer';
162-
163-
private const SYMFONY_MAILER = 'symfony_mailer';
164-
165160
private static $possibleKernelClasses = [
166161
'AppKernel', // Symfony Standard
167162
'App\Kernel', // Symfony Flex
@@ -181,7 +176,6 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
181176
'cache_router' => false,
182177
'em_service' => 'doctrine.orm.entity_manager',
183178
'rebootable_client' => true,
184-
'mailer' => self::SWIFTMAILER,
185179
'guard' => false
186180
];
187181

@@ -462,12 +456,10 @@ protected function debugResponse($url): void
462456
$this->debugSection('User', 'Anonymous');
463457
}
464458
}
465-
if ($profile->hasCollector('swiftmailer')) {
466-
$emails = $profile->getCollector('swiftmailer')->getMessageCount();
467-
} elseif ($profile->hasCollector('mailer')) {
468-
$emails = count($profile->getCollector('mailer')->getEvents()->getMessages());
469-
}
470-
if (isset($emails)) {
459+
if ($profile->hasCollector('mailer')) {
460+
/** @var MessageDataCollector $mailerCollector */
461+
$mailerCollector = $profile->getCollector('mailer');
462+
$emails = count($mailerCollector->getEvents()->getMessages());
471463
$this->debugSection('Emails', $emails . ' sent');
472464
}
473465
if ($profile->hasCollector('time')) {

src/Codeception/Module/Symfony/MailerAssertionsTrait.php

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,55 @@
33
declare(strict_types=1);
44

55
namespace Codeception\Module\Symfony;
6-
use function count;
7-
use function sprintf;
6+
7+
use Symfony\Component\Mailer\Event\MessageEvents;
8+
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
9+
use Symfony\Component\Mailer\Test\Constraint as MailerConstraint;
810

911
trait MailerAssertionsTrait
1012
{
1113
/**
12-
* Checks that no email was sent. This is an alias for seeEmailIsSent(0).
14+
* Checks that no email was sent.
1315
*/
1416
public function dontSeeEmailIsSent(): void
1517
{
16-
$this->seeEmailIsSent(0);
18+
$this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount(0));
1719
}
1820

1921
/**
2022
* Checks if the desired number of emails was sent.
21-
* If no argument is provided then at least one email must be sent to satisfy the check.
22-
* The email is checked using Symfony's profiler, which means:
23+
* Asserts that 1 email was sent by default, specify the `expectedCount` parameter to modify it.
24+
* The email is checked using Symfony message logger, which means:
2325
* * If your app performs a redirect after sending the email, you need to suppress this using REST Module's [stopFollowingRedirects](https://codeception.com/docs/modules/REST#stopFollowingRedirects)
24-
* * If the email is sent by a Symfony Console Command, Codeception cannot detect it yet.
2526
*
26-
* ``` php
27+
* ```php
2728
* <?php
2829
* $I->seeEmailIsSent(2);
2930
* ```
3031
*
31-
* @param int|null $expectedCount
32+
* @param int $expectedCount The expected number of emails sent
3233
*/
33-
public function seeEmailIsSent(?int $expectedCount = null): void
34+
public function seeEmailIsSent(int $expectedCount = 1): void
35+
{
36+
$this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount($expectedCount));
37+
}
38+
39+
protected function getMessageMailerEvents(): MessageEvents
3440
{
35-
$realCount = 0;
36-
$mailer = $this->config['mailer'];
37-
if ($mailer === self::SWIFTMAILER) {
38-
$mailCollector = $this->grabCollector('swiftmailer', __FUNCTION__);
39-
$realCount = $mailCollector->getMessageCount();
40-
} elseif ($mailer === self::SYMFONY_MAILER) {
41-
$mailCollector = $this->grabCollector('mailer', __FUNCTION__);
42-
$realCount = count($mailCollector->getEvents()->getMessages());
43-
} else {
44-
$this->fail(
45-
"Emails can't be tested without Mailer service connector.
46-
Set your mailer service in `functional.suite.yml`: `mailer: swiftmailer`
47-
(Or `mailer: symfony_mailer` for Symfony Mailer)."
48-
);
41+
$container = $this->_getContainer();
42+
43+
if ($container->has('mailer.message_logger_listener')) {
44+
/** @var MessageLoggerListener $messageLogger */
45+
$messageLogger = $container->get('mailer.message_logger_listener');
46+
return $messageLogger->getEvents();
4947
}
5048

51-
if ($expectedCount !== null) {
52-
$this->assertEquals($expectedCount, $realCount, sprintf(
53-
'Expected number of sent emails was %d, but in reality %d %s sent.',
54-
$expectedCount, $realCount, $realCount === 1 ? 'was' : 'were'
55-
));
56-
return;
49+
if ($container->has('mailer.logger_message_listener')) {
50+
/** @var MessageLoggerListener $messageLogger */
51+
$messageLogger = $container->get('mailer.logger_message_listener');
52+
return $messageLogger->getEvents();
5753
}
58-
$this->assertGreaterThan(0, $realCount);
54+
55+
$this->fail("Emails can't be tested without Symfony Mailer service.");
5956
}
6057
}

0 commit comments

Comments
 (0)