Completed
Push — master ( 8ffdf8...f67228 )
by Matthew
08:43
created

SaveJobTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 78
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C testSaveJob() 0 75 7
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();
0 ignored issues
show
Bug introduced by
It seems like drain() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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