From 5dac9c1bd9e55ff7d59ea47af2d90fa722f09d89 Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Sat, 20 Jun 2020 23:57:08 +0200 Subject: [PATCH 1/6] 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 --- src/Codeception/Module/Symfony.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 7fcaa285..7ebaf7ef 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -498,6 +498,7 @@ public function seeEmailIsSent($expectedCount = null) } $realCount = $profile->getCollector('swiftmailer')->getMessageCount(); + $realCount += count($profile->getCollector('mailer')->getEvents()->getMessages()); if ($expectedCount === null) { $this->assertGreaterThan(0, $realCount); } else { From a295628795c25a94c417218df53512ac16147cba Mon Sep 17 00:00:00 2001 From: Gustavo Nieves <37160403+TavoNiievez@users.noreply.github.com> Date: Tue, 11 Aug 2020 12:45:44 -0500 Subject: [PATCH 2/6] Update Symfony.php --- src/Codeception/Module/Symfony.php | 31 +++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 7ebaf7ef..cdf89451 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -479,15 +479,27 @@ public function seeInCurrentRoute($routeName) * ``` * * @param null|int $expectedCount + * @param null|string $mailer */ - public function seeEmailIsSent($expectedCount = null) + public function seeEmailIsSent($expectedCount = null, $mailer = 'swiftmailer') { $profile = $this->getProfile(); if (!$profile) { $this->fail('Emails can\'t be tested without Profiler'); } - if (!$profile->hasCollector('swiftmailer')) { - $this->fail('Emails can\'t be tested without SwiftMailer connector'); + switch ($mailer) { + case 'swiftmailer': + if (!$profile->hasCollector('swiftmailer')) { + $this->fail('Emails can\'t be tested without SwiftMailer connector'); + } + break; + case 'symfony_mailer': + if (!$profile->hasCollector('mailer')) { + $this->fail('Emails can\'t be tested without Symfony Mailer connector'); + } + break; + default: + $this->fail('Invalid mailer argument. Allowed Options: "swiftmailer" or "symfony_mailer"'); } if (!is_int($expectedCount) && !is_null($expectedCount)) { @@ -497,8 +509,12 @@ public function seeEmailIsSent($expectedCount = null) )); } - $realCount = $profile->getCollector('swiftmailer')->getMessageCount(); - $realCount += count($profile->getCollector('mailer')->getEvents()->getMessages()); + if ($mailer === 'swiftmailer') { + $realCount = $profile->getCollector('swiftmailer')->getMessageCount(); + } else { + $realCount = count($profile->getCollector('mailer')->getEvents()->getMessages()); + } + if ($expectedCount === null) { $this->assertGreaterThan(0, $realCount); } else { @@ -634,6 +650,11 @@ protected function debugResponse($url) if ($messages) { $this->debugSection('Emails', $messages . ' sent'); } + } elseif ($profile->hasCollector('mailer')) { + $messages = count($profile->getCollector('mailer')->getEvents()->getMessages()); + if ($messages) { + $this->debugSection('Emails', $messages . ' sent'); + } } if ($profile->hasCollector('timer')) { $this->debugSection('Time', $profile->getCollector('timer')->getTime()); From 2f8467151b5228335cc167263a98b5e6386573b3 Mon Sep 17 00:00:00 2001 From: Gustavo Nieves <37160403+TavoNiievez@users.noreply.github.com> Date: Tue, 11 Aug 2020 13:50:48 -0500 Subject: [PATCH 3/6] Added a more descriptive fail comment --- src/Codeception/Module/Symfony.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index cdf89451..b1f8d85b 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -490,7 +490,10 @@ public function seeEmailIsSent($expectedCount = null, $mailer = 'swiftmailer') switch ($mailer) { case 'swiftmailer': if (!$profile->hasCollector('swiftmailer')) { - $this->fail('Emails can\'t be tested without SwiftMailer connector'); + $this->fail( + 'Emails can\'t be tested without SwiftMailer connector. + If you are using Symfony Mailer use "symfony_mailer" as second parameter' + ); } break; case 'symfony_mailer': From dfaf6f0a7dba0cae86650548cd9af8a75f6fb0e7 Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Wed, 12 Aug 2020 13:46:20 +0200 Subject: [PATCH 4/6] Update Symfony.php Adding `const`s for https://github.com/ThomasLandauer/module-symfony/pull/1 --- src/Codeception/Module/Symfony.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index b1f8d85b..172df25c 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -113,6 +113,9 @@ */ class Symfony extends Framework implements DoctrineProvider, PartedModule { + public const SWIFTMAILER = 'swiftmailer'; + public const SYMFONY_MAILER = 'symfony_mailer'; + private static $possibleKernelClasses = [ 'AppKernel', // Symfony Standard 'App\Kernel', // Symfony Flex From 80b7c3229d07dee168d5aad316cb1f44018e0e76 Mon Sep 17 00:00:00 2001 From: Gustavo Nieves <37160403+TavoNiievez@users.noreply.github.com> Date: Wed, 12 Aug 2020 10:27:46 -0500 Subject: [PATCH 5/6] mailer setting in module config --- src/Codeception/Module/Symfony.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 172df25c..8b34332b 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -36,6 +36,7 @@ * * debug: true - turn on/off debug mode * * 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) * * rebootable_client: 'true' - reboot client's kernel before each request + * * mailer: 'symfony_mailer' - choose the mailer used by your application * * #### Example (`functional.suite.yml`) - Symfony 4 Directory Structure * @@ -56,6 +57,7 @@ * * debug: true - turn on/off debug mode * * 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) * * rebootable_client: 'true' - reboot client's kernel before each request + * * mailer: 'swiftmailer' - choose the mailer used by your application * * #### Example (`functional.suite.yml`) - Symfony 3 Directory Structure * @@ -76,6 +78,7 @@ * * em_service: 'doctrine.orm.entity_manager' - use the stated EntityManager to pair with Doctrine Module. * * 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) * * rebootable_client: 'true' - reboot client's kernel before each request + * * mailer: 'swiftmailer' - choose the mailer used by your application * * ### Example (`functional.suite.yml`) - Symfony 2.x Directory Structure * @@ -135,6 +138,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule 'cache_router' => false, 'em_service' => 'doctrine.orm.entity_manager', 'rebootable_client' => true, + 'mailer' => self::SWIFTMAILER ]; /** @@ -482,30 +486,32 @@ public function seeInCurrentRoute($routeName) * ``` * * @param null|int $expectedCount - * @param null|string $mailer */ - public function seeEmailIsSent($expectedCount = null, $mailer = 'swiftmailer') + public function seeEmailIsSent($expectedCount = null) { $profile = $this->getProfile(); if (!$profile) { $this->fail('Emails can\'t be tested without Profiler'); } - switch ($mailer) { - case 'swiftmailer': + switch ($this->config['mailer']) { + case self::SWIFTMAILER: if (!$profile->hasCollector('swiftmailer')) { $this->fail( 'Emails can\'t be tested without SwiftMailer connector. - If you are using Symfony Mailer use "symfony_mailer" as second parameter' + If you are using Symfony Mailer define mailer: "symfony_mailer" in Symfony module config.' ); } break; - case 'symfony_mailer': + case self::SYMFONY_MAILER: if (!$profile->hasCollector('mailer')) { - $this->fail('Emails can\'t be tested without Symfony Mailer connector'); + $this->fail( + 'Emails can\'t be tested without Symfony Mailer connector. + If you are using Symfony Mailer define mailer: "swiftmailer" in Symfony module config.' + ); } break; default: - $this->fail('Invalid mailer argument. Allowed Options: "swiftmailer" or "symfony_mailer"'); + $this->fail('Invalid mailer config. Allowed Options: "swiftmailer" or "mailer"'); } if (!is_int($expectedCount) && !is_null($expectedCount)) { @@ -515,7 +521,7 @@ public function seeEmailIsSent($expectedCount = null, $mailer = 'swiftmailer') )); } - if ($mailer === 'swiftmailer') { + if ($this->config['mailer'] === self::SWIFTMAILER) { $realCount = $profile->getCollector('swiftmailer')->getMessageCount(); } else { $realCount = count($profile->getCollector('mailer')->getEvents()->getMessages()); From 70d5c657b6ad1c2b574365894584da772c2be691 Mon Sep 17 00:00:00 2001 From: Gustavo Nieves <37160403+TavoNiievez@users.noreply.github.com> Date: Wed, 12 Aug 2020 10:30:11 -0500 Subject: [PATCH 6/6] Fix little Typo --- src/Codeception/Module/Symfony.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 8b34332b..39756533 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -506,7 +506,7 @@ public function seeEmailIsSent($expectedCount = null) if (!$profile->hasCollector('mailer')) { $this->fail( 'Emails can\'t be tested without Symfony Mailer connector. - If you are using Symfony Mailer define mailer: "swiftmailer" in Symfony module config.' + If you are using SwiftMailer define mailer: "swiftmailer" in Symfony module config.' ); } break;