Completed
Push — master ( 3c1e11...7d78ad )
by
unknown
37:34
created

JobContractTest::mockJobContract()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\JobContract\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
        $this->assertEquals($job->getId(), $contract->getJobId());
31
        $this->assertEquals($job->getAttempts(), $contract->attempts());
32
        $this->assertEquals($job->isReserved(), $contract->reserved());
33
        $this->assertEquals($job->getReservedAt(), $contract->reservedAt());
34
        $this->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
        $this->assertEquals($job->getPayload(), $contract->payload());
46
        $this->assertEquals($job->getPayload()['job'], $contract->getName());
47
        $this->assertEquals($job->getPayload()['data'], $contract->getData());
48
        $this->assertEquals($job->getPayload()['maxTries'], $contract->maxTries());
49
        $this->assertEquals($job->getPayload()['timeout'], $contract->timeout());
50
        $this->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
        $this->assertFalse($contract->isDeleted());
62
        $this->assertFalse($contract->isDeletedOrReleased());
63
64
        $contract->delete();
65
66
        $this->assertTrue($contract->isDeleted());
67
        $this->assertTrue($contract->isDeletedOrReleased());
68
69
        $this->assertFalse($contract->isReleased());
70
71
        $contract->release();
72
73
        $this->assertTrue($contract->isReleased());
74
75
        $this->assertFalse($contract->hasFailed());
76
77
        $contract->markAsFailed();
78
79
        $this->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)
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/pull/3687

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
92
            ->setMethods([
93
                'fire',
94
                'failed',
95
            ])
96
            ->getMock();
97
        $jobInstance
98
            ->expects($this->once())
99
            ->method('fire')
100
            ->with($this->anything(), $this->equalTo($job->getPayload()['data']));
101
        $jobInstance
102
            ->expects($this->once())
103
            ->method('failed')
104
            ->with($this->equalTo($job->getPayload()['data']), $this->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
        $this->assertFalse($contract->hasFailed());
111
112
        $contract->failed($exception);
113
114
        $this->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($this->any())
161
            ->method('resolve')
162
            ->with($this->equalTo($jobData->getPayload()['job']))
163
            ->will($this->returnValue($jobInstance));
164
165
        $contract = $this->getMockBuilder(JobContract::class)
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/pull/3687

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
166
            ->setConstructorArgs([$resolver, $queue, $jobData, $queueName])
167
            ->setMethods(null)
168
            ->getMock();
169
170
        return $contract;
171
    }
172
}
173