|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Queue\Tests\Unit\Stubs; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Yiisoft\Queue\Message\IdEnvelope; |
| 9 | +use Yiisoft\Queue\Message\Message; |
| 10 | +use Yiisoft\Queue\Message\MessageInterface; |
| 11 | +use Yiisoft\Queue\MessageStatus; |
| 12 | +use Yiisoft\Queue\Stubs\InMemoryAdapter; |
| 13 | + |
| 14 | +final class InMemoryAdapterTest extends TestCase |
| 15 | +{ |
| 16 | + public function testPush(): void |
| 17 | + { |
| 18 | + $adapter = new InMemoryAdapter(); |
| 19 | + |
| 20 | + $envelope1 = $adapter->push(new Message('test', 'a')); |
| 21 | + $envelope2 = $adapter->push(new Message('test', 'b')); |
| 22 | + $envelope3 = $adapter->push(new Message('test', 'c')); |
| 23 | + |
| 24 | + $this->assertInstanceOf(IdEnvelope::class, $envelope1); |
| 25 | + $this->assertInstanceOf(IdEnvelope::class, $envelope2); |
| 26 | + $this->assertInstanceOf(IdEnvelope::class, $envelope3); |
| 27 | + |
| 28 | + $this->assertSame(0, $envelope1->getId()); |
| 29 | + $this->assertSame('a', $envelope1->getMessage()->getData()); |
| 30 | + $this->assertSame(1, $envelope2->getId()); |
| 31 | + $this->assertSame('b', $envelope2->getMessage()->getData()); |
| 32 | + $this->assertSame(2, $envelope3->getId()); |
| 33 | + $this->assertSame('c', $envelope3->getMessage()->getData()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testStatusWaitingForPushedMessage(): void |
| 37 | + { |
| 38 | + $adapter = new InMemoryAdapter(); |
| 39 | + $envelope = $adapter->push(new Message('test', null)); |
| 40 | + |
| 41 | + $this->assertInstanceOf(IdEnvelope::class, $envelope); |
| 42 | + $this->assertSame(MessageStatus::WAITING, $adapter->status($envelope->getId())); |
| 43 | + } |
| 44 | + |
| 45 | + public function testStatusDoneAfterProcessing(): void |
| 46 | + { |
| 47 | + $adapter = new InMemoryAdapter(); |
| 48 | + $envelope = $adapter->push(new Message('test', null)); |
| 49 | + |
| 50 | + $adapter->runExisting(static fn() => true); |
| 51 | + |
| 52 | + $this->assertInstanceOf(IdEnvelope::class, $envelope); |
| 53 | + $this->assertSame(MessageStatus::DONE, $adapter->status($envelope->getId())); |
| 54 | + } |
| 55 | + |
| 56 | + public function testStatusNotFoundForNonExistentId(): void |
| 57 | + { |
| 58 | + $adapter = new InMemoryAdapter(); |
| 59 | + |
| 60 | + $this->assertSame(MessageStatus::NOT_FOUND, $adapter->status(99)); |
| 61 | + } |
| 62 | + |
| 63 | + public function testStatusNotFoundForNegativeId(): void |
| 64 | + { |
| 65 | + $adapter = new InMemoryAdapter(); |
| 66 | + |
| 67 | + $this->assertSame(MessageStatus::NOT_FOUND, $adapter->status(-1)); |
| 68 | + } |
| 69 | + |
| 70 | + public function testStatusAcceptsStringId(): void |
| 71 | + { |
| 72 | + $adapter = new InMemoryAdapter(); |
| 73 | + $envelope = $adapter->push(new Message('test', null)); |
| 74 | + |
| 75 | + $this->assertInstanceOf(IdEnvelope::class, $envelope); |
| 76 | + $this->assertSame(MessageStatus::WAITING, $adapter->status((string) $envelope->getId())); |
| 77 | + } |
| 78 | + |
| 79 | + public function testRunExistingProcessesAllMessages(): void |
| 80 | + { |
| 81 | + $adapter = new InMemoryAdapter(); |
| 82 | + $adapter->push(new Message('test', 'a')); |
| 83 | + $adapter->push(new Message('test', 'b')); |
| 84 | + $adapter->push(new Message('test', 'c')); |
| 85 | + |
| 86 | + $processed = []; |
| 87 | + $adapter->runExisting( |
| 88 | + static function (MessageInterface $message) use (&$processed): bool { |
| 89 | + $processed[] = $message->getData(); |
| 90 | + return true; |
| 91 | + }, |
| 92 | + ); |
| 93 | + |
| 94 | + $this->assertSame(['a', 'b', 'c'], $processed); |
| 95 | + } |
| 96 | + |
| 97 | + public function testRunExistingStopsWhenHandlerReturnsFalse(): void |
| 98 | + { |
| 99 | + $adapter = new InMemoryAdapter(); |
| 100 | + $adapter->push(new Message('test', 'a')); |
| 101 | + $adapter->push(new Message('test', 'b')); |
| 102 | + $adapter->push(new Message('test', 'c')); |
| 103 | + |
| 104 | + $processed = []; |
| 105 | + $adapter->runExisting( |
| 106 | + static function (MessageInterface $message) use (&$processed): bool { |
| 107 | + $processed[] = $message->getData(); |
| 108 | + return false; |
| 109 | + }, |
| 110 | + ); |
| 111 | + |
| 112 | + $this->assertSame(['a'], $processed); |
| 113 | + } |
| 114 | + |
| 115 | + public function testRunExistingOnEmptyQueue(): void |
| 116 | + { |
| 117 | + $adapter = new InMemoryAdapter(); |
| 118 | + |
| 119 | + $called = false; |
| 120 | + $adapter->runExisting(static function () use (&$called): bool { |
| 121 | + $called = true; |
| 122 | + return true; |
| 123 | + }); |
| 124 | + |
| 125 | + $this->assertFalse($called); |
| 126 | + } |
| 127 | + |
| 128 | + public function testRunExistingDoesNotReprocessMessages(): void |
| 129 | + { |
| 130 | + $adapter = new InMemoryAdapter(); |
| 131 | + $adapter->push(new Message('test', 'x')); |
| 132 | + |
| 133 | + $count = 0; |
| 134 | + $handler = static function () use (&$count): bool { |
| 135 | + $count++; |
| 136 | + return true; |
| 137 | + }; |
| 138 | + $adapter->runExisting($handler); |
| 139 | + $adapter->runExisting($handler); |
| 140 | + |
| 141 | + $this->assertSame(1, $count); |
| 142 | + } |
| 143 | + |
| 144 | + public function testIdContinuesAfterProcessing(): void |
| 145 | + { |
| 146 | + $adapter = new InMemoryAdapter(); |
| 147 | + $adapter->push(new Message('test', null)); |
| 148 | + $adapter->runExisting(static fn() => true); |
| 149 | + |
| 150 | + $envelope = $adapter->push(new Message('test', null)); |
| 151 | + |
| 152 | + $this->assertInstanceOf(IdEnvelope::class, $envelope); |
| 153 | + $this->assertSame(1, $envelope->getId()); |
| 154 | + $this->assertSame(MessageStatus::WAITING, $adapter->status($envelope->getId())); |
| 155 | + } |
| 156 | + |
| 157 | + public function testSubscribeProcessesExistingMessages(): void |
| 158 | + { |
| 159 | + $adapter = new InMemoryAdapter(); |
| 160 | + $adapter->push(new Message('test', 'a')); |
| 161 | + $adapter->push(new Message('test', 'b')); |
| 162 | + |
| 163 | + $processed = []; |
| 164 | + $adapter->subscribe( |
| 165 | + static function (MessageInterface $message) use (&$processed): bool { |
| 166 | + $processed[] = $message->getData(); |
| 167 | + return true; |
| 168 | + }, |
| 169 | + ); |
| 170 | + |
| 171 | + $this->assertSame(['a', 'b'], $processed); |
| 172 | + } |
| 173 | +} |
0 commit comments