Completed
Pull Request — master (#7)
by Lexey
03:27
created

QueueServiceTest::createService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\QueueBundle\Tests\Unit\Service;
6
7
use Lamoda\QueueBundle\Entity\QueueRepository;
8
use Lamoda\QueueBundle\Event\QueueAttemptsReachedEvent;
9
use Lamoda\QueueBundle\Exception\AttemptsReachedException;
10
use Lamoda\QueueBundle\Exception\UnexpectedValueException;
11
use Lamoda\QueueBundle\Factory\EntityFactory;
12
use Lamoda\QueueBundle\Service\QueueService;
13
use Lamoda\QueueBundle\Tests\Unit\Job\StubJob;
14
use Lamoda\QueueBundle\Tests\Unit\QueueCommonServicesTrait;
15
use Lamoda\QueueBundle\Tests\Unit\QueueEntity;
16
use Lamoda\QueueBundle\Tests\Unit\SymfonyMockTrait;
17
use PHPUnit_Framework_TestCase;
18
use Symfony\Component\EventDispatcher\EventDispatcher;
19
20
class QueueServiceTest 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...
21
{
22
    use QueueCommonServicesTrait;
23
    use SymfonyMockTrait;
24
25
    /** @var int */
26
    protected $maxAttempts = 5;
27
28
    /**
29
     * @throws \Doctrine\ORM\TransactionRequiredException
30
     */
31
    public function testGetToRestore(): void
32
    {
33
        $limit = 10;
34
35
        $queueRepository = $this->getQueueRepository();
36
        $queueRepository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lamoda\QueueBundle\Entity\QueueRepository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
37
            ->expects($this->once())
38
            ->method('getToRestore')
39
            ->with($this->maxAttempts, $limit);
40
41
        $this->createService($queueRepository)->getToRestore($limit);
42
    }
43
44
    /**
45
     * @param QueueEntity $queue
46
     *
47
     * @throws \Exception
48
     *
49
     * @dataProvider dataGetToProcess()
50
     */
51
    public function testGetToProcess(QueueEntity $queue): void
52
    {
53
        $queueRepository = $this->getQueueRepository();
54
        $queueRepository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lamoda\QueueBundle\Entity\QueueRepository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
55
            ->expects($this->once())
56
            ->method('find')
57
            ->willReturn($queue);
58
        $queueRepository
59
            ->expects($this->once())
60
            ->method('save')
61
            ->with($queue);
62
63
        $this->assertEquals($queue, $this->createService($queueRepository)->getToProcess(1));
64
        $this->assertEquals(QueueEntity::STATUS_IN_PROGRESS_TITLE, $queue->getStatusAsString());
65
    }
66
67
    public function dataGetToProcess(): array
68
    {
69
        return [
70
            'new status' => [(new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]))->setNew()],
71
            'in progress status' => [(new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]))->setInProgress()],
72
        ];
73
    }
74
75
    /**
76
     * @param null | QueueEntity $queueEntity
77
     * @param string $expectedExceptionMessage
78
     *
79
     * @throws \Exception
80
     *
81
     * @dataProvider dataGetToProcessQueueNotFound
82
     */
83
    public function testGetToProcessQueueNotFound(?QueueEntity $queueEntity, string $expectedExceptionMessage): void
84
    {
85
        $this->expectException(UnexpectedValueException::class);
86
        $this->expectExceptionMessage($expectedExceptionMessage);
87
88
        $queueRepository = $this->getQueueRepository();
89
90
        $queueRepository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lamoda\QueueBundle\Entity\QueueRepository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
91
            ->expects($this->once())
92
            ->method('find')
93
            ->willReturn($queueEntity);
94
95
        $this->createService($queueRepository)->getToProcess(1);
96
    }
97
98
    public function dataGetToProcessQueueNotFound(): array
99
    {
100
        return [
101
            'Not found' => [
102
                null,
103
                'The queue with id "1" was not found',
104
            ],
105
            'Status not NEW' => [
106
                new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]),
107
                'The queue "queue" with job "ClassJob" was not found in suitable status. Actual status is "initial"',
108
            ],
109
        ];
110
    }
111
112
    /**
113
     * @throws \Exception
114
     */
115
    public function testGetToProcessAttemptsReached(): void
