|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Michalsn\CodeIgniterQueue\Entities\QueueJob; |
| 7 | +use Michalsn\CodeIgniterQueue\Enums\Status; |
| 8 | +use Michalsn\CodeIgniterQueue\Exceptions\QueueException; |
| 9 | +use Michalsn\CodeIgniterQueue\Handlers\DatabaseHandler; |
| 10 | +use Michalsn\CodeIgniterQueue\Models\QueueJobFailedModel; |
| 11 | +use ReflectionException; |
| 12 | +use Tests\Support\Config\Queue as QueueConfig; |
| 13 | +use Tests\Support\Database\Seeds\TestQueueSeeder; |
| 14 | +use Tests\Support\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * @internal |
| 18 | + */ |
| 19 | +final class DatabaseHandlerTest extends TestCase |
| 20 | +{ |
| 21 | + protected $seed = TestQueueSeeder::class; |
| 22 | + protected QueueConfig $config; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->config = config(QueueConfig::class); |
| 29 | + } |
| 30 | + |
| 31 | + public function testDatabaseHandler() |
| 32 | + { |
| 33 | + $handler = new DatabaseHandler($this->config); |
| 34 | + $this->assertInstanceOf(DatabaseHandler::class, $handler); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @throws ReflectionException |
| 39 | + */ |
| 40 | + public function testPush() |
| 41 | + { |
| 42 | + $handler = new DatabaseHandler($this->config); |
| 43 | + $result = $handler->push('queue', 'success', ['key' => 'value']); |
| 44 | + |
| 45 | + $this->assertTrue($result); |
| 46 | + $this->seeInDatabase('queue_jobs', [ |
| 47 | + 'queue' => 'queue', |
| 48 | + 'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]), |
| 49 | + ]); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @throws ReflectionException |
| 54 | + */ |
| 55 | + public function testPushException() |
| 56 | + { |
| 57 | + $this->expectException(QueueException::class); |
| 58 | + $this->expectExceptionMessage('This job name is not defined in the $jobHandlers array.'); |
| 59 | + |
| 60 | + $handler = new DatabaseHandler($this->config); |
| 61 | + $handler->push('queue', 'not-exists', ['key' => 'value']); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @throws ReflectionException |
| 66 | + */ |
| 67 | + public function testPop() |
| 68 | + { |
| 69 | + $handler = new DatabaseHandler($this->config); |
| 70 | + $result = $handler->pop('queue1'); |
| 71 | + |
| 72 | + $this->assertInstanceOf(QueueJob::class, $result); |
| 73 | + $this->seeInDatabase('queue_jobs', [ |
| 74 | + 'status' => Status::RESERVED->value, |
| 75 | + 'available_at' => 1697269860, |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @throws ReflectionException |
| 81 | + */ |
| 82 | + public function testPopEmpty() |
| 83 | + { |
| 84 | + $handler = new DatabaseHandler($this->config); |
| 85 | + $result = $handler->pop('queue123'); |
| 86 | + |
| 87 | + $this->assertNull($result); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @throws ReflectionException |
| 92 | + */ |
| 93 | + public function testLater() |
| 94 | + { |
| 95 | + $handler = new DatabaseHandler($this->config); |
| 96 | + $queueJob = $handler->pop('queue1'); |
| 97 | + |
| 98 | + $this->seeInDatabase('queue_jobs', [ |
| 99 | + 'id' => 2, |
| 100 | + 'status' => Status::RESERVED->value, |
| 101 | + ]); |
| 102 | + |
| 103 | + $result = $handler->later($queueJob, 60); |
| 104 | + |
| 105 | + $this->assertTrue($result); |
| 106 | + $this->seeInDatabase('queue_jobs', [ |
| 107 | + 'id' => 2, |
| 108 | + 'status' => Status::PENDING->value, |
| 109 | + ]); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @throws ReflectionException |
| 114 | + */ |
| 115 | + public function testFailedAndKeepJob() |
| 116 | + { |
| 117 | + $handler = new DatabaseHandler($this->config); |
| 118 | + $queueJob = $handler->pop('queue1'); |
| 119 | + |
| 120 | + $err = new Exception('Sample exception'); |
| 121 | + $result = $handler->failed($queueJob, $err, true); |
| 122 | + |
| 123 | + $this->assertTrue($result); |
| 124 | + $this->dontSeeInDatabase('queue_jobs', [ |
| 125 | + 'id' => 2, |
| 126 | + ]); |
| 127 | + $this->seeInDatabase('queue_jobs_failed', [ |
| 128 | + 'id' => 2, |
| 129 | + 'connection' => 'database', |
| 130 | + 'queue' => 'queue1', |
| 131 | + ]); |
| 132 | + } |
| 133 | + |
| 134 | + public function testFailedAndDontKeepJob() |
| 135 | + { |
| 136 | + $handler = new DatabaseHandler($this->config); |
| 137 | + $queueJob = $handler->pop('queue1'); |
| 138 | + |
| 139 | + $err = new Exception('Sample exception'); |
| 140 | + $result = $handler->failed($queueJob, $err, false); |
| 141 | + |
| 142 | + $this->assertTrue($result); |
| 143 | + $this->dontSeeInDatabase('queue_jobs', [ |
| 144 | + 'id' => 2, |
| 145 | + ]); |
| 146 | + $this->dontSeeInDatabase('queue_jobs_failed', [ |
| 147 | + 'id' => 2, |
| 148 | + 'connection' => 'database', |
| 149 | + 'queue' => 'queue1', |
| 150 | + ]); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @throws ReflectionException |
| 155 | + */ |
| 156 | + public function testDoneAndKeepJob() |
| 157 | + { |
| 158 | + $handler = new DatabaseHandler($this->config); |
| 159 | + $queueJob = $handler->pop('queue1'); |
| 160 | + |
| 161 | + $result = $handler->done($queueJob, true); |
| 162 | + |
| 163 | + $this->assertTrue($result); |
| 164 | + $this->seeInDatabase('queue_jobs', [ |
| 165 | + 'id' => 2, |
| 166 | + 'status' => Status::DONE->value, |
| 167 | + ]); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @throws ReflectionException |
| 172 | + */ |
| 173 | + public function testDoneAndDontKeepJob() |
| 174 | + { |
| 175 | + $handler = new DatabaseHandler($this->config); |
| 176 | + $queueJob = $handler->pop('queue1'); |
| 177 | + |
| 178 | + $result = $handler->done($queueJob, false); |
| 179 | + |
| 180 | + $this->assertTrue($result); |
| 181 | + $this->dontSeeInDatabase('queue_jobs', [ |
| 182 | + 'id' => 2, |
| 183 | + ]); |
| 184 | + } |
| 185 | + |
| 186 | + public function testClear() |
| 187 | + { |
| 188 | + $handler = new DatabaseHandler($this->config); |
| 189 | + $result = $handler->clear('queue1'); |
| 190 | + |
| 191 | + $this->assertTrue($result); |
| 192 | + |
| 193 | + $this->dontSeeInDatabase('queue_jobs', [ |
| 194 | + 'id' => 1, |
| 195 | + ]); |
| 196 | + $this->dontSeeInDatabase('queue_jobs', [ |
| 197 | + 'id' => 2, |
| 198 | + ]); |
| 199 | + } |
| 200 | + |
| 201 | + public function testRetry() |
| 202 | + { |
| 203 | + $handler = new DatabaseHandler($this->config); |
| 204 | + $count = $handler->retry(1, 'queue1'); |
| 205 | + |
| 206 | + $this->assertSame($count, 1); |
| 207 | + |
| 208 | + $this->seeInDatabase('queue_jobs', [ |
| 209 | + 'id' => 3, |
| 210 | + 'queue' => 'queue1', |
| 211 | + 'payload' => json_encode(['job' => 'failure', 'data' => []]), |
| 212 | + ]); |
| 213 | + $this->dontSeeInDatabase('queue_jobs_failed', [ |
| 214 | + 'id' => 1, |
| 215 | + ]); |
| 216 | + } |
| 217 | + |
| 218 | + public function testForget() |
| 219 | + { |
| 220 | + $handler = new DatabaseHandler($this->config); |
| 221 | + $result = $handler->forget(1); |
| 222 | + |
| 223 | + $this->assertTrue($result); |
| 224 | + |
| 225 | + $this->dontSeeInDatabase('queue_jobs_failed', [ |
| 226 | + 'id' => 1, |
| 227 | + ]); |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * @throws ReflectionException |
| 232 | + */ |
| 233 | + public function testFlush() |
| 234 | + { |
| 235 | + $handler = new DatabaseHandler($this->config); |
| 236 | + $queueJob = $handler->pop('queue1'); |
| 237 | + |
| 238 | + $err = new Exception('Sample exception here'); |
| 239 | + $result = $handler->failed($queueJob, $err, true); |
| 240 | + |
| 241 | + $this->assertTrue($result); |
| 242 | + |
| 243 | + // Set first record as older than 1 hour |
| 244 | + model(QueueJobFailedModel::class)->builder()->where('id', 1)->update(['failed_at' => 1697269860]); |
| 245 | + |
| 246 | + $handler->flush(1, 'queue1'); |
| 247 | + $this->dontSeeInDatabase('queue_jobs_failed', [ |
| 248 | + 'id' => 1, |
| 249 | + ]); |
| 250 | + $this->seeInDatabase('queue_jobs_failed', [ |
| 251 | + 'id' => 2, |
| 252 | + ]); |
| 253 | + } |
| 254 | + |
| 255 | + public function testFlushAll() |
| 256 | + { |
| 257 | + $handler = new DatabaseHandler($this->config); |
| 258 | + $handler->flush(null, null); |
| 259 | + $this->dontSeeInDatabase('queue_jobs_failed', [ |
| 260 | + 'id' => 1, |
| 261 | + ]); |
| 262 | + } |
| 263 | + |
| 264 | + public function testListFailed() |
| 265 | + { |
| 266 | + $handler = new DatabaseHandler($this->config); |
| 267 | + $list = $handler->listFailed('queue1'); |
| 268 | + $this->assertCount(1, $list); |
| 269 | + } |
| 270 | +} |
0 commit comments