Skip to content

Commit 05ab1c0

Browse files
authored
Change QueueInterface::getChannel() result type to string only (#239)
1 parent a1b7171 commit 05ab1c0

File tree

7 files changed

+43
-11
lines changed

7 files changed

+43
-11
lines changed

src/Debug/QueueCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function collectWorkerProcessing(MessageInterface $message, QueueInterfac
6767
if (!$this->isActive()) {
6868
return;
6969
}
70-
$this->processingMessages[$queue->getChannel() ?? 'null'][] = $message;
70+
$this->processingMessages[$queue->getChannel()][] = $message;
7171
}
7272

7373
private function reset(): void

src/Debug/QueueDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function withAdapter(AdapterInterface $adapter): QueueInterface
5050
return new self($this->queue->withAdapter($adapter), $this->collector);
5151
}
5252

53-
public function getChannel(): ?string
53+
public function getChannel(): string
5454
{
5555
return $this->queue->getChannel();
5656
}

src/Queue.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ public function __construct(
3737
$this->adapterPushHandler = new AdapterPushHandler();
3838
}
3939

40-
public function getChannel(): ?string
40+
public function getChannel(): string
4141
{
42-
return $this->adapter?->getChannel();
42+
$this->checkAdapter();
43+
return $this->adapter->getChannel();
4344
}
4445

4546
public function push(
@@ -83,7 +84,6 @@ public function run(int $max = 0): int
8384
return true;
8485
};
8586

86-
/** @psalm-suppress PossiblyNullReference */
8787
$this->adapter->runExisting($handlerCallback);
8888

8989
$this->logger->info(
@@ -99,16 +99,13 @@ public function listen(): void
9999
$this->checkAdapter();
100100

101101
$this->logger->info('Start listening to the queue.');
102-
/** @psalm-suppress PossiblyNullReference */
103102
$this->adapter->subscribe(fn (MessageInterface $message) => $this->handle($message));
104103
$this->logger->info('Finish listening to the queue.');
105104
}
106105

107106
public function status(string|int $id): JobStatus
108107
{
109108
$this->checkAdapter();
110-
111-
/** @psalm-suppress PossiblyNullReference */
112109
return $this->adapter->status($id);
113110
}
114111

@@ -143,6 +140,9 @@ private function handle(MessageInterface $message): bool
143140
return $this->loop->canContinue();
144141
}
145142

143+
/**
144+
* @psalm-assert AdapterInterface $this->adapter
145+
*/
146146
private function checkAdapter(): void
147147
{
148148
if ($this->adapter === null) {

src/QueueInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ public function status(string|int $id): JobStatus;
4545

4646
public function withAdapter(AdapterInterface $adapter): self;
4747

48-
public function getChannel(): ?string;
48+
public function getChannel(): string;
4949
}

stubs/StubQueue.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Queue\Stubs;
66

7+
use LogicException;
78
use Yiisoft\Queue\Adapter\AdapterInterface;
89
use Yiisoft\Queue\JobStatus;
910
use Yiisoft\Queue\Message\MessageInterface;
@@ -53,8 +54,12 @@ public function withAdapter(AdapterInterface $adapter): QueueInterface
5354
return $new;
5455
}
5556

56-
public function getChannel(): ?string
57+
public function getChannel(): string
5758
{
59+
if ($this->adapter === null) {
60+
throw new LogicException('Adapter is not set.');
61+
}
62+
5863
return $this->adapter?->getChannel();
5964
}
6065
}

tests/Unit/QueueTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Yiisoft\Queue\Exception\AdapterConfiguration\AdapterNotConfiguredException;
99
use Yiisoft\Queue\JobStatus;
1010
use Yiisoft\Queue\Message\Message;
11+
use Yiisoft\Queue\Stubs\StubAdapter;
1112
use Yiisoft\Queue\Tests\App\FakeAdapter;
1213
use Yiisoft\Queue\Tests\TestCase;
1314
use Yiisoft\Queue\Message\IdEnvelope;
@@ -147,4 +148,21 @@ public function testRunWithSignalLoop(): void
147148

148149
self::assertEquals(2, $this->executionTimes);
149150
}
151+
152+
public function testGetChannel(): void
153+
{
154+
$queue = $this
155+
->getQueue()
156+
->withAdapter(new StubAdapter('test-channel'));
157+
158+
$this->assertSame('test-channel', $queue->getChannel());
159+
}
160+
161+
public function testGetChannelWithoutAdapter(): void
162+
{
163+
$queue = $this->getQueue();
164+
165+
$this->expectException(AdapterNotConfiguredException::class);
166+
$queue->getChannel();
167+
}
150168
}

tests/Unit/Stubs/StubQueueTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Queue\Tests\Unit\Stubs;
66

7+
use LogicException;
78
use PHPUnit\Framework\TestCase;
89
use Yiisoft\Queue\JobStatus;
910
use Yiisoft\Queue\Message\Message;
@@ -20,11 +21,19 @@ public function testBase(): void
2021
$this->assertSame($message, $queue->push($message));
2122
$this->assertSame(0, $queue->run());
2223
$this->assertSame(JobStatus::DONE, $queue->status('test'));
23-
$this->assertNull($queue->getChannel());
2424
$this->assertNull($queue->getAdapter());
2525
$queue->listen();
2626
}
2727

28+
public function testGetChannelWithoutAdapter(): void
29+
{
30+
$queue = new StubQueue();
31+
32+
$this->expectException(LogicException::class);
33+
$this->expectExceptionMessage('Adapter is not set.');
34+
$queue->getChannel();
35+
}
36+
2837
public function testWithAdapter(): void
2938
{
3039
$sourceQueue = new StubQueue();

0 commit comments

Comments
 (0)