Completed
Pull Request — master (#30)
by Matthew
06:35
created

AutoRetryTrait::testAutoRetryOnException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 46

Duplication

Lines 55
Ratio 100 %

Importance

Changes 0
Metric Value
dl 55
loc 55
rs 9.7692
c 0
b 0
f 0
cc 1
eloc 46
nc 1
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\RetryableJob;
6
use Dtc\QueueBundle\Model\BaseJob;
7
8
trait AutoRetryTrait
9
{
10 View Code Duplication
    public function testAutoRetryOnFailure()
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...
11
    {
12
        /** @var \Dtc\QueueBundle\ODM\JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
13
        $jobManager = self::$jobManager;
14
        $jobManager->setAutoRetryOnFailure(false);
15
        $job = new self::$jobClass(self::$worker, false, null);
16
        $job->fibonacci(1);
17
        $job->setMaxRetries(1);
18
        $job->setMaxFailures(1);
19
        $job->setStatus(BaseJob::STATUS_FAILURE);
20
        $jobManager->saveHistory($job);
21
        self::assertEquals(RetryableJob::STATUS_MAX_FAILURES, $job->getStatus());
22
23
        $job = new self::$jobClass(self::$worker, false, null);
24
        $job->fibonacci(1);
25
        $job->setMaxRetries(1);
26
        $job->setMaxFailures(2);
27
        $job->setStatus(BaseJob::STATUS_FAILURE);
28
        $jobManager->saveHistory($job);
29
        self::assertEquals(RetryableJob::STATUS_FAILURE, $job->getStatus());
30
31
        $jobManager->setAutoRetryOnFailure(true);
32
        $job = new self::$jobClass(self::$worker, false, null);
33
        $job->fibonacci(1);
34
        $job->setMaxRetries(1);
35
        $job->setMaxFailures(2);
36
        $job->setStatus(BaseJob::STATUS_FAILURE);
37
        $jobManager->saveHistory($job);
38
        self::assertEquals(RetryableJob::STATUS_NEW, $job->getStatus());
39
40
        $job->setStatus(BaseJob::STATUS_FAILURE);
41
        $jobManager->saveHistory($job);
42
        self::assertEquals(RetryableJob::STATUS_MAX_FAILURES, $job->getStatus());
43
44
        $job = new self::$jobClass(self::$worker, false, null);
45
        $job->fibonacci(1);
46
        $job->setMaxRetries(1);
47
        $job->setMaxFailures(3);
48
        $job->setStatus(BaseJob::STATUS_FAILURE);
49
        $jobManager->saveHistory($job);
50
        self::assertEquals(RetryableJob::STATUS_NEW, $job->getStatus());
51
52
        $job->setStatus(BaseJob::STATUS_FAILURE);
53
        $jobManager->saveHistory($job);
54
        self::assertEquals(RetryableJob::STATUS_MAX_RETRIES, $job->getStatus());
55
56
        $jobManager->setAutoRetryOnFailure(false);
57
        $job = new self::$jobClass(self::$worker, false, null);
58
        $job->fibonacci(1);
59
        $job->setMaxRetries(1);
60
        $job->setMaxFailures(1);
61
        $job->setStatus(BaseJob::STATUS_FAILURE);
62
        $jobManager->saveHistory($job);
63
        self::assertEquals(RetryableJob::STATUS_MAX_FAILURES, $job->getStatus());
64
    }
65
66 View Code Duplication
    public function testAutoRetryOnException()
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...
67
    {
68
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
69
        $jobManager = self::$jobManager;
70
        $jobManager->setAutoRetryOnFailure(false);
71
        $job = new self::$jobClass(self::$worker, false, null);
72
        $job->fibonacci(1);
73
        $job->setMaxRetries(1);
74
        $job->setMaxExceptions(1);
75
        $job->setStatus(BaseJob::STATUS_EXCEPTION);
76
        $jobManager->saveHistory($job);
77
        self::assertEquals(RetryableJob::STATUS_MAX_EXCEPTIONS, $job->getStatus());
78
79
        $job = new self::$jobClass(self::$worker, false, null);
80
        $job->fibonacci(1);
81
        $job->setMaxRetries(1);
82
        $job->setMaxExceptions(2);
83
        $job->setStatus(BaseJob::STATUS_EXCEPTION);
84
        $jobManager->saveHistory($job);
85
        self::assertEquals(RetryableJob::STATUS_EXCEPTION, $job->getStatus());
86
87
        $jobManager->setAutoRetryOnException(true);
88
        $job = new self::$jobClass(self::$worker, false, null);
89
        $job->fibonacci(1);
90
        $job->setMaxRetries(1);
91
        $job->setMaxExceptions(2);
92
        $job->setStatus(BaseJob::STATUS_EXCEPTION);
93
        $jobManager->saveHistory($job);
94
        self::assertEquals(RetryableJob::STATUS_NEW, $job->getStatus());
95
96
        $job->setStatus(BaseJob::STATUS_EXCEPTION);
97
        $jobManager->saveHistory($job);
98
        self::assertEquals(RetryableJob::STATUS_MAX_EXCEPTIONS, $job->getStatus());
99
100
        $job = new self::$jobClass(self::$worker, false, null);
101
        $job->fibonacci(1);
102
        $job->setMaxRetries(1);
103
        $job->setMaxExceptions(3);
104
        $job->setStatus(BaseJob::STATUS_EXCEPTION);
105
        $jobManager->saveHistory($job);
106
        self::assertEquals(RetryableJob::STATUS_NEW, $job->getStatus());
107
108
        $job->setStatus(BaseJob::STATUS_EXCEPTION);
109
        $jobManager->saveHistory($job);
110
        self::assertEquals(RetryableJob::STATUS_MAX_RETRIES, $job->getStatus());
111
112
        $jobManager->setAutoRetryOnFailure(false);
113
        $job = new self::$jobClass(self::$worker, false, null);
114
        $job->fibonacci(1);
115
        $job->setMaxRetries(1);
116
        $job->setMaxExceptions(1);
117
        $job->setStatus(BaseJob::STATUS_EXCEPTION);
118
        $jobManager->saveHistory($job);
119
        self::assertEquals(RetryableJob::STATUS_MAX_EXCEPTIONS, $job->getStatus());
120
    }
121
}
122