1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lamoda\QueueBundle\Tests\Unit\Service; |
6
|
|
|
|
7
|
|
|
use Lamoda\QueueBundle\Factory\PublisherFactory; |
8
|
|
|
use Lamoda\QueueBundle\Service\QueueRepublishService; |
9
|
|
|
use Lamoda\QueueBundle\Service\QueueService; |
10
|
|
|
use Lamoda\QueueBundle\Tests\Unit\QueueCommonServicesTrait; |
11
|
|
|
use Lamoda\QueueBundle\Tests\Unit\QueueEntity; |
12
|
|
|
use Lamoda\QueueBundle\Tests\Unit\Reflection; |
13
|
|
|
use PHPUnit_Framework_TestCase; |
14
|
|
|
use Symfony\Component\HttpKernel\Tests\Logger; |
15
|
|
|
|
16
|
|
|
class QueueRepublishServiceTest extends PHPUnit_Framework_TestCase |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
use QueueCommonServicesTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $queueMessages |
22
|
|
|
* |
23
|
|
|
* @dataProvider dataRestoreQueues |
24
|
|
|
*/ |
25
|
|
|
public function testRestoreQueues(array $queueMessages): void |
26
|
|
|
{ |
27
|
|
|
$mockQueueService = $this->getMockQueueService( |
28
|
|
|
[ |
29
|
|
|
'getToRepublish', |
30
|
|
|
'beginTransaction', |
31
|
|
|
'commit', |
32
|
|
|
'flush', |
33
|
|
|
'isTransactionActive', |
34
|
|
|
] |
35
|
|
|
); |
36
|
|
|
$mockQueueService |
37
|
|
|
->expects($this->once()) |
38
|
|
|
->method('beginTransaction'); |
39
|
|
|
$mockQueueService |
40
|
|
|
->expects($this->once()) |
41
|
|
|
->method('commit'); |
42
|
|
|
$mockQueueService |
43
|
|
|
->expects($this->once()) |
44
|
|
|
->method('flush'); |
45
|
|
|
$mockQueueService |
46
|
|
|
->expects($this->once()) |
47
|
|
|
->method('getToRepublish') |
48
|
|
|
->willReturn($queueMessages); |
49
|
|
|
$mockPublisherFactory = $this->getMockPublisherFactory(['republish', 'releaseAll']); |
50
|
|
|
$mockPublisherFactory |
51
|
|
|
->expects($this->exactly(count($queueMessages))) |
52
|
|
|
->method('republish'); |
53
|
|
|
$mockPublisherFactory |
54
|
|
|
->expects($this->once()) |
55
|
|
|
->method('releaseAll'); |
56
|
|
|
|
57
|
|
|
$queueRepublishService = $this->createService($mockPublisherFactory, $mockQueueService); |
58
|
|
|
|
59
|
|
|
$this->assertTrue($queueRepublishService->republishQueues(5)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @throws \Exception |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function dataRestoreQueues(): array |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$queueEntity = $this->getQueueEntity(); |
70
|
|
|
$queueEntity2 = $this->getQueueEntity(); |
71
|
|
|
Reflection::setProtectedProperty($queueEntity, 'id', 1); |
72
|
|
|
Reflection::setProtectedProperty($queueEntity2, 'id', 2); |
73
|
|
|
|
74
|
|
|
return [ |
75
|
|
|
[ |
76
|
|
|
[ |
77
|
|
|
$queueEntity, |
78
|
|
|
$queueEntity2, |
79
|
|
|
], |
80
|
|
|
], |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $queueMessages |
86
|
|
|
* |
87
|
|
|
* @dataProvider dataBatchRestoreQueues |
88
|
|
|
*/ |
89
|
|
|
public function testBatchRestoreQueues(array $queueMessages): void |
90
|
|
|
{ |
91
|
|
|
$mockQueueService = $this->getMockQueueService( |
92
|
|
|
[ |
93
|
|
|
'getToRepublish', |
94
|
|
|
'beginTransaction', |
95
|
|
|
'commit', |
96
|
|
|
'flush', |
97
|
|
|
'isTransactionActive', |
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
$mockQueueService |
101
|
|
|
->expects($this->exactly(4)) |
102
|
|
|
->method('beginTransaction'); |
103
|
|
|
$mockQueueService |
104
|
|
|
->expects($this->exactly(4)) |
105
|
|
|
->method('commit'); |
106
|
|
|
$mockQueueService |
107
|
|
|
->expects($this->exactly(4)) |
108
|
|
|
->method('flush'); |
109
|
|
|
|
110
|
|
|
$mockQueueService |
111
|
|
|
->expects($this->exactly(4)) |
112
|
|
|
->method('getToRepublish') |
113
|
|
|
->willReturnOnConsecutiveCalls( |
114
|
|
|
$queueMessages, |
115
|
|
|
$queueMessages, |
116
|
|
|
$queueMessages, |
117
|
|
|
[] |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$mockPublisherFactory = $this->getMockPublisherFactory(['republish', 'releaseAll']); |
121
|
|
|
$mockPublisherFactory |
122
|
|
|
->expects($this->exactly(3)) |
123
|
|
|
->method('republish'); |
124
|
|
|
$mockPublisherFactory |
125
|
|
|
->expects($this->exactly(4)) |
126
|
|
|
->method('releaseAll'); |
127
|
|
|
|
128
|
|
|
$queueRepublishService = $this->createService($mockPublisherFactory, $mockQueueService); |
129
|
|
|
|
130
|
|
|
$this->assertTrue($queueRepublishService->republishQueues(1)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @throws \Exception |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function dataBatchRestoreQueues(): array |
139
|
|
|
{ |
140
|
|
|
$queueEntity = $this->getQueueEntity(); |
141
|
|
|
Reflection::setProtectedProperty($queueEntity, 'id', 1); |
142
|
|
|
|
143
|
|
|
return [ |
144
|
|
|
[ |
145
|
|
|
[ |
146
|
|
|
$queueEntity, |
147
|
|
|
], |
148
|
|
|
], |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testRestoreQueuesFailed(): void |
153
|
|
|
{ |
154
|
|
|
$mockQueueService = $this->getMockQueueService( |
155
|
|
|
[ |
156
|
|
|
'getToRepublish', |
157
|
|
|
'beginTransaction', |
158
|
|
|
'commit', |
159
|
|
|
'flush', |
160
|
|
|
'isTransactionActive', |
161
|
|
|
'rollback', |
162
|
|
|
] |
163
|
|
|
); |
164
|
|
|
$mockQueueService |
165
|
|
|
->expects($this->once()) |
166
|
|
|
->method('beginTransaction'); |
167
|
|
|
$mockQueueService |
168
|
|
|
->expects($this->never()) |
169
|
|
|
->method('commit'); |
170
|
|
|
$mockQueueService |
171
|
|
|
->expects($this->never()) |
172
|
|
|
->method('flush'); |
173
|
|
|
$mockQueueService |
174
|
|
|
->expects($this->once()) |
175
|
|
|
->method('isTransactionActive') |
176
|
|
|
->willReturn(true); |
177
|
|
|
$mockQueueService |
178
|
|
|
->expects($this->once()) |
179
|
|
|
->method('rollback'); |
180
|
|
|
$mockQueueService |
181
|
|
|
->expects($this->once()) |
182
|
|
|
->method('getToRepublish') |
183
|
|
|
->will($this->throwException(new \Exception('Something broken'))); |
184
|
|
|
$queueRepublishService = $this->createService($this->getMockPublisherFactory(), $mockQueueService); |
185
|
|
|
|
186
|
|
|
$this->assertFalse($queueRepublishService->republishQueues(5)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param array $queueMessages |
191
|
|
|
* |
192
|
|
|
* @dataProvider dataRestoreQueuesFailedOnPublisherReleaseFailed |
193
|
|
|
*/ |
194
|
|
|
public function testRestoreQueuesFailedOnPublisherReleaseFailed(array $queueMessages): void |
195
|
|
|
{ |
196
|
|
|
$mockQueueService = $this->getMockQueueService( |
197
|
|
|
[ |
198
|
|
|
'getToRepublish', |
199
|
|
|
'beginTransaction', |
200
|
|
|
'commit', |
201
|
|
|
'flush', |
202
|
|
|
'isTransactionActive', |
203
|
|
|
'rollback', |
204
|
|
|
] |
205
|
|
|
); |
206
|
|
|
$mockQueueService |
207
|
|
|
->expects($this->once()) |
208
|
|
|
->method('beginTransaction'); |
209
|
|
|
$mockQueueService |
210
|
|
|
->expects($this->once()) |
211
|
|
|
->method('commit'); |
212
|
|
|
$mockQueueService |
213
|
|
|
->expects($this->once()) |
214
|
|
|
->method('flush'); |
215
|
|
|
$mockQueueService |
216
|
|
|
->expects($this->once()) |
217
|
|
|
->method('isTransactionActive') |
218
|
|
|
->willReturn(false); |
219
|
|
|
$mockQueueService |
220
|
|
|
->expects($this->never()) |
221
|
|
|
->method('rollback'); |
222
|
|
|
$mockQueueService |
223
|
|
|
->expects($this->once()) |
224
|
|
|
->method('getToRepublish') |
225
|
|
|
->willReturn($queueMessages); |
226
|
|
|
|
227
|
|
|
$publisherFactory = $this->getMockPublisherFactory(['republish', 'releaseAll']); |
228
|
|
|
$publisherFactory |
229
|
|
|
->expects($this->once()) |
230
|
|
|
->method('releaseAll') |
231
|
|
|
->will($this->throwException(new \Exception('Something broken'))); |
232
|
|
|
|
233
|
|
|
$queueRepublishService = $this->createService($publisherFactory, $mockQueueService); |
234
|
|
|
|
235
|
|
|
$this->assertFalse($queueRepublishService->republishQueues(5)); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @throws \Exception |
240
|
|
|
* |
241
|
|
|
* @return array |
242
|
|
|
*/ |
243
|
|
View Code Duplication |
public function dataRestoreQueuesFailedOnPublisherReleaseFailed(): array |
|
|
|
|
244
|
|
|
{ |
245
|
|
|
$queueEntity = $this->getQueueEntity(); |
246
|
|
|
$queueEntity2 = $this->getQueueEntity(); |
247
|
|
|
Reflection::setProtectedProperty($queueEntity, 'id', 1); |
248
|
|
|
Reflection::setProtectedProperty($queueEntity2, 'id', 2); |
249
|
|
|
|
250
|
|
|
return [ |
251
|
|
|
[ |
252
|
|
|
[ |
253
|
|
|
$queueEntity, |
254
|
|
|
$queueEntity2, |
255
|
|
|
], |
256
|
|
|
], |
257
|
|
|
]; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param string $jobName |
262
|
|
|
* @param string $exchange |
263
|
|
|
* @param string $queueName |
264
|
|
|
* @param array $data |
265
|
|
|
* |
266
|
|
|
* @throws \Exception |
267
|
|
|
* |
268
|
|
|
* @return QueueEntity |
269
|
|
|
*/ |
270
|
|
|
protected function getQueueEntity( |
271
|
|
|
string $jobName = 'someJobName', |
272
|
|
|
string $exchange = 'exchange', |
273
|
|
|
string $queueName = 'some_queue_name', |
274
|
|
|
array $data = ['id' => 1] |
275
|
|
|
): QueueEntity { |
276
|
|
|
$queue = new QueueEntity($queueName, $exchange, $jobName, $data); |
277
|
|
|
|
278
|
|
|
Reflection::setProtectedProperty($queue, 'id', 1); |
279
|
|
|
|
280
|
|
|
return $queue; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
protected function createService( |
284
|
|
|
PublisherFactory $publisherFactory, |
285
|
|
|
QueueService $queueService |
286
|
|
|
): QueueRepublishService { |
287
|
|
|
return new QueueRepublishService( |
288
|
|
|
$publisherFactory, |
289
|
|
|
$queueService, |
290
|
|
|
new Logger() |
291
|
|
|
); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|