-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathNotificationAssertionsTrait.php
100 lines (83 loc) · 3.95 KB
/
NotificationAssertionsTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Test;
use PHPUnit\Framework\Constraint\LogicalNot;
use Symfony\Component\Notifier\Event\MessageEvent;
use Symfony\Component\Notifier\Event\NotificationEvents;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Test\Constraint as NotifierConstraint;
/**
* @author Smaïne Milianni <[email protected]>
*/
trait NotificationAssertionsTrait
{
public static function assertNotificationCount(int $count, ?string $transportName = null, string $message = ''): void
{
self::assertThat(self::getNotificationEvents(), new NotifierConstraint\NotificationCount($count, $transportName), $message);
}
public static function assertQueuedNotificationCount(int $count, ?string $transportName = null, string $message = ''): void
{
self::assertThat(self::getNotificationEvents(), new NotifierConstraint\NotificationCount($count, $transportName, true), $message);
}
public static function assertNotificationIsQueued(MessageEvent $event, string $message = ''): void
{
self::assertThat($event, new NotifierConstraint\NotificationIsQueued(), $message);
}
public static function assertNotificationIsNotQueued(MessageEvent $event, string $message = ''): void
{
self::assertThat($event, new LogicalNot(new NotifierConstraint\NotificationIsQueued()), $message);
}
public static function assertNotificationSubjectContains(MessageInterface $notification, string $text, string $message = ''): void
{
self::assertThat($notification, new NotifierConstraint\NotificationSubjectContains($text), $message);
}
public static function assertNotificationSubjectNotContains(MessageInterface $notification, string $text, string $message = ''): void
{
self::assertThat($notification, new LogicalNot(new NotifierConstraint\NotificationSubjectContains($text)), $message);
}
public static function assertNotificationTransportIsEqual(MessageInterface $notification, ?string $transportName = null, string $message = ''): void
{
self::assertThat($notification, new NotifierConstraint\NotificationTransportIsEqual($transportName), $message);
}
public static function assertNotificationTransportIsNotEqual(MessageInterface $notification, ?string $transportName = null, string $message = ''): void
{
self::assertThat($notification, new LogicalNot(new NotifierConstraint\NotificationTransportIsEqual($transportName)), $message);
}
/**
* @return MessageEvent[]
*/
public static function getNotifierEvents(?string $transportName = null): array
{
return self::getNotificationEvents()->getEvents($transportName);
}
public static function getNotifierEvent(int $index = 0, ?string $transportName = null): ?MessageEvent
{
return self::getNotifierEvents($transportName)[$index] ?? null;
}
/**
* @return MessageInterface[]
*/
public static function getNotifierMessages(?string $transportName = null): array
{
return self::getNotificationEvents()->getMessages($transportName);
}
public static function getNotifierMessage(int $index = 0, ?string $transportName = null): ?MessageInterface
{
return self::getNotifierMessages($transportName)[$index] ?? null;
}
public static function getNotificationEvents(): NotificationEvents
{
$container = static::getContainer();
if ($container->has('notifier.notification_logger_listener')) {
return $container->get('notifier.notification_logger_listener')->getEvents();
}
static::fail('A client must have Notifier enabled to make notifications assertions. Did you forget to require symfony/notifier?');
}
}