Completed
Pull Request — master (#3)
by Anton
04:32 queued 01:19
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 View Code Duplication
    public function testRestoreQueues(array $queueMessages): void
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...
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->exactly(count($queueMessages)))
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
    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(3))
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(3))
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 View Code Duplication
    public function testRestoreQueuesFailed(): void
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...
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('rollback');
176
        $mockQueueService
177
            ->expects($this->once())
178
            ->method('getToRepublish')
179
            ->will($this->throwException(new \Exception('Something broken')));
180
        $queueRepublishService = $this->createService($this->getMockPublisherFactory(), $mockQueueService);
181
182
        $this->assertFalse($queueRepublishService->republishQueues(5));
183
    }
184
185
    /**
186
     * @param string $jobName
187
     * @param string $exchange
188
     * @param string $queueName
189
     * @param array  $data
190
     *
191
     * @throws \Exception
192
     *
193
     * @return QueueEntity
194
     */
195
    protected function getQueueEntity(
196
        string $jobName = 'someJobName',
197
        string $exchange = 'exchange',
198
        string $queueName = 'some_queue_name',
199
        array $data = ['id' => 1]
200
    ): QueueEntity {
201
        $queue = new QueueEntity($queueName, $exchange, $jobName, $data);
202
203
        Reflection::setProtectedProperty($queue, 'id', 1);
204
205
        return $queue;
206
    }
207
208
    protected function createService(
209
        PublisherFactory $publisherFactory,
210
        QueueService $queueService
211
    ): QueueRepublishService {
212
        return new QueueRepublishService(
213
            $publisherFactory,
214
            $queueService,
215
            new Logger()
216
        );
217
    }
218
}
219