Skip to content

Commit 28275f1

Browse files
committed
[Mailer] added a way to test the number of queued emails
1 parent 940eabb commit 28275f1

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/Test/MailerAssertionsTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public static function assertEmailCount(int $count, string $transport = null, st
2525
self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport), $message);
2626
}
2727

28+
public static function assertQueuedEmailCount(int $count, string $transport = null, string $message = ''): void
29+
{
30+
self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport, true), $message);
31+
}
32+
2833
public static function assertEmailIsQueued(MessageEvent $event, string $message = ''): void
2934
{
3035
self::assertThat($event, new MailerConstraint\EmailIsQueued(), $message);

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ public function testMailerAssertions()
7070
$client = $this->createClient(['test_case' => 'Mailer', 'root_config' => 'config.yml', 'debug' => true]);
7171
$client->request('GET', '/send_email');
7272

73+
$this->assertQueuedEmailCount(2);
7374
$this->assertEmailCount(2);
74-
$this->assertEmailIsQueued($this->getMailerEvent(0));
75+
$this->assertEmailIsQueued($this->getMailerEvent(1));
7576

76-
$email = $this->getMailerMessage(0);
77+
$email = $this->getMailerMessage(1);
7778
$this->assertEmailHasHeader($email, 'To');
7879
$this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
7980
$this->assertEmailHeaderNotSame($email, 'To', 'helene@symfony.com');
@@ -83,7 +84,7 @@ public function testMailerAssertions()
8384
$this->assertEmailHtmlBodyNotContains($email, 'Bar');
8485
$this->assertEmailAttachementCount($email, 1);
8586

86-
$email = $this->getMailerMessage(1);
87+
$email = $this->getMailerMessage(3);
8788
$this->assertEmailAddressContains($email, 'To', 'fabien@symfony.com');
8889
$this->assertEmailAddressContains($email, 'To', 'thomas@symfony.com');
8990
$this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com');

src/Symfony/Component/Mailer/Test/Constraint/EmailCount.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ final class EmailCount extends Constraint
1818
{
1919
private $expectedValue;
2020
private $transport;
21+
private $queued;
2122

22-
public function __construct(int $expectedValue, string $transport = null)
23+
public function __construct(int $expectedValue, string $transport = null, bool $queued = false)
2324
{
2425
$this->expectedValue = $expectedValue;
2526
$this->transport = $transport;
27+
$this->queued = $queued;
2628
}
2729

2830
/**
2931
* {@inheritdoc}
3032
*/
3133
public function toString(): string
3234
{
33-
return sprintf('%shas sent "%d" emails', $this->transport ? $this->transport.' ' : '', $this->expectedValue);
35+
return sprintf('%shas %s "%d" emails', $this->transport ? $this->transport.' ' : '', $this->queued ? 'queued' : 'sent', $this->expectedValue);
3436
}
3537

3638
/**
@@ -40,7 +42,7 @@ public function toString(): string
4042
*/
4143
protected function matches($events): bool
4244
{
43-
return $this->expectedValue === \count($events->getEvents($this->transport));
45+
return $this->expectedValue === $this->countEmails($events);
4446
}
4547

4648
/**
@@ -50,6 +52,22 @@ protected function matches($events): bool
5052
*/
5153
protected function failureDescription($events): string
5254
{
53-
return sprintf('the Transport %s (%d sent)', $this->toString(), \count($events->getEvents($this->transport)));
55+
return sprintf('the Transport %s (%d %s)', $this->toString(), $this->countEmails($events), $this->queued ? 'queued' : 'sent');
56+
}
57+
58+
private function countEmails(MessageEvents $events): int
59+
{
60+
$count = 0;
61+
foreach ($events->getEvents($this->transport) as $event) {
62+
if (
63+
($this->queued && $event->isQueued())
64+
||
65+
(!$this->queued && !$event->isQueued())
66+
) {
67+
++$count;
68+
}
69+
}
70+
71+
return $count;
5472
}
5573
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy