Completed
Push — master ( 49a196...e8b010 )
by
unknown
04:13
created

tests/unit/Service/QueueRepublishServiceTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 View Code Duplication
    public function testRestoreQueues(array $queueMessages): void
0 ignored issues
show
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 View Code Duplication
    public function testRestoreQueuesFailed(): void
0 ignored issues
show
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...
85
    {
86
        $mockQueueService = $this->getMockQueueService(
87
            [
88
                'getToRepublish',
89
                'beginTransaction',
90
                'commit',
91
                'flush',
92
                'isTransactionActive',
93
                'rollback',
94
            ]
95
        );
96
        $mockQueueService
97
            ->expects($this->once())
98
            ->method('beginTransaction');
99
        $mockQueueService
100
            ->expects($this->never())
101
            ->method('commit');
102
        $mockQueueService
103
            ->expects($this->never())
104
            ->method('flush');
105
        $mockQueueService
106
            ->expects($this->once())
107
            ->method('rollback');
108
        $mockQueueService
109
            ->expects($this->once())
110
            ->method('getToRepublish')
111
            ->will($this->throwException(new \Exception('Something broken')));
112
        $queueRepublishService = $this->createService($this->getMockPublisherFactory(), $mockQueueService);
113
114
        $this->assertFalse($queueRepublishService->republishQueues(5));
115
    }
116
117
    /**
118
     * @param string $jobName
119
     * @param string $exchange
120
     * @param string $queueName
121
     * @param array  $data
122
     *
123
     * @throws \Exception
124
     *
125
     * @return QueueEntity
126
     */
127
    protected function getQueueEntity(
128
        string $jobName = 'someJobName',
129
        string $exchange = 'exchange',
130
        string $queueName = 'some_queue_name',
131
        array $data = ['id' => 1]
132
    ): QueueEntity {
133
        $queue = new QueueEntity($queueName, $exchange, $jobName, $data);
134
135
        Reflection::setProtectedProperty($queue, 'id', 1);
136
137
        return $queue;
138
    }
139
140
    protected function createService(
141
        PublisherFactory $publisherFactory,
142
        QueueService $queueService
143
    ): QueueRepublishService {
144
        return new QueueRepublishService(
145
            $publisherFactory,
146
            $queueService,
147
            new Logger()
148
        );
149
    }
150
}
151