|
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\Notification; |
|
15
|
|
|
|
|
16
|
|
|
use Laminas\Diagnostics\Result\Failure; |
|
17
|
|
|
use Laminas\Diagnostics\Result\Success; |
|
18
|
|
|
use Laminas\Diagnostics\Result\Warning; |
|
19
|
|
|
use PHPUnit\Framework\TestCase; |
|
20
|
|
|
use Sonata\NotificationBundle\Backend\MessageManagerBackend; |
|
21
|
|
|
use Sonata\NotificationBundle\Exception\HandlingException; |
|
22
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
|
23
|
|
|
use Sonata\NotificationBundle\Model\MessageManagerInterface; |
|
24
|
|
|
use Sonata\NotificationBundle\Tests\Entity\Message; |
|
25
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
26
|
|
|
|
|
27
|
|
|
class MessageManagerBackendTest extends TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
public function testCreateAndPublish(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$message = new Message(); |
|
32
|
|
|
$modelManager = $this->createMock(MessageManagerInterface::class); |
|
33
|
|
|
$modelManager->expects($this->once())->method('save')->willReturn($message); |
|
34
|
|
|
$modelManager->expects($this->once())->method('create')->willReturn($message); |
|
35
|
|
|
|
|
36
|
|
|
$backend = new MessageManagerBackend($modelManager, []); |
|
37
|
|
|
$message = $backend->createAndPublish('foo', ['message' => 'salut']); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertInstanceOf(MessageInterface::class, $message); |
|
40
|
|
|
$this->assertSame(MessageInterface::STATE_OPEN, $message->getState()); |
|
41
|
|
|
$this->assertNotNull($message->getCreatedAt()); |
|
42
|
|
|
$this->assertSame('foo', $message->getType()); |
|
43
|
|
|
$this->assertSame(['message' => 'salut'], $message->getBody()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testHandleSuccess(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$message = new Message(); |
|
49
|
|
|
$modelManager = $this->createMock(MessageManagerInterface::class); |
|
50
|
|
|
$modelManager->expects($this->exactly(2))->method('save')->willReturn($message); |
|
51
|
|
|
|
|
52
|
|
|
$dispatcher = $this->createMock(EventDispatcherInterface::class); |
|
53
|
|
|
$dispatcher->expects($this->once())->method('dispatch'); |
|
54
|
|
|
|
|
55
|
|
|
$backend = new MessageManagerBackend($modelManager, []); |
|
56
|
|
|
|
|
57
|
|
|
$backend->handle($message, $dispatcher); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertSame(MessageInterface::STATE_DONE, $message->getState()); |
|
60
|
|
|
$this->assertNotNull($message->getCreatedAt()); |
|
61
|
|
|
$this->assertNotNull($message->getCompletedAt()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testHandleError(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$message = new Message(); |
|
67
|
|
|
$modelManager = $this->createMock(MessageManagerInterface::class); |
|
68
|
|
|
$modelManager->expects($this->exactly(2))->method('save')->willReturn($message); |
|
69
|
|
|
|
|
70
|
|
|
$dispatcher = $this->createMock(EventDispatcherInterface::class); |
|
71
|
|
|
$dispatcher->expects($this->once())->method('dispatch')->will($this->throwException(new \RuntimeException())); |
|
72
|
|
|
$backend = new MessageManagerBackend($modelManager, []); |
|
73
|
|
|
|
|
74
|
|
|
$e = false; |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
|
|
$backend->handle($message, $dispatcher); |
|
78
|
|
|
} catch (HandlingException $e) { |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->assertInstanceOf(HandlingException::class, $e); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertSame(MessageInterface::STATE_ERROR, $message->getState()); |
|
84
|
|
|
$this->assertNotNull($message->getCreatedAt()); |
|
85
|
|
|
$this->assertNotNull($message->getCompletedAt()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @dataProvider statusProvider |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testStatus($counts, $expectedStatus, $message): void |
|
92
|
|
|
{ |
|
93
|
|
|
$modelManager = $this->createMock(MessageManagerInterface::class); |
|
94
|
|
|
$modelManager->expects($this->once())->method('countStates')->willReturn($counts); |
|
95
|
|
|
|
|
96
|
|
|
$backend = new MessageManagerBackend($modelManager, [ |
|
97
|
|
|
MessageInterface::STATE_IN_PROGRESS => 10, |
|
98
|
|
|
MessageInterface::STATE_ERROR => 30, |
|
99
|
|
|
MessageInterface::STATE_OPEN => 100, |
|
100
|
|
|
MessageInterface::STATE_DONE => 10000, |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
$status = $backend->getStatus(); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertInstanceOf(\get_class($expectedStatus), $status); |
|
106
|
|
|
$this->assertSame($message, $status->getMessage()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public static function statusProvider(): array |
|
110
|
|
|
{ |
|
111
|
|
|
if (!class_exists(Success::class)) { |
|
112
|
|
|
return [[1, 1, 1]]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$data = []; |
|
116
|
|
|
|
|
117
|
|
|
$data[] = [ |
|
118
|
|
|
[ |
|
119
|
|
|
MessageInterface::STATE_IN_PROGRESS => 11, //here |
|
120
|
|
|
MessageInterface::STATE_ERROR => 31, |
|
121
|
|
|
MessageInterface::STATE_OPEN => 100, |
|
122
|
|
|
MessageInterface::STATE_DONE => 10000, |
|
123
|
|
|
], |
|
124
|
|
|
new Failure(), |
|
125
|
|
|
'Too many messages processed at the same time (Database)', |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
$data[] = [ |
|
129
|
|
|
[ |
|
130
|
|
|
MessageInterface::STATE_IN_PROGRESS => 1, |
|
131
|
|
|
MessageInterface::STATE_ERROR => 31, //here |
|
132
|
|
|
MessageInterface::STATE_OPEN => 100, |
|
133
|
|
|
MessageInterface::STATE_DONE => 10000, |
|
134
|
|
|
], |
|
135
|
|
|
new Failure(), |
|
136
|
|
|
'Too many errors (Database)', |
|
137
|
|
|
]; |
|
138
|
|
|
|
|
139
|
|
|
$data[] = [ |
|
140
|
|
|
[ |
|
141
|
|
|
MessageInterface::STATE_IN_PROGRESS => 1, |
|
142
|
|
|
MessageInterface::STATE_ERROR => 1, |
|
143
|
|
|
MessageInterface::STATE_OPEN => 101, //here |
|
144
|
|
|
MessageInterface::STATE_DONE => 10000, |
|
145
|
|
|
], |
|
146
|
|
|
new Warning(), |
|
147
|
|
|
'Too many messages waiting to be processed (Database)', |
|
148
|
|
|
]; |
|
149
|
|
|
|
|
150
|
|
|
$data[] = [ |
|
151
|
|
|
[ |
|
152
|
|
|
MessageInterface::STATE_IN_PROGRESS => 1, |
|
153
|
|
|
MessageInterface::STATE_ERROR => 1, |
|
154
|
|
|
MessageInterface::STATE_OPEN => 100, |
|
155
|
|
|
MessageInterface::STATE_DONE => 10001, //here |
|
156
|
|
|
], |
|
157
|
|
|
new Warning(), |
|
158
|
|
|
'Too many processed messages, please clean the database (Database)', |
|
159
|
|
|
]; |
|
160
|
|
|
|
|
161
|
|
|
$data[] = [ |
|
162
|
|
|
[ |
|
163
|
|
|
MessageInterface::STATE_IN_PROGRESS => 1, |
|
164
|
|
|
MessageInterface::STATE_ERROR => 1, |
|
165
|
|
|
MessageInterface::STATE_OPEN => 1, |
|
166
|
|
|
MessageInterface::STATE_DONE => 1, |
|
167
|
|
|
], |
|
168
|
|
|
new Success(), |
|
169
|
|
|
'Ok (Database)', |
|
170
|
|
|
]; |
|
171
|
|
|
|
|
172
|
|
|
return $data; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|