Completed
Branch feature/redis (23fb44)
by Matthew
05:31
created

BaseJobManagerTest::testDeleteJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Manager;
4
5
use Dtc\QueueBundle\Manager\JobManagerInterface;
6
use Dtc\QueueBundle\Manager\RunManager;
7
use Dtc\QueueBundle\Manager\Worker;
8
use Dtc\QueueBundle\ODM\JobTimingManager;
9
use PHPUnit\Framework\TestCase;
10
11
abstract class BaseJobManagerTest extends TestCase
12
{
13
    /** @var Worker */
14
    public static $worker;
15
16
    /** @var string */
17
    public static $jobClass;
18
19
    /** @var JobManagerInterface */
20
    public static $jobManager;
21
22
    /** @var RunManager */
23
    public static $runManager;
24
25
    /** @var JobTimingManager */
26
    public static $jobTimingManager;
27
28
    public static function setUpBeforeClass()
29
    {
30
        self::$jobClass = self::$jobManager->getJobClass();
31
        self::$worker->setJobManager(self::$jobManager);
32
    }
33
34
    public function testSaveJob()
35
    {
36
        $job = $this->getJob();
37
        $jobInQueue = self::$jobManager->getJob();
38
        self::assertNotNull($jobInQueue, 'There should be a job.');
39
        self::assertEquals(
40
            $job->getId(),
41
            $jobInQueue->getId(),
42
            'Job id returned by manager should be the same'
43
        );
44
        self::$jobManager->deleteJob($job);
45
    }
46
47
    protected function getJob()
48
    {
49
        $job = new self::$jobClass(self::$worker, false, null);
50
        $job->fibonacci(1);
51
        self::assertNotNull($job->getId(), 'Job id should be generated');
52
53
        return $job;
54
    }
55
56
    public function testGetJobByWorker()
57
    {
58
        $job = $this->getJob();
59
        $jobInQueue = self::$jobManager->getJob(self::$worker->getName());
60
        self::assertEquals(
61
            $job->getId(),
62
            $jobInQueue->getId(),
63
            'Job id returned by manager should be the same'
64
        );
65
    }
66
67
    public function testDeleteJob()
68
    {
69
        $job = $this->getJob();
70
        self::$jobManager->deleteJob($job);
71
72
        $nextJob = self::$jobManager->getJob();
73
        self::assertNull($nextJob, "Shouldn't be any jobs left in queue");
74
75
        return $job;
76
    }
77
78
    /**
79
     * Not all managers will support job priority.
80
     */
81
    public function testJobPriority()
82
    {
83
        $firstJob = new self::$jobClass(self::$worker, false, 12);
84
        $firstJob->fibonacci(1);
85
        self::assertNotNull($firstJob->getId(), 'Job id should be generated');
86
87
        $secondJob = new self::$jobClass(self::$worker, false, 1);
88
        $secondJob->fibonacci(1);
89
        self::assertNotNull($secondJob->getId(), 'Job id should be generated');
90
91
        $thirdJob = new self::$jobClass(self::$worker, false, 5);
92
        $thirdJob->fibonacci(1);
93
        self::assertNotNull($thirdJob->getId(), 'Job id should be generated');
94
95
        $jobInQueue = self::$jobManager->getJob();
96
        self::assertNotNull($jobInQueue, 'There should be a job.');
97
        self::assertEquals(
98
            $secondJob->getId(),
99
            $jobInQueue->getId(),
100
            'Second job id should be returned - lower number unload first'
101
        );
102
103
        $jobInQueue = self::$jobManager->getJob();
104
        self::assertEquals(
105
            $thirdJob->getId(),
106
            $jobInQueue->getId(),
107
            'Third job id should be returned - lower number unload first'
108
        );
109
110
        $jobInQueue = self::$jobManager->getJob();
111
        self::assertEquals(
112
            $firstJob->getId(),
113
            $jobInQueue->getId(),
114
            'First job id should be returned - lower number unload first'
115
        );
116
    }
117
118
    public function testResetExceptionJobs()
119
    {
120
        $this->expectingException('resetExceptionJobs');
121
    }
122
123
    public function testResetStalledJobs()
124
    {
125
        $this->expectingException('resetStalledJobs');
126
    }
127
128
    public function testPruneStalledJobs()
129
    {
130
        $this->expectingException('pruneStalledJobs');
131
    }
132
133
    public function testPruneExceptionJobs()
134
    {
135
        $this->expectingException('pruneExceptionJobs');
136
    }
137
138
    public function testPruneExpiredJobs()
139
    {
140
        $this->expectingException('pruneExpiredJobs');
141
    }
142
143
    public function testGetStatus()
144
    {
145
        $this->expectingException('getStatus');
146
    }
147
148
    public function testPruneArchivedJobs()
149
    {
150
        $failed = false;
151
        try {
152
            $time = time() - 86400;
153
            self::$jobManager->pruneArchivedJobs(new \DateTime("@$time"));
154
            $failed = true;
155
        } catch (\Exception $exception) {
156
            self::assertTrue(true);
157
        }
158
        self::assertFalse($failed);
159
    }
160
161
    /**
162
     * @param string $method
163
     */
164 View Code Duplication
    protected function expectingException($method)
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...
165
    {
166
        $failed = false;
167
        try {
168
            self::$jobManager->$method();
169
            $failed = true;
170
        } catch (\Exception $exception) {
171
            self::assertTrue(true);
172
        }
173
        self::assertFalse($failed);
174
    }
175
176
    /**
177
     * @outputBuffering disabled
178
     */
179
    public function testPerformance()
180
    {
181
        echo "\n".static::class.": Testing Performance\n";
182
        flush();
183
184
        $start = microtime(true);
185
        $jobsTotal = 100; // have to trim this down as Travis is slow.
186
        self::$jobManager->enableSorting = false; // Ignore priority
0 ignored issues
show
Bug introduced by
Accessing enableSorting on the interface Dtc\QueueBundle\Manager\JobManagerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
187
188
        $limit = 10000;
189
        while ($job = self::$jobManager->getJob() && --$limit) {
0 ignored issues
show
Unused Code introduced by
$job is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $job = (self::$jobManager->getJob() && --$limit), Probably Intended Meaning: ($job = self::$jobManager->getJob()) && --$limit
Loading history...
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
190
            // Dequeue all existing jobs
191
        }
192
        self::assertGreaterThan(0, $limit);
193
194
        for ($i = 0; $i < $jobsTotal; ++$i) {
195
            self::$worker->later()->fibonacci(1);
196
        }
197
198
        $total = microtime(true) - $start;
199
        echo "\nTotal of {$jobsTotal} jobs enqueued in {$total} seconds\n";
200
201
        try {
202
            $count = self::$jobManager->getJobCount();
203
            self::assertEquals($jobsTotal, $count);
204
        } catch (\Exception $e) {
205
            if ('Unsupported' !== $e->getMessage()) {
206
                throw $e;
207
            }
208
        }
209
210
        $start = microtime(true);
211
        $job = null;
212
        for ($i = 0; $i < $jobsTotal; ++$i) {
213
            $job = self::$jobManager->getJob();
214
        }
215
        $total = microtime(true) - $start;
216
        echo "Total of {$jobsTotal} jobs dequeued in {$total} seconds\n";
217
        self::assertNotNull($job, 'The last job in queue is null');
218
219
        $nextJob = self::$jobManager->getJob();
220
        self::assertNull($nextJob, "Shouldn't be any jobs left in queue");
221
    }
222
}
223