|
| 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\Bundle\FrameworkBundle\Test; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Constraint\LogicalNot; |
| 15 | +use Symfony\Component\Notifier\Event\MessageEvent; |
| 16 | +use Symfony\Component\Notifier\Event\NotificationEvents; |
| 17 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 18 | +use Symfony\Component\Notifier\Test\Constraint as NotifierConstraint; |
| 19 | + |
| 20 | +/* |
| 21 | + * @author Smaïne Milianni <[email protected]> |
| 22 | + */ |
| 23 | +trait NotificationAssertionsTrait |
| 24 | +{ |
| 25 | + public static function assertNotificationCount(int $count, string $transport = null, string $message = ''): void |
| 26 | + { |
| 27 | + self::assertThat(self::getNotificationEvents(), new NotifierConstraint\NotificationCount($count, $transport), $message); |
| 28 | + } |
| 29 | + |
| 30 | + public static function assertQueuedNotificationCount(int $count, string $transport = null, string $message = ''): void |
| 31 | + { |
| 32 | + self::assertThat(self::getMessageMailerEvents(), new NotifierConstraint\NotificationCount($count, $transport, true), $message); |
| 33 | + } |
| 34 | + |
| 35 | + public static function assertNotificationIsQueued(MessageEvent $event, string $message = ''): void |
| 36 | + { |
| 37 | + self::assertThat($event, new NotifierConstraint\NotificationIsQueued(), $message); |
| 38 | + } |
| 39 | + |
| 40 | + public static function assertNotificationIsNotQueued(MessageEvent $event, string $message = ''): void |
| 41 | + { |
| 42 | + self::assertThat($event, new LogicalNot(new NotifierConstraint\NotificationIsQueued()), $message); |
| 43 | + } |
| 44 | + |
| 45 | + public static function assertNotificationSubjectContains(MessageInterface $messageObject, string $text, string $message = ''): void |
| 46 | + { |
| 47 | + self::assertThat($messageObject, new NotifierConstraint\NotificationSubjectContains($text), $message); |
| 48 | + } |
| 49 | + |
| 50 | + public static function assertNotificationSubjectNotContains(MessageInterface $messageObject, string $text, string $message = ''): void |
| 51 | + { |
| 52 | + self::assertThat($messageObject, new LogicalNot(new NotifierConstraint\NotificationSubjectContains($text)), $message); |
| 53 | + } |
| 54 | + |
| 55 | + public static function assertNotificationTransportIsEqual(MessageInterface $messageObject, string $text, string $message = ''): void |
| 56 | + { |
| 57 | + self::assertThat($messageObject, new NotifierConstraint\NotificationTransportIsEqual($text), $message); |
| 58 | + } |
| 59 | + |
| 60 | + public static function assertNotificationTransportIsNotEqual(MessageInterface $messageObject, string $text, string $message = ''): void |
| 61 | + { |
| 62 | + self::assertThat($messageObject, new LogicalNot(new NotifierConstraint\NotificationTransportIsEqual($text)), $message); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return MessageEvent[] |
| 67 | + */ |
| 68 | + public static function getNotifierEvents(string $transport = null): array |
| 69 | + { |
| 70 | + return self::getNotificationEvents()->getEvents($transport); |
| 71 | + } |
| 72 | + |
| 73 | + public static function getNotifierEvent(int $index = 0, string $transport = null): ?MessageEvent |
| 74 | + { |
| 75 | + return self::getNotifierEvents($transport)[$index] ?? null; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return MessageInterface[] |
| 80 | + */ |
| 81 | + public static function getNotifierMessages(string $transport = null): array |
| 82 | + { |
| 83 | + return self::getNotificationEvents()->getMessages($transport); |
| 84 | + } |
| 85 | + |
| 86 | + public static function getNotifierMessage(int $index = 0, string $transport = null): ?MessageInterface |
| 87 | + { |
| 88 | + return self::getNotifierMessages($transport)[$index] ?? null; |
| 89 | + } |
| 90 | + |
| 91 | + public static function getNotificationEvents(): NotificationEvents |
| 92 | + { |
| 93 | + $container = static::getContainer(); |
| 94 | + if ($container->has('notifier.logger_notification_listener')) { |
| 95 | + return $container->get('notifier.logger_notification_listener')->getEvents(); |
| 96 | + } |
| 97 | + |
| 98 | + static::fail('A client must have Notifier enabled to make notifications assertions. Did you forget to require symfony/notifier?'); |
| 99 | + } |
| 100 | +} |
0 commit comments