|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Notifier\Test\Constraint; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Constraint\Constraint; |
| 15 | +use Symfony\Component\Notifier\Event\NotificationEvents; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Smaïne Milianni <[email protected]> |
| 19 | + */ |
| 20 | +final class NotificationCount extends Constraint |
| 21 | +{ |
| 22 | + private $expectedValue; |
| 23 | + private $transport; |
| 24 | + private $queued; |
| 25 | + |
| 26 | + public function __construct(int $expectedValue, string $transport = null, bool $queued = false) |
| 27 | + { |
| 28 | + $this->expectedValue = $expectedValue; |
| 29 | + $this->transport = $transport; |
| 30 | + $this->queued = $queued; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritdoc} |
| 35 | + */ |
| 36 | + public function toString(): string |
| 37 | + { |
| 38 | + return sprintf('%shas %s "%d" emails', $this->transport ? $this->transport.' ' : '', $this->queued ? 'queued' : 'sent', $this->expectedValue); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param NotificationEvents $events |
| 43 | + * |
| 44 | + * {@inheritdoc} |
| 45 | + */ |
| 46 | + protected function matches($events): bool |
| 47 | + { |
| 48 | + return $this->expectedValue === $this->countNotifications($events); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param NotificationEvents $events |
| 53 | + * |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + protected function failureDescription($events): string |
| 57 | + { |
| 58 | + return sprintf('the Transport %s (%d %s)', $this->toString(), $this->countNotifications($events), $this->queued ? 'queued' : 'sent'); |
| 59 | + } |
| 60 | + |
| 61 | + private function countNotifications(NotificationEvents $events): int |
| 62 | + { |
| 63 | + $count = 0; |
| 64 | + foreach ($events->getEvents($this->transport) as $event) { |
| 65 | + if ( |
| 66 | + ($this->queued && $event->isQueued()) |
| 67 | + || |
| 68 | + (!$this->queued && !$event->isQueued()) |
| 69 | + ) { |
| 70 | + ++$count; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return $count; |
| 75 | + } |
| 76 | +} |
0 commit comments