Skip to content

Commit f8f74b7

Browse files
committed
[Mailer] added a way to test the number of queued emails
1 parent 1546daf commit f8f74b7

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

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

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
}

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)