Skip to content

Commit 7a2ecf8

Browse files
ThomasLandauerGustavo Nieves
and
Gustavo Nieves
authored
Fix seeEmailIsSent
* Update Symfony.php Fixing `seeEmailIsSent()` for [Symfony Mailer](https://symfony.com/doc/master/mailer.html) How did I figure it out (=Symfony internals): [`mailer_debug.php`](https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Resources/config/mailer_debug.php) sends the [`MessageDataCollector`](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Mailer/DataCollector/MessageDataCollector.php) to [`mailer.html.twig`](https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/mailer.html.twig#L9), which shows that the number of `events.messages` is the thing to look for. I'm just adding this number to the number returned by the SwiftMailer Collector. Limitations (=TODO): * I have both SwiftMailer and Symfony Mailer installed - don't know what happens if you only have one (or none) * I didn't add any tests - don't know how to do that * Update Symfony.php * Added a more descriptive fail comment * Update Symfony.php Adding `const`s for ThomasLandauer#1 * mailer setting in module config * Fix little Typo Co-authored-by: Gustavo Nieves <[email protected]>
1 parent e60d426 commit 7a2ecf8

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

Diff for: src/Codeception/Module/Symfony.php

+37-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* * debug: true - turn on/off debug mode
3737
* * 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)
3838
* * rebootable_client: 'true' - reboot client's kernel before each request
39+
* * mailer: 'symfony_mailer' - choose the mailer used by your application
3940
*
4041
* #### Example (`functional.suite.yml`) - Symfony 4 Directory Structure
4142
*
@@ -56,6 +57,7 @@
5657
* * debug: true - turn on/off debug mode
5758
* * 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)
5859
* * rebootable_client: 'true' - reboot client's kernel before each request
60+
* * mailer: 'swiftmailer' - choose the mailer used by your application
5961
*
6062
* #### Example (`functional.suite.yml`) - Symfony 3 Directory Structure
6163
*
@@ -76,6 +78,7 @@
7678
* * em_service: 'doctrine.orm.entity_manager' - use the stated EntityManager to pair with Doctrine Module.
7779
* * 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)
7880
* * rebootable_client: 'true' - reboot client's kernel before each request
81+
* * mailer: 'swiftmailer' - choose the mailer used by your application
7982
*
8083
* ### Example (`functional.suite.yml`) - Symfony 2.x Directory Structure
8184
*
@@ -113,6 +116,9 @@
113116
*/
114117
class Symfony extends Framework implements DoctrineProvider, PartedModule
115118
{
119+
public const SWIFTMAILER = 'swiftmailer';
120+
public const SYMFONY_MAILER = 'symfony_mailer';
121+
116122
private static $possibleKernelClasses = [
117123
'AppKernel', // Symfony Standard
118124
'App\Kernel', // Symfony Flex
@@ -132,6 +138,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
132138
'cache_router' => false,
133139
'em_service' => 'doctrine.orm.entity_manager',
134140
'rebootable_client' => true,
141+
'mailer' => self::SWIFTMAILER
135142
];
136143

137144
/**
@@ -486,8 +493,25 @@ public function seeEmailIsSent($expectedCount = null)
486493
if (!$profile) {
487494
$this->fail('Emails can\'t be tested without Profiler');
488495
}
489-
if (!$profile->hasCollector('swiftmailer')) {
490-
$this->fail('Emails can\'t be tested without SwiftMailer connector');
496+
switch ($this->config['mailer']) {
497+
case self::SWIFTMAILER:
498+
if (!$profile->hasCollector('swiftmailer')) {
499+
$this->fail(
500+
'Emails can\'t be tested without SwiftMailer connector.
501+
If you are using Symfony Mailer define mailer: "symfony_mailer" in Symfony module config.'
502+
);
503+
}
504+
break;
505+
case self::SYMFONY_MAILER:
506+
if (!$profile->hasCollector('mailer')) {
507+
$this->fail(
508+
'Emails can\'t be tested without Symfony Mailer connector.
509+
If you are using SwiftMailer define mailer: "swiftmailer" in Symfony module config.'
510+
);
511+
}
512+
break;
513+
default:
514+
$this->fail('Invalid mailer config. Allowed Options: "swiftmailer" or "mailer"');
491515
}
492516

493517
if (!is_int($expectedCount) && !is_null($expectedCount)) {
@@ -497,7 +521,12 @@ public function seeEmailIsSent($expectedCount = null)
497521
));
498522
}
499523

500-
$realCount = $profile->getCollector('swiftmailer')->getMessageCount();
524+
if ($this->config['mailer'] === self::SWIFTMAILER) {
525+
$realCount = $profile->getCollector('swiftmailer')->getMessageCount();
526+
} else {
527+
$realCount = count($profile->getCollector('mailer')->getEvents()->getMessages());
528+
}
529+
501530
if ($expectedCount === null) {
502531
$this->assertGreaterThan(0, $realCount);
503532
} else {
@@ -633,6 +662,11 @@ protected function debugResponse($url)
633662
if ($messages) {
634663
$this->debugSection('Emails', $messages . ' sent');
635664
}
665+
} elseif ($profile->hasCollector('mailer')) {
666+
$messages = count($profile->getCollector('mailer')->getEvents()->getMessages());
667+
if ($messages) {
668+
$this->debugSection('Emails', $messages . ' sent');
669+
}
636670
}
637671
if ($profile->hasCollector('timer')) {
638672
$this->debugSection('Time', $profile->getCollector('timer')->getTime());

0 commit comments

Comments
 (0)