JobContractTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 153
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testJob() 0 11 1
A testJobPayload() 0 12 1
A testContract() 0 25 1
A testActions() 0 31 1
A mockJob() 0 18 1
A mockJobContract() 0 23 2
1
<?php
2
3
namespace SfCod\QueueBundle\Tests\Job;
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\Job
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 $job
0 ignored issues
show
Bug introduced by
There is no parameter named $job. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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