Test Setup Failed
Push — master ( d1c94c...4274e1 )
by Matthew
05:58 queued 03:11
created

WorkerTest::batchLaterTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
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
9
class WorkerTest extends \PHPUnit_Framework_TestCase
10
{
11
    protected $worker;
12
    protected $jobManager;
13
14
    public function setUp()
15
    {
16
        $this->jobManager = new StaticJobManager();
17
        $this->worker = new FibonacciWorker();
18
        $this->worker->setJobManager($this->jobManager);
19
    }
20
21
    public function testAt()
22
    {
23
        $time = time() + 2600;
24
        $job = $this->worker->at($time)->fibonacci(20);
25
        self::assertJob($job, $time, 'fibonacci');
26
27
        // Test at with priority
28
        $priority = 1024;
29
        $job = $this->worker->at($time, false, $priority)->fibonacci(20);
30
        self::assertJob($job, $time, 'fibonacci', $priority);
31
        self::assertFalse($job->getBatch(), 'Should not be batching');
32
33
        // Test job with object
34
        try {
35
            $object = new \stdClass();
36
            $this->worker->at($time)->fibonacci($object);
37
            self::fail('Exception should be thrown.');
38
        } catch (\Exception $e) {
39
            self::assertTrue(true);
40
        }
41
    }
42
43
    public function testLater()
44
    {
45
        $this->batchLaterTest('later');
46
        $this->failureTest($time, 'later');
0 ignored issues
show
Bug introduced by
The variable $time does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47
    }
48
49 View Code Duplication
    public function batchLaterTest($method)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $time = null;
52
        $job = $this->worker->$method()->fibonacci(20);
53
        self::assertJob($job, $time, 'fibonacci');
54
        self::assertFalse($job->getBatch(), 'Should not be batching');
55
56
        // Test with priority
57
        $priority = 1024;
58
        $job = $this->worker->$method(0, $priority)->fibonacci(20);
59
        self::assertJob($job, $time, 'fibonacci', $priority);
60
    }
61
62
    public function testBatchLater()
63
    {
64
        $this->batchLaterTest('batchLater');
65
        $this->failureTest($time, 'batchLater');
0 ignored issues
show
Bug introduced by
The variable $time does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
66
    }
67
68
    protected function failureTest($method, $time)
69
    {
70
        // Test job with object
71
        try {
72
            $object = new \stdClass();
73
            $this->worker->$method($time)->fibonacci($object);
74
            self::fail('Exception should be thrown.');
75
        } catch (\Exception $e) {
76
            self::assertTrue(true);
77
        }
78
    }
79
80 View Code Duplication
    public function testBatchAt()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $time = time() + 3600;
83
        $job = $this->worker->batchAt($time)->fibonacci(20);
84
        self::assertJob($job, $time, 'fibonacci');
85
        self::assertTrue($job->getBatch(), 'Should be batching');
86
87
        // Test priority
88
        $priority = 1024;
89
        $job = $this->worker->batchAt($time, $priority)->fibonacci(20);
90
        self::assertJob($job, $time, 'fibonacci', $priority);
91
92
        $this->failureTest($time, 'batchAt');
93
    }
94
95
    /**
96
     * @param int|null $time
97
     * @param string   $method
98
     * @param int      $priority
99
     */
100
    protected function assertJob(Job $job, $time, $method, $priority = null)
101
    {
102
        self::assertNotEmpty($job->getId(), 'Job should have an id');
103
104
        if (null !== $time && $time > 0) {
105
            self::assertEquals(
106
                $time,
107
                $job->getWhenAt()->getTimestamp(),
108
                    'Job start time should equals'
109
            );
110
        }
111
112
        if (null !== $priority) {
113
            self::assertEquals(
114
                $priority,
115
                $job->getPriority(),
116
                    'Priority should be the same.'
117
            );
118
        } else {
119
            self::assertNull($job->getPriority(), 'Priority should be null');
120
        }
121
122
        self::assertEquals(
123
            $this->worker->getName(),
124
            $job->getWorkerName(),
125
                'Worker should be the same'
126
        );
127
        self::assertEquals(
128
            $method,
129
            $job->getMethod(),
130
                'Worker method should be the same'
131
        );
132
133
        // Make sure param gets saved
134
        self::assertContains(20, $job->getArgs());
135
    }
136
}
137