Skip to content

Commit 9c52178

Browse files
mbohalDavertMik
authored andcommitted
Symfony test email count (#5059)
* Improve testing sent emails in Symfony * fixup! Improve testing sent emails in Symfony * fixup! fixup! Improve testing sent emails in Symfony
1 parent 79dec7c commit 9c52178

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,18 @@ public function seeInCurrentRoute($routeName)
445445
}
446446

447447
/**
448-
* Checks if any email were sent by last request
448+
* Checks if the desired number of emails was sent.
449+
* If no argument is provided then at least one email must be sent to satisfy the check.
449450
*
450-
* @throws \LogicException
451+
* ``` php
452+
* <?php
453+
* $I->seeEmailIsSent(2);
454+
* ?>
455+
* ```
456+
*
457+
* @param null|int $expectedCount
451458
*/
452-
public function seeEmailIsSent()
459+
public function seeEmailIsSent($expectedCount = null)
453460
{
454461
$profile = $this->getProfile();
455462
if (!$profile) {
@@ -459,7 +466,38 @@ public function seeEmailIsSent()
459466
$this->fail('Emails can\'t be tested without SwiftMailer connector');
460467
}
461468

462-
$this->assertGreaterThan(0, $profile->getCollector('swiftmailer')->getMessageCount());
469+
if (!is_int($expectedCount) && !is_null($expectedCount)) {
470+
$this->fail(sprintf(
471+
'The required number of emails must be either an integer or null. "%s" was provided.',
472+
print_r($expectedCount, true)
473+
));
474+
}
475+
476+
$realCount = $profile->getCollector('swiftmailer')->getMessageCount();
477+
if ($expectedCount === null) {
478+
$this->assertGreaterThan(0, $realCount);
479+
} else {
480+
$this->assertEquals(
481+
$expectedCount,
482+
$realCount,
483+
sprintf(
484+
'Expected number of sent emails was %d, but in reality %d %s sent.',
485+
$expectedCount,
486+
$realCount,
487+
$realCount === 2 ? 'was' : 'were'
488+
)
489+
);
490+
}
491+
}
492+
493+
/**
494+
* Checks that no email was sent. This is an alias for seeEmailIsSent(0).
495+
*
496+
* @part email
497+
*/
498+
public function dontSeeEmailIsSent()
499+
{
500+
$this->seeEmailIsSent(0);
463501
}
464502

465503
/**

0 commit comments

Comments
 (0)