Completed
Push — master ( 008bc0...0640ce )
by
unknown
46:22 queued 05:29
created

JobQueueTest::testLaterUnique()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 49
rs 9.2258
c 1
b 0
f 1
cc 1
eloc 39
nc 1
nop 0
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
use SfCod\SocketIoBundle\Service\Broadcast;
12
13
/**
14
 * Class JobQueueTest
15
 * @author Virchenko Maksim <[email protected]>
16
 * @package SfCod\QueueBundle\Tests\Service
17
 */
18
class JobQueueTest extends TestCase
19
{
20
    use LoadTrait;
21
22
    /**
23
     * Set up test
24
     */
25
    protected function setUp()
26
    {
27
        $this->configure();
28
    }
29
30
    /**
31
     * Test pushing into queue
32
     */
33
    public function testPush()
34
    {
35
        $job = uniqid('job_');
36
        $data = array_rand(range(0, 100), 10);
37
        $queue = uniqid('queue_');
38
        $connection = uniqid('connection_');
39
40
        $manager = $this->mockManager();
41
        $manager
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<SfCod\QueueBundle\Service\QueueManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
42
            ->expects($this->once())
43
            ->method('push')
44
            ->with($job, $data, $queue, $connection)
45
            ->will($this->returnValue(true));
46
47
        $jobQueue = $this->mockJobQueue($manager);
48
49
        $this->assertTrue($jobQueue->push($job, $data, $queue, $connection));
50
    }
51
52
    /**
53
     * Test push unique job into queue
54
     */
55
    public function testPushUnique()
56
    {
57
        $job = uniqid('job_');
58
        $data = array_rand(range(0, 100), 10);
59
        $queue = uniqid('queue_');
60
        $connection = uniqid('connection_');
61
62
        $connectionMock = $this->createMock(QueueInterface::class);
63
        $connectionMock
64
            ->expects($this->once())
65
            ->method('exists')
66
            ->with($this->equalTo($job), $this->equalTo($data), $this->equalTo($queue))
67
            ->will($this->returnValue(false));
68
69
        $manager = $this->mockManager();
70
        $manager
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<SfCod\QueueBundle\Service\QueueManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
71
            ->expects($this->once())
72
            ->method('connection')
73
            ->with($this->equalTo($connection))
74
            ->will($this->returnValue($connectionMock));
75
        $manager
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<SfCod\QueueBundle\Service\QueueManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
76
            ->expects($this->once())
77
            ->method('push')
78
            ->with($job, $data, $queue, $connection)
79
            ->will($this->returnValue(true));
80
81
        $jobQueue = $this->mockJobQueue($manager);
82
83
        $this->assertTrue($jobQueue->pushUnique($job, $data, $queue, $connection));
84
85
        $connectionMock = $this->createMock(QueueInterface::class);
86
        $connectionMock
87
            ->expects($this->once())
88
            ->method('exists')
89
            ->with($this->equalTo($job), $this->equalTo($data), $this->equalTo($queue))
90
            ->will($this->returnValue(true));
91
92
        $manager = $this->mockManager();
93
        $manager
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<SfCod\QueueBundle\Service\QueueManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
94
            ->expects($this->once())
95
            ->method('connection')
96
            ->with($this->equalTo($connection))
97
            ->will($this->returnValue($connectionMock));
98
99
        $jobQueue = $this->mockJobQueue($manager);
100
101
        $this->assertNull($jobQueue->pushUnique($job, $data, $queue, $connection));
102
    }
103
104
    /**
105
     * Test bulk pushing into queue
106
     */
107
    public function testBulk()
108
    {
109
        $jobs = [];
110
111
        for ($i = 0; $i < 10; $i++) {
112
            $jobs[] = uniqid('job_' . $i);
113
        }
114
115
        $data = array_rand(range(0, 100), 10);
116
        $queue = uniqid('queue_');
117
        $connection = uniqid('connection_');
118
119
        $manager = $this->mockManager();
120
        $manager
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<SfCod\QueueBundle\Service\QueueManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
121
            ->expects($this->once())
122
            ->method('bulk')
123
            ->with($jobs, $data, $queue, $connection)
124
            ->will($this->returnValue(true));
125
126
        $jobQueue = $this->mockJobQueue($manager);
127
128
        $this->assertTrue($jobQueue->bulk($jobs, $data, $queue, $connection));
129
    }
130
131
    /**
132
     * Test pushing job with delay
133
     */
134
    public function testLater()
135
    {
136
        $delay = rand(1, 1000);
137
        $job = uniqid('job_');
138
        $data = array_rand(range(0, 100), 10);
139
        $queue = uniqid('queue_');
140
        $connection = uniqid('connection_');
141
142
        $manager = $this->mockManager();
143
        $manager
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<SfCod\QueueBundle\Service\QueueManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
144
            ->expects($this->once())
145
            ->method('later')
146
            ->with($delay, $job, $data, $queue, $connection)
147
            ->will($this->returnValue(true));
148
149
        $jobQueue = $this->mockJobQueue($manager);
150
151
        $this->assertTrue($jobQueue->later($delay, $job, $data, $queue, $connection));
152
    }
153
154
    /**
155
     * Test push unique job with delay into queue
156
     */
157
    public function testLaterUnique()
158
    {
159
        $delay = rand(1, 1000);
160
        $job = uniqid('job_');
161
        $data = array_rand(range(0, 100), 10);
162
        $queue = uniqid('queue_');
163
        $connection = uniqid('connection_');
164
165
        $connectionMock = $this->createMock(QueueInterface::class);
166
        $connectionMock
167
            ->expects($this->once())
168
            ->method('exists')
169
            ->with($this->equalTo($job), $this->equalTo($data), $this->equalTo($queue))
170
            ->will($this->returnValue(false));
171
172
        $manager = $this->mockManager();
173
        $manager
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<SfCod\QueueBundle\Service\QueueManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
174
            ->expects($this->once())
175
            ->method('connection')
176
            ->with($this->equalTo($connection))
177
            ->will($this->returnValue($connectionMock));
178
        $manager
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<SfCod\QueueBundle\Service\QueueManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
179
            ->expects($this->once())
180
            ->method('later')
181
            ->with($delay, $job, $data, $queue, $connection)
182
            ->will($this->returnValue(true));
183
184
        $jobQueue = $this->mockJobQueue($manager);
185
186
        $this->assertTrue($jobQueue->laterUnique($delay, $job, $data, $queue, $connection));
187
188
        $connectionMock = $this->createMock(QueueInterface::class);
189
        $connectionMock
190
            ->expects($this->once())
191
            ->method('exists')
192
            ->with($this->equalTo($job), $this->equalTo($data), $this->equalTo($queue))
193
            ->will($this->returnValue(true));
194
195
        $manager = $this->mockManager();
196
        $manager
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<SfCod\QueueBundle\Service\QueueManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
197
            ->expects($this->once())
198
            ->method('connection')
199
            ->with($this->equalTo($connection))
200
            ->will($this->returnValue($connectionMock));
201
202
        $jobQueue = $this->mockJobQueue($manager);
203
204
        $this->assertNull($jobQueue->laterUnique($delay, $job, $data, $queue, $connection));
205
    }
206
207
    /**
208
     * Mock job queue
209
     *
210
     * @param QueueManager $manager
211
     *
212
     * @return JobQueue|MockObject
213
     */
214
    private function mockJobQueue(QueueManager $manager): JobQueue
215
    {
216
        $jobQueue = $this->getMockBuilder(JobQueue::class)
217
            ->setConstructorArgs([$manager])
218
            ->setMethods(null)
219
            ->getMock();
220
221
        return $jobQueue;
222
    }
223
224
    /**
225
     * Mock manager
226
     *
227
     * @return QueueManager|MockObject
228
     */
229
    private function mockManager(): QueueManager
230
    {
231
        $manager = $this->getMockBuilder(QueueManager::class)
232
            ->setMethods([
233
                'push',
234
                'bulk',
235
                'later',
236
                'connection',
237
            ])
238
            ->getMock();
239
240
        return $manager;
241
    }
242
}
243