116
    {
117
        $this->expectException(AttemptsReachedException::class);
118
119
        $queue = new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]);
120
        $queue->setNew();
121
122
        $this->maxAttempts = 0;
123
124
        $queueRepository = $this->getQueueRepository();
125
        $queueRepository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lamoda\QueueBundle\Entity\QueueRepository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
126
            ->expects($this->once())
127
            ->method('find')
128
            ->willReturn($queue);
129
        $queueRepository
130
            ->expects($this->once())
131
            ->method('save')
132
            ->with($queue);
133
134
        $attemptsReachedEvent = new QueueAttemptsReachedEvent($queue);
135
136
        $eventDispatcher = $this->getEventDispatcher();
137
        $eventDispatcher
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\EventDispatcher\EventDispatcher.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
138
            ->expects($this->once())
139
            ->method('dispatch')
140
            ->with('queue.attempts.reached.event', $attemptsReachedEvent);
141
142
        $this->assertEquals(
143
            $queue,
144
            $this->createService($queueRepository, null, $eventDispatcher)->getToProcess(1)
145
        );
146
        $this->assertEquals(QueueEntity::STATUS_ATTEMPTS_REACHED_TITLE, $queue->getStatusAsString());
147
    }
148
149
    public function testSave(): void
150
    {
151
        $queue = new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]);
152
153
        $queueRepository = $this->getQueueRepository();
154
        $queueRepository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lamoda\QueueBundle\Entity\QueueRepository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
155
            ->expects($this->once())
156
            ->method('save')
157
            ->with($queue)
158
            ->willReturnArgument(0);
159
160
        $this->assertEquals(
161
            $queue,
162
            $this->createService($queueRepository)->save($queue)
163
        );
164
    }
165
166
    public function testCreateQueue(): void
167
    {
168
        $job = new StubJob(1);
169
        $queue = new QueueEntity('queue', 'exchange', StubJob::class, ['id' => 1]);
170
171
        $entityFactory = $this->getEntityFactory();
172
        $entityFactory
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lamoda\QueueBundle\Factory\EntityFactory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
173
            ->expects($this->once())
174
            ->method('createQueue')
175
            ->with($job)
176
            ->willReturn($queue);
177
178
        $queueRepository = $this->getQueueRepository();
179
180
        $this->assertEquals(
181
            $queue,
182
            $this->createService($queueRepository, $entityFactory)->createQueue($job)
183
        );
184
    }
185
186
    public function testIsTransactionActive(): void
187
    {
188
        $expected = true;
189
190
        $queueRepository = $this->getQueueRepository();
191
        $queueRepository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lamoda\QueueBundle\Entity\QueueRepository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
192
            ->expects($this->once())
193
            ->method('isTransactionActive')
194
            ->willReturn($expected);
195
196
        $this->assertEquals(
197
            $expected,
198
            $this->createService($queueRepository)->isTransactionActive()
199
        );
200
    }
201
202
    private function createService(
203
        ?QueueRepository $repository = null,
204
        ?EntityFactory $entityFactory = null,
205
        ?EventDispatcher $eventDispatcher = null
206
    ): QueueService {
207
        return new QueueService(
208
            $repository ?? $this->getQueueRepository(),
209
            $entityFactory ?? $this->getEntityFactory(),
210
            $eventDispatcher ?? $this->getEventDispatcher(),
211
            $this->maxAttempts
212
        );
213
    }
214
215
    /**
216
     * @return \PHPUnit_Framework_MockObject_MockObject|QueueRepository
217
     */
218
    private function getQueueRepository()
219
    {
220
        return $this->getMockQueueRepository(
221
            [
222
                'find',
223
                'getToRestore',
224
                'save',
225
                'isTransactionActive',
226
            ]
227
        );
228
    }
229
230
    /**
231
     * @return EntityFactory|\PHPUnit_Framework_MockObject_MockObject
232
     */
233
    private function getEntityFactory()
234
    {
235
        return $this->getMockEntityFactory(['createQueue']);
236
    }
237
238
    /**
239
     * @return EventDispatcher|\PHPUnit_Framework_MockObject_MockObject
240
     */
241
    private function getEventDispatcher()
242
    {
243
        return $this->getMockEventDispatcher(['dispatch']);
244
    }
245
}
246