Passed
Push — master ( f67228...ea1bad )
by Matthew
08:44
created

WorkerTest::assertJob()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 36
rs 8.5806
c 1
b 1
f 0
cc 4
eloc 23
nc 4
nop 4
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Model;
4
5
use Dtc\QueueBundle\Model\Job;
6
use Dtc\QueueBundle\Model\JobTiming;
7
use Dtc\QueueBundle\Manager\JobTimingManager;
8
use Dtc\QueueBundle\Model\Run;
9
use Dtc\QueueBundle\Manager\RunManager;
10
use Dtc\QueueBundle\Model\Worker;
11
use Dtc\QueueBundle\Tests\FibonacciWorker;
12
use Dtc\QueueBundle\Tests\StaticJobManager;
13
14
class WorkerTest extends \PHPUnit_Framework_TestCase
15
{
16
    /** @var Worker */
17
    protected $worker;
18
    protected $jobManager;
19
20
    public function setUp()
21
    {
22
        $jobTimingManager = new JobTimingManager(JobTiming::class, false);
23
        $runManager = new RunManager(Run::class);
24
        $this->jobManager = new StaticJobManager($runManager, $jobTimingManager, Job::class);
25
        $this->worker = new FibonacciWorker();
26
        $this->worker->setJobManager($this->jobManager);
27
    }
28
29
    public function testGetJobManager()
30
    {
31
        self::assertNotNull($this->worker->getJobManager());
32
    }
33
34
    public function testAt()
35
    {
36
        $time = time() + 2600;
37
        $job = $this->worker->at($time)->fibonacci(20);
38
        self::assertJob($job, $time, 'fibonacci');
39
40
        // Test at with priority
41
        $priority = 1024;
42
        $job = $this->worker->at($time, false, $priority)->fibonacci(20);
43
        self::assertJob($job, $time, 'fibonacci', $priority);
44
        self::assertFalse($job->getBatch(), 'Should not be batching');
45
46
        // Test job with object
47
        $failed = false;
48
        try {
49
            $object = new \stdClass();
50
            $this->worker->at($time)->fibonacci($object);
51
            $failed = true;
52
        } catch (\Exception $e) {
53
            self::assertTrue(true);
54
        }
55
        self::assertFalse($failed);
56
57
        $dateTime = new \DateTime();
58
        $job = $this->worker->at()->fibonacci(20);
59
        self::assertGreaterThanOrEqual($dateTime, $job->getWhenAt());
60
        self::assertFalse($job->getBatch());
61
62
        $failed = false;
63
        try {
64
            $this->worker->at('abcd')->fibonacci(20);
65
            $failed = true;
66
        } catch (\InvalidArgumentException $exception) {
67
            self::assertNotNull($exception);
68
        }
69
        self::assertFalse($failed);
70
    }
71
72
    public function testLater()
73
    {
74
        $time = null;
75
        $this->batchLaterTest('later', false);
76
        $this->failureTest($time, 'later');
77
    }
78
79
    public function batchLaterTest($method, $assert = false)
80
    {
81
        $time = null;
82
        $job = $this->worker->$method()->fibonacci(20);
83
        self::assertJob($job, $time, 'fibonacci');
84
        if (!$assert) {
85
            self::assertFalse($job->getBatch(), 'Should not be batching');
86
        } else {
87
            self::assertTrue($job->getBatch(), 'Should be batching');
88
        }
89
90
        // Test with priority
91
        $priority = 1024;
92
        $job = $this->worker->$method(0, $priority)->fibonacci(20);
93
        self::assertJob($job, $time, 'fibonacci', $priority);
94
    }
95
96
    public function testBatchLater()
97
    {
98
        $time = null;
99
        $this->batchLaterTest('batchLater', true);
100
        $this->failureTest($time, 'batchLater');
101
    }
102
103
    protected function failureTest($time, $method)
104
    {
105
        // Test job with object
106
        try {
107
            $object = new \stdClass();
108
            $this->worker->$method($time)->fibonacci($object);
109
            self::fail('Exception should be thrown.');
110
        } catch (\Exception $e) {
111
            self::assertTrue(true);
112
        }
113
    }
114
115
    public function testBatchAt()
116
    {
117
        $time = time() + 3600;
118
        $job = $this->worker->batchAt($time)->fibonacci(20);
119
        self::assertJob($job, $time, 'fibonacci');
120
        self::assertTrue($job->getBatch(), 'Should be batching');
121
122
        // Test priority
123
        $priority = 1024;
124
        $job = $this->worker->batchAt($time, $priority)->fibonacci(20);
125
        self::assertJob($job, $time, 'fibonacci', $priority);
126
127
        $this->failureTest($time, 'batchAt');
128
    }
129
130
    /**
131
     * @param int|null $time
132
     * @param string   $method
133
     * @param int      $priority
134
     */
135
    protected function assertJob(Job $job, $time, $method, $priority = null)
136
    {
137
        self::assertNotEmpty($job->getId(), 'Job should have an id');
138
139
        if (null !== $time && $time > 0) {
140
            self::assertEquals(
141
                $time,
142
                $job->getWhenAt()->getTimestamp(),
143
                    'Job start time should equals'
144
            );
145
        }
146
147
        if (null !== $priority) {
148
            self::assertEquals(
149
                $priority,
150
                $job->getPriority(),
151
                    'Priority should be the same.'
152
            );
153
        } else {
154
            self::assertNull($job->getPriority(), 'Priority should be null');
155
        }
156
157
        self::assertEquals(
158
            $this->worker->getName(),
159
            $job->getWorkerName(),
160
                'Worker should be the same'
161
        );
162
        self::assertEquals(
163
            $method,
164
            $job->getMethod(),
165
                'Worker method should be the same'
166
        );
167
168
        // Make sure param gets saved
169
        self::assertContains(20, $job->getArgs());
170
    }
171
}
172