Skip to content

Commit

Permalink
Change QueueInterface::getChannel() result type to string only (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Feb 14, 2025
1 parent a1b7171 commit 05ab1c0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Debug/QueueCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/QueueDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
12 changes: 6 additions & 6 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -83,7 +84,6 @@ public function run(int $max = 0): int
return true;
};

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

$this->logger->info(
Expand All @@ -99,16 +99,13 @@ 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.');
}

public function status(string|int $id): JobStatus
{
$this->checkAdapter();

/** @psalm-suppress PossiblyNullReference */
return $this->adapter->status($id);
}

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/QueueInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 6 additions & 1 deletion stubs/StubQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Stubs;

use LogicException;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\JobStatus;
use Yiisoft\Queue\Message\MessageInterface;
Expand Down Expand Up @@ -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();
}
}
18 changes: 18 additions & 0 deletions tests/Unit/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
11 changes: 10 additions & 1 deletion tests/Unit/Stubs/StubQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit 05ab1c0

Please sign in to comment.