Completed
Push — master ( 203a9c...2f9634 )
by Matthew
05:23 queued 25s
created

BaseJobManagerTest::getNewJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

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