From 8669e2750ebd853cfb226dba9ec05ff3ed84cf6c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 27 Jan 2025 17:13:31 +0300 Subject: [PATCH 1/6] Change `QueueInterface::getChannel()` result type to string only --- src/Debug/QueueCollector.php | 2 +- src/Debug/QueueDecorator.php | 2 +- src/Queue.php | 9 +++++++-- src/QueueInterface.php | 2 +- stubs/StubQueue.php | 7 ++++++- tests/Unit/Stubs/StubQueueTest.php | 11 ++++++++++- 6 files changed, 26 insertions(+), 7 deletions(-) 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 af38420d..f6293552 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue; +use LogicException; use Psr\Log\LoggerInterface; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Cli\LoopInterface; @@ -37,9 +38,13 @@ public function __construct( $this->adapterPushHandler = new AdapterPushHandler(); } - public function getChannel(): ?string + public function getChannel(): string { - return $this->adapter?->getChannel(); + if ($this->adapter === null) { + throw new LogicException('Adapter is not set.'); + } + + return $this->adapter->getChannel(); } public function push( 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/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(); From 8ab91e2f0a39b4c58ea60f13cace19f6d28e06d7 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 27 Jan 2025 17:21:01 +0300 Subject: [PATCH 2/6] test --- tests/Unit/QueueTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index 25727285..a80657ab 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Tests\Unit; +use LogicException; use Yiisoft\Queue\Cli\SignalLoop; use Yiisoft\Queue\Exception\AdapterConfiguration\AdapterNotConfiguredException; use Yiisoft\Queue\JobStatus; @@ -147,4 +148,13 @@ public function testRunWithSignalLoop(): void self::assertEquals(2, $this->executionTimes); } + + public function testGetChannelWithoutAdapter(): void + { + $queue = $this->getQueue(); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Adapter is not set.'); + $queue->getChannel(); + } } From bf91ba3536b5da6ec4d7e8b274b2da72042f8d9f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 27 Jan 2025 17:26:41 +0300 Subject: [PATCH 3/6] test --- tests/Unit/QueueTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index a80657ab..8a4a9a87 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -9,6 +9,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; @@ -149,6 +150,15 @@ 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(); From b3c2d6deeb6583267be57c47e827c8efdec57214 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 14 Feb 2025 10:12:03 +0300 Subject: [PATCH 4/6] fix --- src/Queue.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Queue.php b/src/Queue.php index f6293552..0870d806 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -40,10 +40,7 @@ public function __construct( public function getChannel(): string { - if ($this->adapter === null) { - throw new LogicException('Adapter is not set.'); - } - + $this->checkAdapter(); return $this->adapter->getChannel(); } @@ -86,7 +83,6 @@ public function run(int $max = 0): int return true; }; - /** @psalm-suppress PossiblyNullReference */ $this->adapter->runExisting($handlerCallback); $this->logger->info( @@ -102,7 +98,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.'); } @@ -110,8 +105,6 @@ public function listen(): void public function status(string|int $id): JobStatus { $this->checkAdapter(); - - /** @psalm-suppress PossiblyNullReference */ return $this->adapter->status($id); } @@ -146,6 +139,9 @@ private function handle(MessageInterface $message): bool return $this->loop->canContinue(); } + /** + * @psalm-assert AdapterInterface $this->adapter + */ private function checkAdapter(): void { if ($this->adapter === null) { From fa86149ca7f78cd7a8caea25ab3559bd80fe146e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 14 Feb 2025 07:12:42 +0000 Subject: [PATCH 5/6] Apply fixes from StyleCI --- src/Queue.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Queue.php b/src/Queue.php index 0870d806..8b07e6e0 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -4,7 +4,6 @@ namespace Yiisoft\Queue; -use LogicException; use Psr\Log\LoggerInterface; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Cli\LoopInterface; From c0000c1871b89674386dd34d6ea8538b74b3d880 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 14 Feb 2025 10:13:43 +0300 Subject: [PATCH 6/6] fix --- src/Queue.php | 1 - tests/Unit/QueueTest.php | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Queue.php b/src/Queue.php index 0870d806..8b07e6e0 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -4,7 +4,6 @@ namespace Yiisoft\Queue; -use LogicException; use Psr\Log\LoggerInterface; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Cli\LoopInterface; diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index 8a4a9a87..5ff8f7b2 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -4,7 +4,6 @@ namespace Yiisoft\Queue\Tests\Unit; -use LogicException; use Yiisoft\Queue\Cli\SignalLoop; use Yiisoft\Queue\Exception\AdapterConfiguration\AdapterNotConfiguredException; use Yiisoft\Queue\JobStatus; @@ -163,8 +162,7 @@ public function testGetChannelWithoutAdapter(): void { $queue = $this->getQueue(); - $this->expectException(LogicException::class); - $this->expectExceptionMessage('Adapter is not set.'); + $this->expectException(AdapterNotConfiguredException::class); $queue->getChannel(); } }