Completed
Push — master ( 109933...83326a )
by Matthew
02:47
created

WorkerTest::testBatchAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
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
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 View Code Duplication
    public function testLater()
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...
44
    {
45
        $time = null;
46
        $job = $this->worker->later()->fibonacci(20);
47
        self::assertJob($job, $time, 'fibonacci');
48
        self::assertFalse($job->getBatch(), 'Should not be batching');
49
50
        // Test later with priority
51
        $priority = 1024;
52
        $job = $this->worker->later(0, $priority)->fibonacci(20);
53
        self::assertJob($job, $time, 'fibonacci', $priority);
54
55
        $this->failureTest($time, 'later');
56
    }
57
58 View Code Duplication
    public function testBatchLater()
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...
59
    {
60
        $time = null;
61
        $job = $this->worker->batchLater()->fibonacci(20);
62
        self::assertJob($job, $time, 'fibonacci');
63
        self::assertTrue($job->getBatch(), 'Should be batching');
64
65
        // Test batchLater with priority
66
        $priority = 1024;
67
        $job = $this->worker->batchLater(0, $priority)->fibonacci(20);
68
        self::assertJob($job, $time, 'fibonacci', $priority);
69
70
        $this->failureTest($time, 'batchLater');
71
    }
72
73
    protected function failureTest($method, $time)
74
    {
75
        // Test job with object
76
        try {
77
            $object = new \stdClass();
78
            $this->worker->$method($time)->fibonacci($object);
79
            self::fail('Exception should be thrown.');
80
        } catch (\Exception $e) {
81
            self::assertTrue(true);
82
        }
83
    }
84
85 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...
86
    {
87
        $time = time() + 3600;
88
        $job = $this->worker->batchAt($time)->fibonacci(20);
89
        self::assertJob($job, $time, 'fibonacci');
90
        self::assertTrue($job->getBatch(), 'Should be batching');
91
92
        // Test priority
93
        $priority = 1024;
94
        $job = $this->worker->batchAt($time, $priority)->fibonacci(20);
95
        self::assertJob($job, $time, 'fibonacci', $priority);
96
97
        $this->failureTest($time, 'batchAt');
98
    }
99
100
    /**
101
     * @param int|null $time
102
     * @param string   $method
103
     * @param int      $priority
104
     */
105
    protected function assertJob(Job $job, $time, $method, $priority = null)
106
    {
107
        self::assertNotEmpty($job->getId(), 'Job should have an id');
108
109
        if (null !== $time && $time > 0) {
110
            self::assertEquals(
111
                $time,
112
                $job->getWhenAt()->getTimestamp(),
113
                    'Job start time should equals'
114
            );
115
        }
116
117
        if (null !== $priority) {
118
            self::assertEquals(
119
                $priority,
120
                $job->getPriority(),
121
                    'Priority should be the same.'
122
            );
123
        } else {
124
            self::assertNull($job->getPriority(), 'Priority should be null');
125
        }
126
127
        self::assertEquals(
128
            $this->worker->getName(),
129
            $job->getWorkerName(),
130
                'Worker should be the same'
131
        );
132
        self::assertEquals(
133
            $method,
134
            $job->getMethod(),
135
                'Worker method should be the same'
136
        );
137
138
        // Make sure param gets saved
139
        self::assertContains(20, $job->getArgs());
140
    }
141
}
142