Skip to content

Simplify seeEmailIsSent #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 29 additions & 39 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,64 +485,54 @@ public function seeInCurrentRoute($routeName)
* ``` php
* <?php
* $I->seeEmailIsSent(2);
* ?>
* ```
*
* @param null|int $expectedCount
* @param int|null $expectedCount
*/
public function seeEmailIsSent($expectedCount = null)
{
$profile = $this->getProfile();
if (!$profile) {
$this->fail('Emails can\'t be tested without Profiler');
if (!$profile = $this->getProfile()) {
$this->fail("Emails can't be tested without Profiler");
return;
}
switch ($this->config['mailer']) {
case self::SWIFTMAILER:
if (!$profile->hasCollector('swiftmailer')) {
$this->fail(
"Emails can't be tested without SwiftMailer connector.\nIf you are using Symfony Mailer, set this in your `functional.suite.yml`: `mailer: 'symfony_mailer'`"
);
}
break;
case self::SYMFONY_MAILER:
if (!$profile->hasCollector('mailer')) {
$this->fail(
'Emails can\'t be tested without Symfony Mailer connector.
If you are using SwiftMailer define mailer: "swiftmailer" in Symfony module config.'
);
}
break;
default:
$this->fail('Invalid mailer config. Allowed Options: "swiftmailer" or "mailer"');

$mailer = $this->config['mailer'];
if ($mailer === self::SYMFONY_MAILER) {
$mailer = 'mailer';
}

if (!$profile->hasCollector($mailer)) {
$this->fail(
"Emails can't be tested without Mailer service connector.
Set your mailer service in `functional.suite.yml`: `mailer: swiftmailer`
(Or `mailer: symfony_mailer` for Symfony Mailer)."
);
return;
}

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

if ($this->config['mailer'] === self::SWIFTMAILER) {
$realCount = $profile->getCollector('swiftmailer')->getMessageCount();
$mailCollector = $profile->getCollector($mailer);
if ($mailer === self::SWIFTMAILER) {
$realCount = $mailCollector->getMessageCount();
} else {
$realCount = count($profile->getCollector('mailer')->getEvents()->getMessages());
$realCount = count($mailCollector->getEvents()->getMessages());
}

if ($expectedCount === null) {
$this->assertGreaterThan(0, $realCount);
} else {
$this->assertEquals(
$expectedCount,
$realCount,
sprintf(
'Expected number of sent emails was %d, but in reality %d %s sent.',
$expectedCount,
$realCount,
$realCount === 2 ? 'was' : 'were'
)
);
if ($expectedCount) {
$this->assertEquals($expectedCount, $realCount, sprintf(
'Expected number of sent emails was %d, but in reality %d %s sent.',
$expectedCount, $realCount, $realCount === 2 ? 'was' : 'were'
));
return;
}
$this->assertGreaterThan(0, $realCount);
}

/**
Expand Down