Passed
Pull Request — master (#57)
by Matthew
07:36
created

SaveJobTrait::testSaveJob()   C

Complexity

Conditions 7
Paths 72

Size

Total Lines 75
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 6.5862
c 0
b 0
f 0
cc 7
eloc 57
nc 72
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\Model\Job;
6
use Dtc\QueueBundle\Model\BaseJob;
7
8
trait SaveJobTrait
9
{
10
    public function testSaveJob()
11
    {
12
        $this->drain();
13
        // Make sure a job proper type
14
        $failed = false;
15
        try {
16
            $job = new Job();
17
            self::$jobManager->save($job);
18
            $failed = true;
19
        } catch (\Exception $exception) {
20
            self::assertTrue(true);
21
        }
22
        self::assertFalse($failed);
23
24
        if (null !== self::$jobManager->getMaxPriority()) {
25
            $job = new self::$jobClass(self::$worker, false, null);
26
            try {
27
                $job->setPriority(256)->fibonacci(1);
28
                $failed = true;
29
            } catch (\Exception $exception) {
30
                self::assertTrue(true);
31
            }
32
            self::assertFalse($failed);
33
34
            $job = new self::$jobClass(self::$worker, false, null);
35
            $job->setPriority(100)->fibonacci(1);
36
            self::assertNotNull($job->getId(), 'Job id should be generated');
37
38
            $jobInQueue = self::$jobManager->getJob();
39
            self::assertNotNull($jobInQueue, 'There should be a job.');
40
            self::$jobManager->saveHistory($jobInQueue);
41
        }
42
43
        $job = new self::$jobClass(self::$worker, false, null);
44
        $job->fibonacci(1);
45
        self::assertNotNull($job->getId(), 'Job id should be generated');
46
47
        $failed = false;
48
        try {
49
            self::$jobManager->getJob('fibonacci');
50
            $failed = true;
51
        } catch (\Exception $exception) {
52
            self::assertTrue(true);
53
        }
54
        self::assertFalse($failed);
55
56
        $failed = false;
57
        try {
58
            self::$jobManager->getJob(null, 'fibonacci');
59
            $failed = true;
60
        } catch (\Exception $exception) {
61
            self::assertTrue(true);
62
        }
63
        self::assertFalse($failed);
64
65
        $jobInQueue = self::$jobManager->getJob();
66
        self::assertNotNull($jobInQueue, 'There should be a job.');
67
        self::assertEquals(
68
            $job->getId(),
69
            $jobInQueue->getId(),
70
            'Job id returned by manager should be the same'
71
        );
72
73
        $job->setStatus(BaseJob::STATUS_SUCCESS);
74
        $badJob = new Job();
75
        $failed = false;
76
        try {
77
            self::$jobManager->saveHistory($badJob);
78
            $failed = true;
79
        } catch (\Exception $exception) {
80
            self::assertTrue(true);
81
        }
82
        self::assertFalse($failed);
83
        self::$jobManager->saveHistory($jobInQueue);
84
    }
85
}
86