diff --git a/src/Debug/QueueCollector.php b/src/Debug/QueueCollector.php index bb688b74..a47f4b00 100644 --- a/src/Debug/QueueCollector.php +++ b/src/Debug/QueueCollector.php @@ -67,7 +67,7 @@ public function collectWorkerProcessing(MessageInterface $message, QueueInterfac if (!$this->isActive()) { return; } - $this->processingMessages[$queue->getChannel() ?? 'null'][] = $message; + $this->processingMessages[$queue->getChannel()][] = $message; } private function reset(): void diff --git a/src/Debug/QueueDecorator.php b/src/Debug/QueueDecorator.php index 40ce77a0..8da8fb17 100644 --- a/src/Debug/QueueDecorator.php +++ b/src/Debug/QueueDecorator.php @@ -50,7 +50,7 @@ public function withAdapter(AdapterInterface $adapter): QueueInterface return new self($this->queue->withAdapter($adapter), $this->collector); } - public function getChannel(): ?string + public function getChannel(): string { return $this->queue->getChannel(); } diff --git a/src/Queue.php b/src/Queue.php index b0935ac3..abd731e4 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -37,9 +37,10 @@ public function __construct( $this->adapterPushHandler = new AdapterPushHandler(); } - public function getChannel(): ?string + public function getChannel(): string { - return $this->adapter?->getChannel(); + $this->checkAdapter(); + return $this->adapter->getChannel(); } public function push( @@ -83,7 +84,6 @@ public function run(int $max = 0): int return true; }; - /** @psalm-suppress PossiblyNullReference */ $this->adapter->runExisting($handlerCallback); $this->logger->info( @@ -99,7 +99,6 @@ public function listen(): void $this->checkAdapter(); $this->logger->info('Start listening to the queue.'); - /** @psalm-suppress PossiblyNullReference */ $this->adapter->subscribe(fn (MessageInterface $message) => $this->handle($message)); $this->logger->info('Finish listening to the queue.'); } @@ -107,8 +106,6 @@ public function listen(): void public function status(string|int $id): JobStatus { $this->checkAdapter(); - - /** @psalm-suppress PossiblyNullReference */ return $this->adapter->status($id); } @@ -143,6 +140,9 @@ private function handle(MessageInterface $message): bool return $this->loop->canContinue(); } + /** + * @psalm-assert AdapterInterface $this->adapter + */ private function checkAdapter(): void { if ($this->adapter === null) { diff --git a/src/QueueInterface.php b/src/QueueInterface.php index 5ec527ca..17237548 100644 --- a/src/QueueInterface.php +++ b/src/QueueInterface.php @@ -45,5 +45,5 @@ public function status(string|int $id): JobStatus; public function withAdapter(AdapterInterface $adapter): self; - public function getChannel(): ?string; + public function getChannel(): string; } diff --git a/stubs/StubQueue.php b/stubs/StubQueue.php index bd6cdfbb..69efc0d7 100644 --- a/stubs/StubQueue.php +++ b/stubs/StubQueue.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Stubs; +use LogicException; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\MessageInterface; @@ -53,8 +54,12 @@ public function withAdapter(AdapterInterface $adapter): QueueInterface return $new; } - public function getChannel(): ?string + public function getChannel(): string { + if ($this->adapter === null) { + throw new LogicException('Adapter is not set.'); + } + return $this->adapter?->getChannel(); } } diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index 25727285..5ff8f7b2 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -8,6 +8,7 @@ use Yiisoft\Queue\Exception\AdapterConfiguration\AdapterNotConfiguredException; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\Message; +use Yiisoft\Queue\Stubs\StubAdapter; use Yiisoft\Queue\Tests\App\FakeAdapter; use Yiisoft\Queue\Tests\TestCase; use Yiisoft\Queue\Message\IdEnvelope; @@ -147,4 +148,21 @@ public function testRunWithSignalLoop(): void self::assertEquals(2, $this->executionTimes); } + + public function testGetChannel(): void + { + $queue = $this + ->getQueue() + ->withAdapter(new StubAdapter('test-channel')); + + $this->assertSame('test-channel', $queue->getChannel()); + } + + public function testGetChannelWithoutAdapter(): void + { + $queue = $this->getQueue(); + + $this->expectException(AdapterNotConfiguredException::class); + $queue->getChannel(); + } } diff --git a/tests/Unit/Stubs/StubQueueTest.php b/tests/Unit/Stubs/StubQueueTest.php index 566fa571..7bb5ee32 100644 --- a/tests/Unit/Stubs/StubQueueTest.php +++ b/tests/Unit/Stubs/StubQueueTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Tests\Unit\Stubs; +use LogicException; use PHPUnit\Framework\TestCase; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\Message; @@ -20,11 +21,19 @@ public function testBase(): void $this->assertSame($message, $queue->push($message)); $this->assertSame(0, $queue->run()); $this->assertSame(JobStatus::DONE, $queue->status('test')); - $this->assertNull($queue->getChannel()); $this->assertNull($queue->getAdapter()); $queue->listen(); } + public function testGetChannelWithoutAdapter(): void + { + $queue = new StubQueue(); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Adapter is not set.'); + $queue->getChannel(); + } + public function testWithAdapter(): void { $sourceQueue = new StubQueue();