Skip to content

Commit 00156b3

Browse files
authored
seeEmailIsSent logic simplified (#35)
1 parent 595f4e5 commit 00156b3

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -465,64 +465,54 @@ public function seeInCurrentRoute($routeName)
465465
* ``` php
466466
* <?php
467467
* $I->seeEmailIsSent(2);
468-
* ?>
469468
* ```
470469
*
471-
* @param null|int $expectedCount
470+
* @param int|null $expectedCount
472471
*/
473472
public function seeEmailIsSent($expectedCount = null)
474473
{
475-
$profile = $this->getProfile();
476-
if (!$profile) {
477-
$this->fail('Emails can\'t be tested without Profiler');
474+
if (!$profile = $this->getProfile()) {
475+
$this->fail("Emails can't be tested without Profiler");
476+
return;
478477
}
479-
switch ($this->config['mailer']) {
480-
case self::SWIFTMAILER:
481-
if (!$profile->hasCollector('swiftmailer')) {
482-
$this->fail(
483-
"Emails can't be tested without SwiftMailer connector.\nIf you are using Symfony Mailer, set this in your `functional.suite.yml`: `mailer: 'symfony_mailer'`"
484-
);
485-
}
486-
break;
487-
case self::SYMFONY_MAILER:
488-
if (!$profile->hasCollector('mailer')) {
489-
$this->fail(
490-
'Emails can\'t be tested without Symfony Mailer connector.
491-
If you are using SwiftMailer define mailer: "swiftmailer" in Symfony module config.'
492-
);
493-
}
494-
break;
495-
default:
496-
$this->fail('Invalid mailer config. Allowed Options: "swiftmailer" or "mailer"');
478+
479+
$mailer = $this->config['mailer'];
480+
if ($mailer === self::SYMFONY_MAILER) {
481+
$mailer = 'mailer';
482+
}
483+
484+
if (!$profile->hasCollector($mailer)) {
485+
$this->fail(
486+
"Emails can't be tested without Mailer service connector.
487+
Set your mailer service in `functional.suite.yml`: `mailer: swiftmailer`
488+
(Or `mailer: symfony_mailer` for Symfony Mailer)."
489+
);
490+
return;
497491
}
498492

499493
if (!is_int($expectedCount) && !is_null($expectedCount)) {
500494
$this->fail(sprintf(
501495
'The required number of emails must be either an integer or null. "%s" was provided.',
502496
print_r($expectedCount, true)
503497
));
498+
return;
504499
}
505500

506-
if ($this->config['mailer'] === self::SWIFTMAILER) {
507-
$realCount = $profile->getCollector('swiftmailer')->getMessageCount();
501+
$mailCollector = $profile->getCollector($mailer);
502+
if ($mailer === self::SWIFTMAILER) {
503+
$realCount = $mailCollector->getMessageCount();
508504
} else {
509-
$realCount = count($profile->getCollector('mailer')->getEvents()->getMessages());
505+
$realCount = count($mailCollector->getEvents()->getMessages());
510506
}
511507

512-
if ($expectedCount === null) {
513-
$this->assertGreaterThan(0, $realCount);
514-
} else {
515-
$this->assertEquals(
516-
$expectedCount,
517-
$realCount,
518-
sprintf(
519-
'Expected number of sent emails was %d, but in reality %d %s sent.',
520-
$expectedCount,
521-
$realCount,
522-
$realCount === 2 ? 'was' : 'were'
523-
)
524-
);
508+
if ($expectedCount) {
509+
$this->assertEquals($expectedCount, $realCount, sprintf(
510+
'Expected number of sent emails was %d, but in reality %d %s sent.',
511+
$expectedCount, $realCount, $realCount === 2 ? 'was' : 'were'
512+
));
513+
return;
525514
}
515+
$this->assertGreaterThan(0, $realCount);
526516
}
527517

528518
/**

0 commit comments

Comments
 (0)