Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afb1c04

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

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
44

55
use Psr\Log\LoggerInterface;
6+
use Symfony\Bundle\FullStack;
67
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
78
use Symfony\Component\Mailer\Mailer;
89
use Symfony\Component\Mailer\SentMessage;
@@ -71,9 +72,19 @@ public function testMailerAssertions()
7172
$client->request('GET', '/send_email');
7273

7374
$this->assertEmailCount(2);
74-
$this->assertEmailIsQueued($this->getMailerEvent(0));
75-
76-
$email = $this->getMailerMessage(0);
75+
$first = 0;
76+
$second = 1;
77+
if (!class_exists(FullStack::class)) {
78+
$this->assertQueuedEmailCount(2);
79+
$first = 1;
80+
$second = 3;
81+
$this->assertEmailIsQueued($this->getMailerEvent(0));
82+
$this->assertEmailIsQueued($this->getMailerEvent(2));
83+
}
84+
$this->assertEmailIsNotQueued($this->getMailerEvent($first));
85+
$this->assertEmailIsNotQueued($this->getMailerEvent($second));
86+
87+
$email = $this->getMailerMessage($first);
7788
$this->assertEmailHasHeader($email, 'To');
7889
$this->assertEmailHeaderSame($email, 'To', '[email protected]');
7990
$this->assertEmailHeaderNotSame($email, 'To', '[email protected]');
@@ -83,7 +94,7 @@ public function testMailerAssertions()
8394
$this->assertEmailHtmlBodyNotContains($email, 'Bar');
8495
$this->assertEmailAttachementCount($email, 1);
8596

86-
$email = $this->getMailerMessage(1);
97+
$email = $this->getMailerMessage($second);
8798
$this->assertEmailAddressContains($email, 'To', '[email protected]');
8899
$this->assertEmailAddressContains($email, 'To', '[email protected]');
89100
$this->assertEmailAddressContains($email, 'Reply-To', '[email protected]');

src/Symfony/Component/Mailer/Mailer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): void
5454
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
5555
}
5656
}
57-
$event = new MessageEvent($message, $envelope, $this->transport->getName());
57+
$event = new MessageEvent($message, $envelope, $this->transport->getName(), true);
5858
$this->dispatcher->dispatch($event);
5959
}
6060

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
}

src/Symfony/Component/Mailer/Transport/AbstractTransport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
6767
}
6868
}
6969

70-
$event = new MessageEvent($message, $envelope, $this->getName(), true);
70+
$event = new MessageEvent($message, $envelope, $this->getName());
7171
$this->dispatcher->dispatch($event);
7272
$envelope = $event->getEnvelope();
7373
if (!$envelope->getRecipients()) {

0 commit comments

Comments
 (0)