Test Setup Failed
Push — master ( 81f598...8628f4 )
by Matthew
02:51
created

JobTest::testChainJobCall()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 3
nop 0
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Model;
4
5
use Dtc\QueueBundle\Model\Job;
6
use Dtc\QueueBundle\Tests\FibonacciWorker;
7
use Dtc\QueueBundle\Tests\GetterSetterTrait;
8
use Dtc\QueueBundle\Tests\StaticJobManager;
9
use PHPUnit\Framework\TestCase;
10
11
class JobTest extends TestCase
12
{
13
    use GetterSetterTrait;
14
15
    public function testSetArgs()
16
    {
17
        $worker = new FibonacciWorker();
18
        $job = new Job($worker, false, null);
19
        $job->setArgs(array(1));
20
        $job->setArgs(array(1, array(1, 2)));
21
22
        try {
23
            $job->setArgs(array($job));
24
            $this->fail('Invalid job argument passed');
25
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
26
        }
27
28
        try {
29
            $job->setArgs(array(1, array($job)));
30
            $this->fail('Invalid job argument passed');
31
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
32
        }
33
    }
34
35
    public function testGettersSetters()
36
    {
37
        $this->runGetterSetterTests('\Dtc\QueueBundle\Model\Job');
38
    }
39
40
    public function testToFromMessage() {
41
        $worker = new FibonacciWorker();
42
        $job = new Job($worker, false, null);
43
        $job->setArgs([1,2,3]);
44
        $job->setMethod('asdf');
45
        $job->setPriority(1234);
46
        $message = $job->toMessage();
47
48
        $job2 = new Job();
49
        $priority2 = $job2->getPriority();
50
        $job2->fromMessage($message);
51
52
        self::assertEquals($job->getMethod(), $job2->getMethod());
53
        self::assertEquals($job->getWorkerName(), $job2->getWorkerName());
54
        self::assertEquals($job->getArgs(), $job2->getArgs());
55
        self::assertEquals($priority2, $job2->getPriority());
56
57
        $worker = new FibonacciWorker();
58
        $job = new Job($worker, false, null);
59
        $job->setArgs([1,2,3]);
60
        $job->setMethod('asdf');
61
        $job->setPriority(1234);
62
        $date = new \DateTime();
63
        $job->setExpiresAt($date);
64
        $message = $job->toMessage();
65
66
        $job2 = new Job();
67
        $priority2 = $job2->getPriority();
68
        $job2->fromMessage($message);
69
70
        self::assertEquals($job->getMethod(), $job2->getMethod());
71
        self::assertEquals($job->getWorkerName(), $job2->getWorkerName());
72
        self::assertEquals($job->getArgs(), $job2->getArgs());
73
        self::assertEquals($priority2, $job2->getPriority());
74
        self::assertEquals($job->getExpiresAt(), $job2->getExpiresAt());
75
    }
76
77
    public function testChainJobCall()
78
    {
79
        $jobManager = new StaticJobManager();
80
        $worker = new FibonacciWorker();
81
        $worker->setJobManager($jobManager);
82
83
        $job = new Job($worker, false, null);
84
        $this->assertNull($job->getId(), 'Job id should be null');
85
86
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? 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...
87
        $this->assertNotNull($job->getId(), 'Job id should be generated');
88
89
        try {
90
            $job->invalidFunctionCall();
0 ignored issues
show
Documentation Bug introduced by
The method invalidFunctionCall does not exist on object<Dtc\QueueBundle\Model\Job>? 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...
91
            $this->fail('invalid chain, should fail');
92
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
93
        }
94
    }
95
}
96