Completed
Pull Request — master (#3)
by Anton
04:00
created

testBatchRestoreQueues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 9.232
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
0 ignored issues
show
Bug introduced by
There is one abstract method getMockBuilder in this class; you could implement it, or declare this class as abstract.
Loading history...
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
    /**
191
     * @param array $queueMessages
192
     *
193
     * @dataProvider dataRestoreQueuesFailedOnPublisherReleaseFailed
194
     */
195
    public function testRestoreQueuesFailedOnPublisherReleaseFailed(array $queueMessages): void
196
    {
197
        $mockQueueService = $this->getMockQueueService(
198
            [
199
                'getToRepublish',
200
                'beginTransaction',
201
                'commit',
202
                'flush',
203
                'isTransactionActive',
204
                'rollback',
205
            ]
206
        );
207
        $mockQueueService
208
            ->expects($this->once())
209
            ->method('beginTransaction');
210
        $mockQueueService
211
            ->expects($this->once())
212
            ->method('commit');
213
        $mockQueueService
214
            ->expects($this->once())
215
            ->method('flush');
216
        $mockQueueService
217
            ->expects($this->once())
218
            ->method('isTransactionActive')
219
            ->willReturn(false);
220
        $mockQueueService
221
            ->expects($this->never())
222
            ->method('rollback');
223
        $mockQueueService
224
            ->expects($this->once())
225
            ->method('getToRepublish')
226
            ->willReturn($queueMessages);
227
228
        $publisherFactory = $this->getMockPublisherFactory(['republish', 'releaseAll']);
229
        $publisherFactory
230
            ->expects($this->once())
231
            ->method('releaseAll')
232
            ->will($this->throwException(new \Exception('Something broken')));
233
234
        $queueRepublishService = $this->createService($publisherFactory, $mockQueueService);
235
236
        $this->assertFalse($queueRepublishService->republishQueues(5));
237
    }
238
239
240
    /**
241
     * @throws \Exception
242
     *
243
     * @return array
244
     */
245 View Code Duplication
    public function dataRestoreQueuesFailedOnPublisherReleaseFailed(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
246
    {
247
        $queueEntity = $this->getQueueEntity();
248
        $queueEntity2 = $this->getQueueEntity();
249
        Reflection::setProtectedProperty($queueEntity, 'id', 1);
250
        Reflection::setProtectedProperty($queueEntity2, 'id', 2);
251
252
        return [
253
            [
254
                [
255
                    $queueEntity,
256
                    $queueEntity2,
257
                ],
258
            ],
259
        ];
260
    }
261
262
    /**
263
     * @param string $jobName
264
     * @param string $exchange
265
     * @param string $queueName
266
     * @param array  $data
267
     *
268
     * @throws \Exception
269
     *
270
     * @return QueueEntity
271
     */
272
    protected function getQueueEntity(
273
        string $jobName = 'someJobName',
274
        string $exchange = 'exchange',
275
        string $queueName = 'some_queue_name',
276
        array $data = ['id' => 1]
277
    ): QueueEntity {
278
        $queue = new QueueEntity($queueName, $exchange, $jobName, $data);
279
280
        Reflection::setProtectedProperty($queue, 'id', 1);
281
282
        return $queue;
283
    }
284
285
    protected function createService(
286
        PublisherFactory $publisherFactory,
287
        QueueService $queueService
288
    ): QueueRepublishService {
289
        return new QueueRepublishService(
290
            $publisherFactory,
291
            $queueService,
292
            new Logger()
293
        );
294
    }
295
}
296