Passed
Pull Request — master (#57)
by Matthew
08:25
created

PriorityTestTrait::testPriorityJobs()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 98
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 98
rs 8.1707
c 0
b 0
f 0
nc 9
cc 5
eloc 60
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Manager;
4
5
use Dtc\QueueBundle\Manager\PriorityJobManager;
6
7
trait PriorityTestTrait
8
{
9
    public function testPriorityJobs()
10
    {
11
        $jobManager = static::$jobManager;
12
13
        if (null === $jobManager->getMaxPriority()) {
14
            return;
15
        }
16
17
        // Drain the queue
18
        $limit = 99999;
19
        while ($jobManager->getJob() && $limit--) {
20
            static::assertTrue(true);
21
        }
22
        self::assertGreaterThan(0, $limit);
23
24
        // Null vs priority case
25
26
        /** @var Job $job */
27
        $job = new static::$jobClass(static::$worker, false, null);
28
        $job->fibonacci(1);
29
        $id = $job->getId();
30
31
        $job2 = new static::$jobClass(static::$worker, false, null);
32
        $job2->setPriority(1);
33
        $job2->fibonacci(1);
34
        $id2 = $job2->getId();
35
36
        $nextJob = $jobManager->getJob();
37
        static::assertEquals($id2, $nextJob->getId());
38
        static::assertEquals($id, $jobManager->getJob()->getId());
39
40
        // priority vs priority case
41
42
        /** @var Job $job */
43
        $job = new static::$jobClass(static::$worker, false, null);
44
        $job->fibonacci(1);
45
        $job->setPriority(3);
46
        $id = $job->getId();
47
48
        $job2 = new static::$jobClass(static::$worker, false, null);
49
        $job2->setPriority(1);
50
        $job2->fibonacci(1);
51
        $id2 = $job2->getId();
52
53
        $nextJob = $jobManager->getJob();
54
        static::assertEquals($id2, $nextJob->getId());
55
        static::assertEquals($id, $jobManager->getJob()->getId());
56
57
        // priority too high case
58
59
        /** @var Job $job */
60
        $failed = false;
61
        try {
62
            $job = new static::$jobClass(static::$worker, false, null);
63
            $job->setPriority(999);
64
            $job->fibonacci(1);
65
            $failed = true;
66
        } catch (\Exception $exception) {
67
            static::assertTrue(true);
68
        }
69
        static::assertFalse($failed);
70
71
        // Flip direction
72
        $jobManager->setPriorityDirection(PriorityJobManager::PRIORITY_ASC);
73
74
        // priority vs priority case
75
76
        /** @var Job $job */
77
        $job = new static::$jobClass(static::$worker, false, null);
78
        $job->fibonacci(1);
79
        $job->setPriority(1);
80
        $id = $job->getId();
81
82
        $job2 = new static::$jobClass(static::$worker, false, null);
83
        $job2->setPriority(3);
84
        $job2->fibonacci(3);
85
        $id2 = $job2->getId();
86
87
        $nextJob = $jobManager->getJob();
88
        static::assertEquals($id2, $nextJob->getId());
89
        static::assertEquals($id, $jobManager->getJob()->getId());
90
91
        // Null vs priority case
92
93
        /** @var Job $job */
94
        $job = new static::$jobClass(static::$worker, false, null);
95
        $job->fibonacci(1);
96
        $id = $job->getId();
97
98
        $job2 = new static::$jobClass(static::$worker, false, null);
99
        $job2->setPriority(1);
100
        $job2->fibonacci(1);
101
        $id2 = $job2->getId();
102
103
        $nextJob = $jobManager->getJob();
104
        static::assertEquals($id2, $nextJob->getId());
105
        static::assertEquals($id, $jobManager->getJob()->getId());
106
    }
107
}
108