JobContractTest::testContract()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SfCod\QueueBundle\Tests\JobContract;
4
5
use Exception;
6
use PHPUnit\Framework\TestCase;
7
use SfCod\QueueBundle\Base\JobInterface;
8
use SfCod\QueueBundle\Base\JobResolverInterface;
9
use SfCod\QueueBundle\Entity\Job;
10
use SfCod\QueueBundle\Job\JobContract;
11
use SfCod\QueueBundle\Queue\QueueInterface;
12
13
/**
14
 * Class JobContractTest
15
 *
16
 * @author Virchenko Maksim <[email protected]>
17
 *
18
 * @package SfCod\QueueBundle\Tests\JobContract
19
 */
20
class JobContractTest extends TestCase
21
{
22
    /**
23
     * Test job
24
     */
25
    public function testJob()
26
    {
27
        $job = $this->mockJob();
28
        $contract = $this->mockJobContract($job);
29
30
        self::assertEquals($job->getId(), $contract->getJobId());
31
        self::assertEquals($job->getAttempts(), $contract->attempts());
32
        self::assertEquals($job->isReserved(), $contract->reserved());
33
        self::assertEquals($job->getReservedAt(), $contract->reservedAt());
34
        self::assertEquals($job->getPayload(), $contract->payload());
35
    }
36
37
    /**
38
     * Test job payload
39
     */
40
    public function testJobPayload()
41
    {
42
        $job = $this->mockJob();
43
        $contract = $this->mockJobContract($job);
44
45
        self::assertEquals($job->getPayload(), $contract->payload());
46
        self::assertEquals($job->getPayload()['job'], $contract->getName());
47
        self::assertEquals($job->getPayload()['data'], $contract->getData());
48
        self::assertEquals($job->getPayload()['maxTries'], $contract->maxTries());
49
        self::assertEquals($job->getPayload()['timeout'], $contract->timeout());
50
        self::assertEquals($job->getPayload()['timeoutAt'], $contract->timeoutAt());
51
    }
52
53
    /**
54
     * Test contract
55
     */
56
    public function testContract()
57
    {
58
        $job = $this->mockJob();
59
        $contract = $this->mockJobContract($job);
60
61
        self::assertFalse($contract->isDeleted());
62
        self::assertFalse($contract->isDeletedOrReleased());
63
64
        $contract->delete();
65
66
        self::assertTrue($contract->isDeleted());
67
        self::assertTrue($contract->isDeletedOrReleased());
68
69
        self::assertFalse($contract->isReleased());
70
71
        $contract->release();
72
73
        self::assertTrue($contract->isReleased());
74
75
        self::assertFalse($contract->hasFailed());
76
77
        $contract->markAsFailed();
78
79
        self::assertTrue($contract->hasFailed());
80
    }
81
82
    /**
83
     * Test contract main actions
84
     */
85
    public function testActions()
86
    {
87
        $job = $this->mockJob();
88
89
        $exception = new Exception(uniqid('message_'));
90
91
        $jobInstance = $this->getMockBuilder(JobInterface::class)
92
            ->setMethods([
93
                'fire',
94
                'failed',
95
            ])
96
            ->getMock();
97
        $jobInstance
98
            ->expects(self::once())
99
            ->method('fire')
100
            ->with(self::anything(), self::equalTo($job->getPayload()['data']));
101
        $jobInstance
102
            ->expects(self::once())
103
            ->method('failed')
104
            ->with(self::equalTo($job->getPayload()['data']), self::equalTo($exception));
105
106
        $contract = $this->mockJobContract($job, $jobInstance);
0 ignored issues
show
Documentation introduced by
$jobInstance is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SfCod\QueueBundle\Base\JobInterface>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
108
        $contract->fire();
109
110
        self::assertFalse($contract->hasFailed());
111
112
        $contract->failed($exception);
113
114
        self::assertTrue($contract->hasFailed());
115
    }
116
117
    /**
118
     * Mock job
119
     *
120
     * @return Job
121
     */
122
    private function mockJob(): Job
123
    {
124
        $job = new Job();
125
        $job->setId(new \MongoDB\BSON\ObjectID());
126
        $job->setQueue(uniqid('queue_'));
127
        $job->setAttempts(rand(1, 10));
128
        $job->setReserved((bool)rand(0, 1));
129
        $job->setReservedAt(time());
130
        $job->setPayload([
131
            'job' => uniqid('job_'),
132
            'data' => range(1, 10),
133
            'maxTries' => rand(1, 10),
134
            'timeout' => rand(1, 1000),
135
            'timeoutAt' => time() + rand(1, 1000),
136
        ]);
137
138
        return $job;
139
    }
140
141
    /**
142
     * Mock mongo job contract
143
     *
144
     * @param Job $jobData
145
     * @param JobInterface|null $jobInstance
146
     *
147
     * @return JobContract
148
     */
149
    private function mockJobContract(Job $jobData, ?JobInterface $jobInstance = null): JobContract
150
    {
151
        $queueName = uniqid('queue_name_');
152
        $queue = $this->createMock(QueueInterface::class);
153
154
        if (is_null($jobInstance)) {
155
            $jobInstance = $this->createMock(JobInterface::class);
156
        }
157
158
        $resolver = $this->createMock(JobResolverInterface::class);
159
        $resolver
160
            ->expects(self::any())
161
            ->method('resolve')
162
            ->with(self::equalTo($jobData->getPayload()['job']))
163
            ->willReturn($jobInstance);
164
165
        $contract = $this->getMockBuilder(JobContract::class)
166
            ->setConstructorArgs([$resolver, $queue, $jobData, $queueName])
167
            ->setMethods(null)
168
            ->getMock();
169
170
        return $contract;
171
    }
172
}
173