Test Setup Failed
Pull Request — master (#7)
by Matthew
14:04
created

JobTest::testGettersSetters()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 17
nc 4
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\StaticJobManager;
8
use PHPUnit\Framework\TestCase;
9
10
class JobTest extends TestCase
11
{
12
    public function testSetArgs()
13
    {
14
        $worker = new FibonacciWorker();
15
        $job = new Job($worker, false, null);
16
        $job->setArgs(array(1));
17
        $job->setArgs(array(1, array(1, 2)));
18
19
        try {
20
            $job->setArgs(array($job));
21
            $this->fail('Invalid job argument passed');
22
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
23
        }
24
25
        try {
26
            $job->setArgs(array(1, array($job)));
27
            $this->fail('Invalid job argument passed');
28
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
29
        }
30
    }
31
32
    public function testGettersSetters()
33
    {
34
        $reflection = new \ReflectionClass('\Dtc\QueueBundle\Model\Job');
35
        $properties = $reflection->getProperties();
36
        foreach ($properties as $property) {
37
            $name = $property->getName();
38
            $getMethod = 'get'.ucfirst($name);
39
            $setMethod = 'set'.ucfirst($name);
40
            self::assertTrue($reflection->hasMethod($getMethod), $getMethod);
41
            self::assertTrue($reflection->hasMethod($setMethod), $setMethod);
42
43
            $job = new Job();
44
45
            $parameters = $reflection->getMethod($setMethod)->getParameters();
46
            if ($parameters && count($parameters) == 1) {
47
                $parameter = $parameters[0];
48
                if (!$parameter->getClass()) {
49
                    $someValue = 'somevalue';
50
                    $job->$setMethod($someValue);
51
                    self::assertSame($someValue, $job->$getMethod(), "$setMethod, $getMethod");
52
                }
53
            }
54
        }
55
    }
56
57
    public function testChainJobCall()
58
    {
59
        $jobManager = new StaticJobManager();
60
        $worker = new FibonacciWorker();
61
        $worker->setJobManager($jobManager);
62
63
        $job = new Job($worker, false, null);
64
        $this->assertNull($job->getId(), 'Job id should be null');
65
66
        $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...
67
        $this->assertNotNull($job->getId(), 'Job id should be generated');
68
69
        try {
70
            $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...
71
            $this->fail('invalid chain, should fail');
72
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
73
        }
74
    }
75
}
76