JobQueueTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 225
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A mockJobQueue() 0 9 1
A mockManager() 0 13 1
A setUp() 0 4 1
A testPush() 0 18 1
A testPushUnique() 0 48 1
A testBulk() 0 23 2
A testLater() 0 19 1
A testLaterUnique() 0 49 1
1
<?php
2
3
namespace SfCod\QueueBundle\Tests\Service;
4
5
use PHPUnit\Framework\MockObject\MockObject;
6
use PHPUnit\Framework\TestCase;
7
use SfCod\QueueBundle\Queue\QueueInterface;
8
use SfCod\QueueBundle\Service\JobQueue;
9
use SfCod\QueueBundle\Service\QueueManager;
10
use SfCod\QueueBundle\Tests\Data\LoadTrait;
11
12
/**
13
 * Class JobQueueTest
14
 *
15
 * @author Virchenko Maksim <[email protected]>
16
 *
17
 * @package SfCod\QueueBundle\Tests\Service
18
 */
19
class JobQueueTest extends TestCase
20
{
21
    use LoadTrait;
22
23
    /**
24
     * Set up test
25
     */
26
    protected function setUp()
27
    {
28
        $this->configure();
29
    }
30
31
    /**
32
     * Test pushing into queue
33
     */
34
    public function testPush()
35
    {
36
        $job = uniqid('job_');
37
        $data = array_rand(range(0, 100), 10);
38
        $queue = uniqid('queue_');
39
        $connection = uniqid('connection_');
40
41
        $manager = $this->mockManager();
42
        $manager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in SfCod\QueueBundle\Service\QueueManager.

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...
43
            ->expects(self::once())
44
            ->method('push')
45
            ->with($job, $data, $queue, $connection)
46
            ->willReturn(true);
47
48
        $jobQueue = $this->mockJobQueue($manager);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->mockManager() on line 41 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, SfCod\QueueBundle\Tests\...eueTest::mockJobQueue() does only seem to accept object<SfCod\QueueBundle\Service\QueueManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
49
50
        self::assertTrue($jobQueue->push($job, $data, $queue, $connection));
0 ignored issues
show
Bug introduced by
The method push does only exist in SfCod\QueueBundle\Service\JobQueue, but not in PHPUnit\Framework\MockObject\MockObject.

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...
51
    }
52
53
    /**
54
     * Test push unique job into queue
55
     */
56
    public function testPushUnique()
57
    {
58
        $job = uniqid('job_');
59
        $data = array_rand(range(0, 100), 10);
60
        $queue = uniqid('queue_');
61
        $connection = uniqid('connection_');
62
63
        $connectionMock = $this->createMock(QueueInterface::class);
64
        $connectionMock
65
            ->expects(self::once())
66
            ->method('exists')
67
            ->with(self::equalTo($job), self::equalTo($data), self::equalTo($queue))
68
            ->willReturn(false);
69
70
        $manager = $this->mockManager();
71
        $manager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in SfCod\QueueBundle\Service\QueueManager.

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...
72
            ->expects(self::once())
73
            ->method('connection')
74
            ->with(self::equalTo($connection))
75
            ->willReturn($connectionMock);
76
        $manager
77
            ->expects(self::once())
78
            ->method('push')
79
            ->with($job, $data, $queue, $connection)
80
            ->willReturn(true);
81
82
        $jobQueue = $this->mockJobQueue($manager);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->mockManager() on line 70 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, SfCod\QueueBundle\Tests\...eueTest::mockJobQueue() does only seem to accept object<SfCod\QueueBundle\Service\QueueManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
83
84
        self::assertTrue($jobQueue->pushUnique($job, $data, $queue, $connection));
0 ignored issues
show
Bug introduced by
The method pushUnique does only exist in SfCod\QueueBundle\Service\JobQueue, but not in PHPUnit\Framework\MockObject\MockObject.

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...
85
86
        $connectionMock = $this->createMock(QueueInterface::class);
87
        $connectionMock
88
            ->expects(self::once())
89
            ->method('exists')
90
            ->with(self::equalTo($job), self::equalTo($data), self::equalTo($queue))
91
            ->willReturn(true);
92
93
        $manager = $this->mockManager();
94
        $manager
95
            ->expects(self::once())
96
            ->method('connection')
97
            ->with(self::equalTo($connection))
98
            ->willReturn($connectionMock);
99
100
        $jobQueue = $this->mockJobQueue($manager);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->mockManager() on line 93 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, SfCod\QueueBundle\Tests\...eueTest::mockJobQueue() does only seem to accept object<SfCod\QueueBundle\Service\QueueManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101
102
        self::assertNull($jobQueue->pushUnique($job, $data, $queue, $connection));
103
    }
104
105
    /**
106
     * Test bulk pushing into queue
107
     */
108
    public function testBulk()
109
    {
110
        $jobs = [];
111
112
        for ($i = 0; $i < 10; ++$i) {
113
            $jobs[] = uniqid('job_' . $i);
114
        }
115
116
        $data = array_rand(range(0, 100), 10);
117
        $queue = uniqid('queue_');
118
        $connection = uniqid('connection_');
119
120
        $manager = $this->mockManager();
121
        $manager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in SfCod\QueueBundle\Service\QueueManager.

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...
122
            ->expects(self::once())
123
            ->method('bulk')
124
            ->with($jobs, $data, $queue, $connection)
125
            ->willReturn(true);
126
127
        $jobQueue = $this->mockJobQueue($manager);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->mockManager() on line 120 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, SfCod\QueueBundle\Tests\...eueTest::mockJobQueue() does only seem to accept object<SfCod\QueueBundle\Service\QueueManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
128
129
        self::assertTrue($jobQueue->bulk($jobs, $data, $queue, $connection));
0 ignored issues
show
Bug introduced by
The method bulk does only exist in SfCod\QueueBundle\Service\JobQueue, but not in PHPUnit\Framework\MockObject\MockObject.

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...
130
    }
131
132
    /**
133
     * Test pushing job with delay
134
     */
135
    public function testLater()
136
    {
137
        $delay = rand(1, 1000);
138
        $job = uniqid('job_');
139
        $data = array_rand(range(0, 100), 10);
140
        $queue = uniqid('queue_');
141
        $connection = uniqid('connection_');
142
143
        $manager = $this->mockManager();
144
        $manager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in SfCod\QueueBundle\Service\QueueManager.

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...
145
            ->expects(self::once())
146
            ->method('later')
147
            ->with($delay, $job, $data, $queue, $connection)
148
            ->willReturn(true);
149
150
        $jobQueue = $this->mockJobQueue($manager);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->mockManager() on line 143 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, SfCod\QueueBundle\Tests\...eueTest::mockJobQueue() does only seem to accept object<SfCod\QueueBundle\Service\QueueManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
151
152
        self::assertTrue($jobQueue->later($delay, $job, $data, $queue, $connection));
0 ignored issues
show
Bug introduced by
The method later does only exist in SfCod\QueueBundle\Service\JobQueue, but not in PHPUnit\Framework\MockObject\MockObject.

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...
153
    }
154
155
    /**
156
     * Test push unique job with delay into queue
157
     */
158
    public function testLaterUnique()
159
    {
160
        $delay = rand(1, 1000);
161
        $job = uniqid('job_');
162
        $data = array_rand(range(0, 100), 10);
163
        $queue = uniqid('queue_');
164
        $connection = uniqid('connection_');
165
166
        $connectionMock = $this->createMock(QueueInterface::class);
167
        $connectionMock
168
            ->expects(self::once())
169
            ->method('exists')
170
            ->with(self::equalTo($job), self::equalTo($data), self::equalTo($queue))
171
            ->willReturn(false);
172
173
        $manager = $this->mockManager();
174
        $manager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in SfCod\QueueBundle\Service\QueueManager.

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...
175
            ->expects(self::once())
176
            ->method('connection')
177
            ->with(self::equalTo($connection))
178
            ->willReturn($connectionMock);
179
        $manager
180
            ->expects(self::once())
181
            ->method('later')
182
            ->with($delay, $job, $data, $queue, $connection)
183
            ->willReturn(true);
184
185
        $jobQueue = $this->mockJobQueue($manager);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->mockManager() on line 173 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, SfCod\QueueBundle\Tests\...eueTest::mockJobQueue() does only seem to accept object<SfCod\QueueBundle\Service\QueueManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
186
187
        self::assertTrue($jobQueue->laterUnique($delay, $job, $data, $queue, $connection));
0 ignored issues
show
Bug introduced by
The method laterUnique does only exist in SfCod\QueueBundle\Service\JobQueue, but not in PHPUnit\Framework\MockObject\MockObject.

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...
188
189
        $connectionMock = $this->createMock(QueueInterface::class);
190
        $connectionMock
191
            ->expects(self::once())
192
            ->method('exists')
193
            ->with(self::equalTo($job), self::equalTo($data), self::equalTo($queue))
194
            ->willReturn(true);
195
196
        $manager = $this->mockManager();
197
        $manager
198
            ->expects(self::once())
199
            ->method('connection')
200
            ->with(self::equalTo($connection))
201
            ->willReturn($connectionMock);
202
203
        $jobQueue = $this->mockJobQueue($manager);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->mockManager() on line 196 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, SfCod\QueueBundle\Tests\...eueTest::mockJobQueue() does only seem to accept object<SfCod\QueueBundle\Service\QueueManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
204
205
        self::assertNull($jobQueue->laterUnique($delay, $job, $data, $queue, $connection));
206
    }
207
208
    /**
209
     * Mock job queue
210
     *
211
     * @param QueueManager $manager
212
     *
213
     * @return JobQueue|MockObject
214
     */
215
    private function mockJobQueue(QueueManager $manager): JobQueue
216
    {
217
        $jobQueue = $this->getMockBuilder(JobQueue::class)
218
            ->setConstructorArgs([$manager])
219
            ->setMethods(null)
220
            ->getMock();
221
222
        return $jobQueue;
223
    }
224
225
    /**
226
     * Mock manager
227
     *
228
     * @return QueueManager|MockObject
229
     */
230
    private function mockManager(): QueueManager
231
    {
232
        $manager = $this->getMockBuilder(QueueManager::class)
233
            ->setMethods([
234
                'push',
235
                'bulk',
236
                'later',
237
                'connection',
238
            ])
239
            ->getMock();
240
241
        return $manager;
242
    }
243
}
244