Completed
Pull Request — master (#30)
by Matthew
14:09 queued 11:28
created

JobManagerTest::testSaveJob()   B

Complexity

Conditions 6
Paths 48

Size

Total Lines 72
Code Lines 55

Duplication

Lines 72
Ratio 100 %

Importance

Changes 0
Metric Value
dl 72
loc 72
rs 8.5164
c 0
b 0
f 0
cc 6
eloc 55
nc 48
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\Redis;
4
5
use Dtc\QueueBundle\Model\BaseJob;
6
use Dtc\QueueBundle\Model\Job;
7
use Dtc\QueueBundle\Model\JobTiming;
8
use Dtc\QueueBundle\Manager\JobTimingManager;
9
use Dtc\QueueBundle\Model\Run;
10
use Dtc\QueueBundle\Manager\RunManager;
11
use Dtc\QueueBundle\Redis\JobManager;
12
use Dtc\QueueBundle\Redis\Predis;
13
use Dtc\QueueBundle\Tests\FibonacciWorker;
14
use Dtc\QueueBundle\Tests\Manager\AutoRetryTrait;
15
use Dtc\QueueBundle\Tests\Manager\BaseJobManagerTest;
16
use Dtc\QueueBundle\Tests\Manager\PriorityTestTrait;
17
use Predis\Client;
18
19
/**
20
 * @author David
21
 *
22
 * This test requires local beanstalkd running
23
 */
24
class JobManagerTest extends BaseJobManagerTest
25
{
26
    use PriorityTestTrait;
27
    use AutoRetryTrait;
28
    public static $connection;
29
30
    public static function setUpBeforeClass()
31
    {
32
        $host = getenv('REDIS_HOST');
33
        $port = getenv('REDIS_PORT') ?: 6379;
34
        $jobTimingClass = JobTiming::class;
35
        $runClass = Run::class;
36
        $predisClient = new Client(['scheme' => 'tcp', 'host' => $host, 'port' => $port]);
37
        $predisClient->flushall();
38
        $predis = new Predis($predisClient);
39
40
        self::$jobTimingManager = new JobTimingManager($jobTimingClass, false);
41
        self::$runManager = new RunManager($runClass);
42
        self::$jobManager = new JobManager(self::$runManager, self::$jobTimingManager, \Dtc\QueueBundle\Redis\Job::class, 'test_cache_key');
43
        self::$jobManager->setRedis($predis);
44
        self::$jobManager->setMaxPriority(255);
45
        self::$worker = new FibonacciWorker();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Dtc\QueueBundle\Tests\FibonacciWorker() of type object<Dtc\QueueBundle\Tests\FibonacciWorker> is incompatible with the declared type object<Dtc\QueueBundle\Manager\Worker> of property $worker.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        parent::setUpBeforeClass();
47
    }
48
49 View Code Duplication
    public function testConstructor()
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
        $test = null;
52
        try {
53
            $test = new JobManager(self::$runManager, self::$jobTimingManager, Job::class, 'something');
54
        } catch (\Exception $exception) {
55
            self::fail("shouldn't get here");
56
        }
57
        self::assertNotNull($test);
58
    }
59
60 View Code Duplication
    public function testGetJobByWorker()
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...
61
    {
62
        $failed = false;
63
        try {
64
            self::$jobManager->getJob(self::$worker->getName());
65
            $failed = true;
66
        } catch (\Exception $exception) {
67
            self::assertTrue(true);
68
        }
69
        self::assertFalse($failed);
70
    }
71
72 View Code Duplication
    public function testExpiredJob()
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...
73
    {
74
        $job = new self::$jobClass(self::$worker, false, null);
75
        $time = time() - 1;
76
        $job->setExpiresAt(new \DateTime("@$time"))->fibonacci(1);
77
        self::assertNotNull($job->getId(), 'Job id should be generated');
78
79
        $jobInQueue = self::$jobManager->getJob();
80
        self::assertNull($jobInQueue, 'The job should have been dropped...');
81
82
        $job = new self::$jobClass(self::$worker, false, null);
83
        $time = time() - 1;
84
        $job->setExpiresAt(new \DateTime("@$time"))->fibonacci(1);
85
86
        $job = new self::$jobClass(self::$worker, false, null);
87
        $time = time() - 1;
88
        $job->setExpiresAt(new \DateTime("@$time"))->fibonacci(5);
89
90
        $job = new self::$jobClass(self::$worker, false, null);
91
        $time = time() - 1;
92
        $job->setExpiresAt(new \DateTime("@$time"))->fibonacci(2);
93
94
        $job = new self::$jobClass(self::$worker, false, null);
95
        $job->fibonacci(1);
96
        $jobInQueue = self::$jobManager->getJob();
97
        self::assertNotNull($jobInQueue, 'There should be a job.');
98
        self::assertEquals(
99
            $job->getId(),
100
            $jobInQueue->getId(),
101
            'Job id returned by manager should be the same'
102
        );
103
    }
104
105 View Code Duplication
    public function testSaveJob()
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...
106
    {
107
        // Make sure a job proper type
108
        $failed = false;
109
        try {
110
            $job = new Job();
111
            self::$jobManager->save($job);
112
            $failed = true;
113
        } catch (\Exception $exception) {
114
            self::assertTrue(true);
115
        }
116
        self::assertFalse($failed);
117
118
        $job = new self::$jobClass(self::$worker, false, null);
119
        try {
120
            $job->setPriority(256)->fibonacci(1);
121
            $failed = true;
122
        } catch (\Exception $exception) {
123
            self::assertTrue(true);
124
        }
125
        self::assertFalse($failed);
126
127
        $job = new self::$jobClass(self::$worker, false, null);
128
        $job->setPriority(100)->fibonacci(1);
129
        self::assertNotNull($job->getId(), 'Job id should be generated');
130
131
        $jobInQueue = self::$jobManager->getJob();
132
        self::assertNotNull($jobInQueue, 'There should be a job.');
133
        self::$jobManager->saveHistory($jobInQueue);
134
135
        $job = new self::$jobClass(self::$worker, false, null);
136
        $job->fibonacci(1);
137
        self::assertNotNull($job->getId(), 'Job id should be generated');
138
139
        $failed = false;
140
        try {
141
            self::$jobManager->getJob('fibonacci');
142
            $failed = true;
143
        } catch (\Exception $exception) {
144
            self::assertTrue(true);
145
        }
146
        self::assertFalse($failed);
147
148
        $failed = false;
149
        try {
150
            self::$jobManager->getJob(null, 'fibonacci');
151
            $failed = true;
152
        } catch (\Exception $exception) {
153
            self::assertTrue(true);
154
        }
155
        self::assertFalse($failed);
156
157
        $jobInQueue = self::$jobManager->getJob();
158
        self::assertNotNull($jobInQueue, 'There should be a job.');
159
        self::assertEquals(
160
            $job->getId(),
161
            $jobInQueue->getId(),
162
            'Job id returned by manager should be the same'
163
        );
164
165
        $job->setStatus(BaseJob::STATUS_SUCCESS);
166
        $badJob = new Job();
167
        $failed = false;
168
        try {
169
            self::$jobManager->saveHistory($badJob);
170
            $failed = true;
171
        } catch (\Exception $exception) {
172
            self::assertTrue(true);
173
        }
174
        self::assertFalse($failed);
175
        self::$jobManager->saveHistory($jobInQueue);
176
    }
177
}
178