|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\NotificationBundle\Tests\Backend; |
|
15
|
|
|
|
|
16
|
|
|
use Laminas\Diagnostics\Result\Success; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
use Sonata\NotificationBundle\Backend\PostponeRuntimeBackend; |
|
19
|
|
|
use Sonata\NotificationBundle\Consumer\ConsumerEventInterface; |
|
20
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
23
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @covers \Sonata\NotificationBundle\Backend\PostponeRuntimeBackend |
|
27
|
|
|
*/ |
|
28
|
|
|
class PostponeRuntimeBackendTest extends TestCase |
|
29
|
|
|
{ |
|
30
|
|
|
public function testIteratorContainsPublishedMessages(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$backend = new PostponeRuntimeBackend( |
|
33
|
|
|
$this->createMock(EventDispatcherInterface::class), |
|
34
|
|
|
true |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
$messages = []; |
|
38
|
|
|
|
|
39
|
|
|
$message = $backend->create('foo', []); |
|
40
|
|
|
$messages[] = $message; |
|
41
|
|
|
$backend->publish($message); |
|
42
|
|
|
|
|
43
|
|
|
$message = $backend->create('bar', []); |
|
44
|
|
|
$messages[] = $message; |
|
45
|
|
|
$backend->publish($message); |
|
46
|
|
|
|
|
47
|
|
|
$message = $backend->create('baz', []); |
|
48
|
|
|
$messages[] = $message; |
|
49
|
|
|
$backend->publish($message); |
|
50
|
|
|
|
|
51
|
|
|
$backend->create('not_published', []); |
|
52
|
|
|
|
|
53
|
|
|
$iterator = $backend->getIterator(); |
|
54
|
|
|
foreach ($iterator as $eachKey => $eachMessage) { |
|
55
|
|
|
$this->assertSame($messages[$eachKey], $eachMessage); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testNoMessagesOnEvent(): void |
|
60
|
|
|
{ |
|
61
|
|
|
$backend = $this->getMockBuilder(PostponeRuntimeBackend::class) |
|
62
|
|
|
->setMethods(['handle']) |
|
63
|
|
|
->setConstructorArgs([$this->createMock(EventDispatcherInterface::class)]) |
|
64
|
|
|
->getMock(); |
|
65
|
|
|
|
|
66
|
|
|
$backend |
|
67
|
|
|
->expects($this->never()) |
|
68
|
|
|
->method('handle') |
|
69
|
|
|
; |
|
70
|
|
|
|
|
71
|
|
|
$backend->onEvent(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testLiveEnvironment(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$dispatcher = new EventDispatcher(); |
|
77
|
|
|
$backend = new PostponeRuntimeBackend($dispatcher, true); |
|
78
|
|
|
$dispatcher->addListener('kernel.terminate', [$backend, 'onEvent']); |
|
79
|
|
|
|
|
80
|
|
|
$message = $backend->create('notification.demo', []); |
|
81
|
|
|
$backend->publish($message); |
|
82
|
|
|
|
|
83
|
|
|
// This message will not be handled. |
|
84
|
|
|
$backend->create('notification.demo', []); |
|
85
|
|
|
|
|
86
|
|
|
$phpunit = $this; |
|
87
|
|
|
$phpunit->passed = false; |
|
88
|
|
|
$dispatcher->addListener('notification.demo', static function (ConsumerEventInterface $event) use ($phpunit, $message): void { |
|
89
|
|
|
$phpunit->assertSame($message, $event->getMessage()); |
|
90
|
|
|
|
|
91
|
|
|
$phpunit->passed = true; |
|
92
|
|
|
}); |
|
93
|
|
|
|
|
94
|
|
|
$dispatcher->dispatch(new GenericEvent(), 'kernel.terminate'); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertTrue($phpunit->passed); |
|
97
|
|
|
$this->assertSame(MessageInterface::STATE_DONE, $message->getState()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testRecursiveMessage(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$dispatcher = new EventDispatcher(); |
|
103
|
|
|
$backend = new PostponeRuntimeBackend($dispatcher, true); |
|
104
|
|
|
$dispatcher->addListener('kernel.terminate', [$backend, 'onEvent']); |
|
105
|
|
|
|
|
106
|
|
|
$message1 = $backend->create('notification.demo1', []); |
|
107
|
|
|
$message2 = $backend->create('notification.demo2', []); |
|
108
|
|
|
|
|
109
|
|
|
$backend->publish($message1); |
|
110
|
|
|
|
|
111
|
|
|
$phpunit = $this; |
|
112
|
|
|
$phpunit->passed1 = false; |
|
113
|
|
|
$phpunit->passed2 = false; |
|
114
|
|
|
|
|
115
|
|
|
$dispatcher->addListener('notification.demo1', static function (ConsumerEventInterface $event) use ($phpunit, $message1, $message2, $backend): void { |
|
116
|
|
|
$phpunit->assertSame($message1, $event->getMessage()); |
|
117
|
|
|
|
|
118
|
|
|
$phpunit->passed1 = true; |
|
119
|
|
|
|
|
120
|
|
|
$backend->publish($message2); |
|
121
|
|
|
}); |
|
122
|
|
|
|
|
123
|
|
|
$dispatcher->addListener('notification.demo2', static function (ConsumerEventInterface $event) use ($phpunit, $message2): void { |
|
124
|
|
|
$phpunit->assertSame($message2, $event->getMessage()); |
|
125
|
|
|
|
|
126
|
|
|
$phpunit->passed2 = true; |
|
127
|
|
|
}); |
|
128
|
|
|
|
|
129
|
|
|
$dispatcher->dispatch(new GenericEvent(), 'kernel.terminate'); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertTrue($phpunit->passed1); |
|
132
|
|
|
$this->assertTrue($phpunit->passed2); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertSame(MessageInterface::STATE_DONE, $message1->getState()); |
|
135
|
|
|
$this->assertSame(MessageInterface::STATE_DONE, $message2->getState()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testStatusIsOk(): void |
|
139
|
|
|
{ |
|
140
|
|
|
$backend = new PostponeRuntimeBackend( |
|
141
|
|
|
$this->createMock(EventDispatcherInterface::class), |
|
142
|
|
|
true |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$status = $backend->getStatus(); |
|
146
|
|
|
$this->assertInstanceOf(Success::class, $status); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function testOnCliPublishHandlesDirectly(): void |
|
150
|
|
|
{ |
|
151
|
|
|
$backend = $this->getMockBuilder(PostponeRuntimeBackend::class) |
|
152
|
|
|
->setMethods(['handle']) |
|
153
|
|
|
->setConstructorArgs([$this->createMock(EventDispatcherInterface::class)]) |
|
154
|
|
|
->getMock(); |
|
155
|
|
|
|
|
156
|
|
|
$backend |
|
157
|
|
|
->expects($this->once()) |
|
158
|
|
|
->method('handle') |
|
159
|
|
|
; |
|
160
|
|
|
|
|
161
|
|
|
$message = $backend->create('notification.demo', []); |
|
162
|
|
|
$backend->publish($message); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|