|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter Queue. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Tests; |
| 15 | + |
| 16 | +use CodeIgniter\I18n\Time; |
| 17 | +use CodeIgniter\Queue\Entities\QueueJob; |
| 18 | +use CodeIgniter\Test\ReflectionHelper; |
| 19 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 20 | +use Tests\Support\Config\Queue as QueueConfig; |
| 21 | +use Tests\Support\Database\Seeds\TestDatabaseQueueSeeder; |
| 22 | +use Tests\Support\TestCase; |
| 23 | + |
| 24 | +/** |
| 25 | + * @internal |
| 26 | + */ |
| 27 | +final class PushAndPopWithDelayTest extends TestCase |
| 28 | +{ |
| 29 | + use ReflectionHelper; |
| 30 | + |
| 31 | + protected $seed = TestDatabaseQueueSeeder::class; |
| 32 | + private QueueConfig $config; |
| 33 | + |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->config = config(QueueConfig::class); |
| 39 | + } |
| 40 | + |
| 41 | + public static function handlerProvider(): iterable |
| 42 | + { |
| 43 | + return [ |
| 44 | + [ |
| 45 | + 'database', // name |
| 46 | + 'CodeIgniter\Queue\Handlers\DatabaseHandler', // class |
| 47 | + ], |
| 48 | + [ |
| 49 | + 'redis', |
| 50 | + 'CodeIgniter\Queue\Handlers\RedisHandler', |
| 51 | + ], |
| 52 | + [ |
| 53 | + 'predis', |
| 54 | + 'CodeIgniter\Queue\Handlers\PredisHandler', |
| 55 | + ], |
| 56 | + ]; |
| 57 | + } |
| 58 | + |
| 59 | + #[DataProvider('handlerProvider')] |
| 60 | + public function testPushAndPopWithDelay(string $name, string $class): void |
| 61 | + { |
| 62 | + Time::setTestNow('2023-12-29 14:15:16'); |
| 63 | + |
| 64 | + $handler = new $class($this->config); |
| 65 | + $result = $handler->setDelay(MINUTE)->push('queue-delay', 'success', ['key1' => 'value1']); |
| 66 | + |
| 67 | + $this->assertTrue($result); |
| 68 | + |
| 69 | + $result = $handler->push('queue-delay', 'success', ['key2' => 'value2']); |
| 70 | + |
| 71 | + $this->assertTrue($result); |
| 72 | + |
| 73 | + if ($name === 'database') { |
| 74 | + $this->seeInDatabase('queue_jobs', [ |
| 75 | + 'queue' => 'queue-delay', |
| 76 | + 'payload' => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]), |
| 77 | + 'available_at' => 1703859376, |
| 78 | + ]); |
| 79 | + |
| 80 | + $this->seeInDatabase('queue_jobs', [ |
| 81 | + 'queue' => 'queue-delay', |
| 82 | + 'payload' => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]), |
| 83 | + 'available_at' => 1703859316, |
| 84 | + ]); |
| 85 | + } |
| 86 | + |
| 87 | + $result = $handler->pop('queue-delay', ['default']); |
| 88 | + $this->assertInstanceOf(QueueJob::class, $result); |
| 89 | + $payload = ['job' => 'success', 'data' => ['key2' => 'value2']]; |
| 90 | + $this->assertSame($payload, $result->payload); |
| 91 | + |
| 92 | + $result = $handler->pop('queue-delay', ['default']); |
| 93 | + $this->assertNull($result); |
| 94 | + |
| 95 | + // add 1 minute |
| 96 | + Time::setTestNow('2023-12-29 14:16:16'); |
| 97 | + |
| 98 | + $result = $handler->pop('queue-delay', ['default']); |
| 99 | + $this->assertInstanceOf(QueueJob::class, $result); |
| 100 | + $payload = ['job' => 'success', 'data' => ['key1' => 'value1']]; |
| 101 | + $this->assertSame($payload, $result->payload); |
| 102 | + } |
| 103 | +} |
0 commit comments