| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Mailer\Test\Constraint;
|
|
|
13 |
|
|
|
14 |
use PHPUnit\Framework\Constraint\Constraint;
|
|
|
15 |
use Symfony\Component\Mailer\Event\MessageEvents;
|
|
|
16 |
|
|
|
17 |
final class EmailCount extends Constraint
|
|
|
18 |
{
|
|
|
19 |
private int $expectedValue;
|
|
|
20 |
private ?string $transport;
|
|
|
21 |
private bool $queued;
|
|
|
22 |
|
|
|
23 |
public function __construct(int $expectedValue, string $transport = null, bool $queued = false)
|
|
|
24 |
{
|
|
|
25 |
$this->expectedValue = $expectedValue;
|
|
|
26 |
$this->transport = $transport;
|
|
|
27 |
$this->queued = $queued;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function toString(): string
|
|
|
31 |
{
|
|
|
32 |
return sprintf('%shas %s "%d" emails', $this->transport ? $this->transport.' ' : '', $this->queued ? 'queued' : 'sent', $this->expectedValue);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @param MessageEvents $events
|
|
|
37 |
*/
|
|
|
38 |
protected function matches($events): bool
|
|
|
39 |
{
|
|
|
40 |
return $this->expectedValue === $this->countEmails($events);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* @param MessageEvents $events
|
|
|
45 |
*/
|
|
|
46 |
protected function failureDescription($events): string
|
|
|
47 |
{
|
|
|
48 |
return sprintf('the Transport %s (%d %s)', $this->toString(), $this->countEmails($events), $this->queued ? 'queued' : 'sent');
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
private function countEmails(MessageEvents $events): int
|
|
|
52 |
{
|
|
|
53 |
$count = 0;
|
|
|
54 |
foreach ($events->getEvents($this->transport) as $event) {
|
|
|
55 |
if (
|
|
|
56 |
($this->queued && $event->isQueued())
|
|
|
57 |
||
|
|
|
58 |
(!$this->queued && !$event->isQueued())
|
|
|
59 |
) {
|
|
|
60 |
++$count;
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
return $count;
|
|
|
65 |
}
|
|
|
66 |
}
|