Test Setup Failed
Push — master ( 2ed22b...7cf6c7 )
by Matthew
02:52
created

WorkerTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 142
Duplicated Lines 59.15 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 84
loc 142
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testAt() 21 21 2
A testLater() 21 21 2
A testBatchLater() 21 21 2
A testBatchAt() 21 21 2
B assertJob() 0 36 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public function testAt()
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...
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
        // Test job with object
56
        try {
57
            $object = new \stdClass();
58
            $this->worker->later($time)->fibonacci($object);
59
            self::fail('Exception should be thrown.');
60
        } catch (\Exception $e) {
61
            self::assertTrue(true);
62
        }
63
    }
64
65 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...
66
    {
67
        $time = null;
68
        $job = $this->worker->batchLater()->fibonacci(20);
69
        self::assertJob($job, $time, 'fibonacci');
70
        self::assertTrue($job->getBatch(), 'Should be batching');
71
72
        // Test batchLater with priority
73
        $priority = 1024;
74
        $job = $this->worker->batchLater(0, $priority)->fibonacci(20);
75
        self::assertJob($job, $time, 'fibonacci', $priority);
76
77
        // Test job with object
78
        try {
79
            $object = new \stdClass();
80
            $this->worker->batchLater($time)->fibonacci($object);
81
            self::fail('Exception should be thrown.');
82
        } catch (\Exception $e) {
83
            self::assertTrue(true);
84
        }
85
    }
86
87 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...
88
    {
89
        $time = time() + 3600;
90
        $job = $this->worker->batchAt($time)->fibonacci(20);
91
        self::assertJob($job, $time, 'fibonacci');
92
        self::assertTrue($job->getBatch(), 'Should be batching');
93
94
        // Test priority
95
        $priority = 1024;
96
        $job = $this->worker->batchAt($time, $priority)->fibonacci(20);
97
        self::assertJob($job, $time, 'fibonacci', $priority);
98
99
        // Test job with object
100
        try {
101
            $object = new \stdClass();
102
            $this->worker->batchAt($time)->fibonacci($object);
103
            self::fail('Exception should be thrown.');
104
        } catch (\Exception $e) {
105
            self::assertTrue(true);
106
        }
107
    }
108
109
    /**
110
     * @param int|null $time
111
     * @param string   $method
112
     * @param int      $priority
113
     */
114
    protected function assertJob(Job $job, $time, $method, $priority = null)
115
    {
116
        self::assertNotEmpty($job->getId(), 'Job should have an id');
117
118
        if ($time && $time > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $time of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
119
            self::assertEquals(
120
                $time,
121
                $job->getWhenAt()->getTimestamp(),
122
                    'Job start time should equals'
123
            );
124
        }
125
126
        if ($priority) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $priority of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
127
            self::assertEquals(
128
                $priority,
129
                $job->getPriority(),
130
                    'Priority should be the same.'
131
            );
132
        } else {
133
            self::assertNull($job->getPriority(), 'Priority should be null');
134
        }
135
136
        self::assertEquals(
137
            $this->worker->getName(),
138
            $job->getWorkerName(),
139
                'Worker should be the same'
140
        );
141
        self::assertEquals(
142
            $method,
143
            $job->getMethod(),
144
                'Worker method should be the same'
145
        );
146
147
        // Make sure param gets saved
148
        self::assertContains(20, $job->getArgs());
149
    }
150
}
151