1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lamoda\QueueBundle\Tests\Unit\Service; |
6
|
|
|
|
7
|
|
|
use Lamoda\QueueBundle\Entity\QueueEntityInterface; |
8
|
|
|
use Lamoda\QueueBundle\Factory\PublisherFactory; |
9
|
|
|
use Lamoda\QueueBundle\Service\QueueRequeueService; |
10
|
|
|
use Lamoda\QueueBundle\Service\QueueService; |
11
|
|
|
use Lamoda\QueueBundle\Tests\Unit\QueueCommonServicesTrait; |
12
|
|
|
use Lamoda\QueueBundle\Tests\Unit\QueueEntity; |
13
|
|
|
use PHPUnit_Framework_TestCase; |
14
|
|
|
use Symfony\Component\HttpKernel\Tests\Logger; |
15
|
|
|
|
16
|
|
|
class QueueRequeueServiceTest extends PHPUnit_Framework_TestCase |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
use QueueCommonServicesTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $queueMessages |
22
|
|
|
* |
23
|
|
|
* @dataProvider dataRestoreQueues |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function testRestoreQueues(array $queueMessages): void |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$mockQueueService = $this->getMockQueueService( |
28
|
|
|
[ |
29
|
|
|
'getToRestore', |
30
|
|
|
'beginTransaction', |
31
|
|
|
'commit', |
32
|
|
|
'flush', |
33
|
|
|
'isTransactionActive', |
34
|
|
|
] |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$mockQueueService |
38
|
|
|
->expects($this->once()) |
39
|
|
|
->method('beginTransaction'); |
40
|
|
|
$mockQueueService |
41
|
|
|
->expects($this->once()) |
42
|
|
|
->method('commit'); |
43
|
|
|
$mockQueueService |
44
|
|
|
->expects($this->exactly(count($queueMessages))) |
45
|
|
|
->method('flush'); |
46
|
|
|
$mockQueueService |
47
|
|
|
->expects($this->once()) |
48
|
|
|
->method('getToRestore') |
49
|
|
|
->willReturn($queueMessages); |
50
|
|
|
|
51
|
|
|
$mockPublisherFactory = $this->getMockPublisherFactory(['requeue', 'releaseAll']); |
52
|
|
|
$mockPublisherFactory |
53
|
|
|
->expects($this->exactly(count($queueMessages))) |
54
|
|
|
->method('requeue'); |
55
|
|
|
$mockPublisherFactory |
56
|
|
|
->expects($this->once()) |
57
|
|
|
->method('releaseAll'); |
58
|
|
|
|
59
|
|
|
$queueRequeueService = $this->createService($mockPublisherFactory, $mockQueueService); |
60
|
|
|
|
61
|
|
|
$this->assertTrue($queueRequeueService->restoreQueues(5)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function dataRestoreQueues(): array |
65
|
|
|
{ |
66
|
|
|
$queueEntity = $this->getQueueEntity(); |
67
|
|
|
$queueEntity2 = $this->getQueueEntity(); |
68
|
|
|
|
69
|
|
|
return [ |
70
|
|
|
[ |
71
|
|
|
[ |
72
|
|
|
$queueEntity, |
73
|
|
|
$queueEntity2, |
74
|
|
|
], |
75
|
|
|
], |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testRestoreQueuesFailed(): void |
80
|
|
|
{ |
81
|
|
|
$mockQueueService = $this->getMockQueueService( |
82
|
|
|
[ |
83
|
|
|
'getToRestore', |
84
|
|
|
'beginTransaction', |
85
|
|
|
'commit', |
86
|
|
|
'flush', |
87
|
|
|
'isTransactionActive', |
88
|
|
|
'rollback', |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
$mockQueueService |
92
|
|
|
->expects($this->once()) |
93
|
|
|
->method('beginTransaction'); |
94
|
|
|
$mockQueueService |
95
|
|
|
->expects($this->never()) |
96
|
|
|
->method('commit'); |
97
|
|
|
$mockQueueService |
98
|
|
|
->expects($this->never()) |
99
|
|
|
->method('flush'); |
100
|
|
|
$mockQueueService |
101
|
|
|
->expects($this->once()) |
102
|
|
|
->method('rollback'); |
103
|
|
|
$mockQueueService |
104
|
|
|
->expects($this->once()) |
105
|
|
|
->method('getToRestore') |
106
|
|
|
->will($this->throwException(new \Exception('Something broken'))); |
107
|
|
|
$queueRequeueService = $this->createService($this->getMockPublisherFactory(), $mockQueueService); |
108
|
|
|
|
109
|
|
|
$this->assertFalse($queueRequeueService->restoreQueues(5)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param QueueEntityInterface $queue |
114
|
|
|
* |
115
|
|
|
* @dataProvider dataRequeue |
116
|
|
|
*/ |
117
|
|
|
public function testRequeue(QueueEntityInterface $queue): void |
118
|
|
|
{ |
119
|
|
|
$mockPublisherFactory = $this->getMockPublisherFactory(['requeue', 'releaseAll']); |
120
|
|
|
$mockPublisherFactory |
121
|
|
|
->expects($this->once()) |
122
|
|
|
->method('requeue'); |
123
|
|
|
$mockPublisherFactory |
124
|
|
|
->expects($this->once()) |
125
|
|
|
->method('releaseAll'); |
126
|
|
|
|
127
|
|
|
$service = $this->createService($mockPublisherFactory, $this->getMockQueueService()); |
128
|
|
|
|
129
|
|
|
$service->requeue($queue); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function dataRequeue(): array |
133
|
|
|
{ |
134
|
|
|
$queue = $this->getQueueEntity() |
135
|
|
|
->setInProgress() |
136
|
|
|
->setError(); |
137
|
|
|
|
138
|
|
|
return [ |
139
|
|
|
[$queue], |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function getQueueEntity( |
144
|
|
|
string $jobName = 'someJobName', |
145
|
|
|
string $exchange = 'exchange', |
146
|
|
|
string $queueName = 'some_queue_name', |
147
|
|
|
array $data = ['id' => 1] |
148
|
|
|
): QueueEntity { |
149
|
|
|
return new QueueEntity($queueName, $exchange, $jobName, $data); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
protected function createService( |
153
|
|
|
PublisherFactory $publisherFactory, |
154
|
|
|
QueueService $queueService |
155
|
|
|
): QueueRequeueService { |
156
|
|
|
return new QueueRequeueService( |
157
|
|
|
$publisherFactory, |
158
|
|
|
$queueService, |
159
|
|
|
new Logger() |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|