Skip to content

Commit cab80ca

Browse files
authored
Update seeEmailIsSent (#78)
1 parent 142b392 commit cab80ca

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
use function is_subclass_of;
7171
use function iterator_to_array;
7272
use function json_encode;
73-
use function print_r;
7473
use function serialize;
7574
use function sprintf;
7675
use function strlen;
@@ -578,41 +577,26 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
578577
*/
579578
public function seeEmailIsSent(?int $expectedCount = null): void
580579
{
581-
if (($profile = $this->getProfile()) === null) {
582-
$this->fail("Emails can't be tested without Profiler");
583-
}
584-
580+
$realCount = 0;
585581
$mailer = $this->config['mailer'];
586-
if ($mailer === self::SYMFONY_MAILER) {
587-
$mailer = 'mailer';
588-
}
589-
590-
if (!$profile->hasCollector($mailer)) {
582+
if ($mailer === self::SWIFTMAILER) {
583+
$mailCollector = $this->grabCollector('swiftmailer', __FUNCTION__);
584+
$realCount = $mailCollector->getMessageCount();
585+
} elseif ($mailer === self::SYMFONY_MAILER) {
586+
$mailCollector = $this->grabCollector('mailer', __FUNCTION__);
587+
$realCount = count($mailCollector->getEvents()->getMessages());
588+
} else {
591589
$this->fail(
592590
"Emails can't be tested without Mailer service connector.
593591
Set your mailer service in `functional.suite.yml`: `mailer: swiftmailer`
594592
(Or `mailer: symfony_mailer` for Symfony Mailer)."
595593
);
596594
}
597595

598-
if (!is_int($expectedCount) && !is_null($expectedCount)) {
599-
$this->fail(sprintf(
600-
'The required number of emails must be either an integer or null. "%s" was provided.',
601-
print_r($expectedCount, true)
602-
));
603-
}
604-
605-
$mailCollector = $profile->getCollector($mailer);
606-
if ($mailer === self::SWIFTMAILER) {
607-
$realCount = $mailCollector->getMessageCount();
608-
} else {
609-
$realCount = count($mailCollector->getEvents()->getMessages());
610-
}
611-
612596
if ($expectedCount !== null) {
613597
$this->assertEquals($expectedCount, $realCount, sprintf(
614598
'Expected number of sent emails was %d, but in reality %d %s sent.',
615-
$expectedCount, $realCount, $realCount === 2 ? 'was' : 'were'
599+
$expectedCount, $realCount, $realCount === 1 ? 'was' : 'were'
616600
));
617601
return;
618602
}

0 commit comments

Comments
 (0)