Completed
Push — master ( d46a90...0d9294 )
by
unknown
43:57 queued 02:43
created

JobQueueTest::testBulk()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 15
nc 2
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
86
    /**
87
     * Test bulk pushing into queue
88
     */
89
    public function testBulk()
90
    {
91
        $jobs = [];
92
93
        for ($i = 0; $i < 10; $i++) {
94
            $jobs[] = uniqid('job_' . $i);
95
        }
96
97
        $data = array_rand(range(0, 100), 10);
98
        $queue = uniqid('queue_');
99
        $connection = uniqid('connection_');
100
101
        $manager = $this->mockManager();
102
        $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...
103
            ->expects($this->once())
104
            ->method('bulk')
105
            ->with($jobs, $data, $queue, $connection)
106
            ->will($this->returnValue(true));
107
108
        $jobQueue = $this->mockJobQueue($manager);
109
110
        $this->assertTrue($jobQueue->bulk($jobs, $data, $queue, $connection));
111
    }
112
113
    /**
114
     * Test pushing job with delay
115
     */
116
    public function testLater()
117
    {
118
        $delay = rand(1, 1000);
119
        $job = uniqid('job_');
120
        $data = array_rand(range(0, 100), 10);
121
        $queue = uniqid('queue_');
122
        $connection = uniqid('connection_');
123
124
        $manager = $this->mockManager();
125
        $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...
126
            ->expects($this->once())
127
            ->method('later')
128
            ->with($delay, $job, $data, $queue, $connection)
129
            ->will($this->returnValue(true));
130
131
        $jobQueue = $this->mockJobQueue($manager);
132
133
        $this->assertTrue($jobQueue->later($delay, $job, $data, $queue, $connection));
134
    }
135
136
    /**
137
     * Test push unique job with delay into queue
138
     */
139
    public function testLaterUnique()
140
    {
141
        $delay = rand(1, 1000);
142
        $job = uniqid('job_');
143
        $data = array_rand(range(0, 100), 10);
144
        $queue = uniqid('queue_');
145
        $connection = uniqid('connection_');
146
147
        $connectionMock = $this->createMock(QueueInterface::class);
148
        $connectionMock
149
            ->expects($this->once())
150
            ->method('exists')
151
            ->with($this->equalTo($job), $this->equalTo($data), $this->equalTo($queue))
152
            ->will($this->returnValue(false));
153
154
        $manager = $this->mockManager();
155
        $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...
156
            ->expects($this->once())
157
            ->method('connection')
158
            ->with($this->equalTo($connection))
159
            ->will($this->returnValue($connectionMock));
160
        $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...
161
            ->expects($this->once())
162
            ->method('later')
163
            ->with($delay, $job, $data, $queue, $connection)
164
            ->will($this->returnValue(true));
165
166
        $jobQueue = $this->mockJobQueue($manager);
167
168
        $this->assertTrue($jobQueue->laterUnique($delay, $job, $data, $queue, $connection));
169
    }
170
171
    /**
172
     * Mock job queue
173
     *
174
     * @param QueueManager $manager
175
     *
176
     * @return JobQueue|MockObject
177
     */
178
    private function mockJobQueue(QueueManager $manager): JobQueue
179
    {
180
        $jobQueue = $this->getMockBuilder(JobQueue::class)
181
            ->setConstructorArgs([$manager])
182
            ->setMethods(null)
183
            ->getMock();
184
185
        return $jobQueue;
186
    }
187
188
    /**
189
     * Mock manager
190
     *
191
     * @return QueueManager|MockObject
192
     */
193
    private function mockManager(): QueueManager
194
    {
195
        $manager = $this->getMockBuilder(QueueManager::class)
196
            ->setMethods([
197
                'push',
198
                'bulk',
199
                'later',
200
                'connection',
201
            ])
202
            ->getMock();
203
204
        return $manager;
205
    }
206
}
207