Completed
Push — master ( c6ec30...6c3722 )
by Matthew
18:20
created

BaseJobManagerTest::testGetStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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 testGetStatus() {
138
        $this->expectException('getStatus');
139
    }
140
141
    public function testPruneArchivedJobs()
142
    {
143
        try {
144
            $time = time() - 86400;
145
            self::$jobManager->pruneArchivedJobs(new \DateTime("@$time"));
146
            self::fail('Expected Exception');
147
        } catch (\Exception $exception) {
148
            self::assertTrue(true);
149
        }
150
    }
151
152
    /**
153
     * @param string $method
154
     */
155
    protected function expectingException($method)
156
    {
157
        try {
158
            self::$jobManager->$method();
159
            self::fail('Expected Exception');
160
        } catch (\Exception $exception) {
161
            self::assertTrue(true);
162
        }
163
    }
164
165
    /**
166
     * @outputBuffering disabled
167
     */
168
    public function testPerformance()
169
    {
170
        echo "\n".static::class.": Testing Performance\n";
171
        flush();
172
173
        $start = microtime(true);
174
        $jobsTotal = 100; // have to trim this down as Travis is slow.
175
        self::$jobManager->enableSorting = false; // Ignore priority
176
177
        for ($i = 0; $i < $jobsTotal; ++$i) {
178
            self::$worker->later()->fibonacci(1);
179
        }
180
181
        $total = microtime(true) - $start;
182
        echo "\nTotal of {$jobsTotal} jobs enqueued in {$total} seconds\n";
183
184
        try {
185
            $count = self::$jobManager->getJobCount();
186
            self::assertEquals($jobsTotal, $count);
187
        } catch (\Exception $e) {
188
            if ('Unsupported' !== $e->getMessage()) {
189
                throw $e;
190
            }
191
        }
192
193
        $start = microtime(true);
194
        $job = null;
195
        for ($i = 0; $i < $jobsTotal; ++$i) {
196
            $job = self::$jobManager->getJob();
197
        }
198
        $total = microtime(true) - $start;
199
        echo "Total of {$jobsTotal} jobs dequeued in {$total} seconds\n";
200
        self::assertNotNull($job, 'The last job in queue...');
201
202
        $nextJob = self::$jobManager->getJob();
203
        self::assertNull($nextJob, "Shouldn't be any jobs left in queue");
204
    }
205
}
206