Completed
Push — master ( 17631a...07adb9 )
by Matthew
05:11
created

BaseJobManagerTest::testDeleteJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
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
    protected function getJob()
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
    public function testDeleteJob()
53
    {
54
        $job = $this->getJob();
55
        self::$jobManager->deleteJob($job);
56
57
        $nextJob = self::$jobManager->getJob();
58
        self::assertNull($nextJob, "Shouldn't be any jobs left in queue");
59
60
        return $job;
61
    }
62
63
    /**
64
     * Not all managers will support job priority.
65
     */
66
    public function testJobPriority()
67
    {
68
        $firstJob = new self::$jobClass(self::$worker, false, 12);
69
        $firstJob->fibonacci(1);
70
        self::assertNotNull($firstJob->getId(), 'Job id should be generated');
71
72
        $secondJob = new self::$jobClass(self::$worker, false, 1);
73
        $secondJob->fibonacci(1);
74
        self::assertNotNull($secondJob->getId(), 'Job id should be generated');
75
76
        $thirdJob = new self::$jobClass(self::$worker, false, 5);
77
        $thirdJob->fibonacci(1);
78
        self::assertNotNull($thirdJob->getId(), 'Job id should be generated');
79
80
        $jobInQueue = self::$jobManager->getJob();
81
        self::assertNotNull($jobInQueue, 'There should be a job.');
82
        self::assertEquals(
83
            $secondJob->getId(),
84
            $jobInQueue->getId(),
85
            'Second job id should be returned - lower number unload first'
86
        );
87
88
        $jobInQueue = self::$jobManager->getJob();
89
        self::assertEquals(
90
            $thirdJob->getId(),
91
            $jobInQueue->getId(),
92
            'Third job id should be returned - lower number unload first'
93
        );
94
95
        $jobInQueue = self::$jobManager->getJob();
96
        self::assertEquals(
97
            $firstJob->getId(),
98
            $jobInQueue->getId(),
99
            'First job id should be returned - lower number unload first'
100
        );
101
    }
102
103
    public function testResetErroneousJobs()
104
    {
105
        $this->expectingException('resetErroneousJobs');
106
    }
107
108
    public function testResetStalledJobs()
109
    {
110
        $this->expectingException('resetStalledJobs');
111
    }
112
113
    public function testPruneStalledJobs()
114
    {
115
        $this->expectingException('pruneStalledJobs');
116
    }
117
118
    public function testPruneErroneousJobs()
119
    {
120
        $this->expectingException('pruneErroneousJobs');
121
    }
122
123
    public function testPruneExpiredJobs()
124
    {
125
        $this->expectingException('pruneExpiredJobs');
126
    }
127
128
    public function testGetStatus()
129
    {
130
        $this->expectingException('getStatus');
131
    }
132
133
    public function testPruneArchivedJobs()
134
    {
135
        $failed = false;
136
        try {
137
            $time = time() - 86400;
138
            self::$jobManager->pruneArchivedJobs(new \DateTime("@$time"));
139
            $failed = true;
140
        } catch (\Exception $exception) {
141
            self::assertTrue(true);
142
        }
143
        self::assertFalse($failed);
144
    }
145
146
    /**
147
     * @param string $method
148
     */
149
    protected function expectingException($method)
150
    {
151
        $failed = false;
152
        try {
153
            self::$jobManager->$method();
154
            $failed = true;
155
        } catch (\Exception $exception) {
156
            self::assertTrue(true);
157
        }
158
        self::assertFalse($failed);
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
            $job = self::$jobManager->getJob();
193
        }
194
        $total = microtime(true) - $start;
195
        echo "Total of {$jobsTotal} jobs dequeued in {$total} seconds\n";
196
        self::assertNotNull($job, 'The last job in queue...');
197
198
        $nextJob = self::$jobManager->getJob();
199
        self::assertNull($nextJob, "Shouldn't be any jobs left in queue");
200
    }
201
}
202