Skip to content

Commit

Permalink
adapter constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 12, 2024
1 parent c485844 commit c10a09f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"psr/container": "^1.0|^2.0",
"psr/log": "^2.0|^3.0",
"symfony/console": "^5.4|^6.0",
"yiisoft/definitions": "^1.0|^2.0|^3.0",
"yiisoft/definitions": "dev-fix-union as 3.0.1",
"yiisoft/factory": "^1.3",
"yiisoft/friendly-exception": "^1.0",
"yiisoft/injector": "^1.0"
Expand Down
7 changes: 5 additions & 2 deletions src/Adapter/SynchronousAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use BackedEnum;
use InvalidArgumentException;
use Yiisoft\Queue\ChannelNormalizer;
use Yiisoft\Queue\Enum\JobStatus;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\QueueInterface;
Expand All @@ -16,12 +17,14 @@ final class SynchronousAdapter implements AdapterInterface
{
private array $messages = [];
private int $current = 0;
private string $channel;

public function __construct(
private WorkerInterface $worker,
private QueueInterface $queue,
private string $channel = QueueInterface::DEFAULT_CHANNEL_NAME,
string|BackedEnum $channel = QueueInterface::DEFAULT_CHANNEL_NAME,
) {
$this->channel = ChannelNormalizer::normalize($channel);
}

public function __destruct()
Expand Down Expand Up @@ -77,7 +80,7 @@ public function subscribe(callable $handlerCallback): void

public function withChannel(string|BackedEnum $channel): self
{
$channel = $channel instanceof BackedEnum ? (string) $channel->value : $channel;
$channel = ChannelNormalizer::normalize($channel);

if ($channel === $this->channel) {
return $this;
Expand Down
18 changes: 18 additions & 0 deletions src/ChannelNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue;

use BackedEnum;

/**
* @internal
*/
final class ChannelNormalizer
{
public static function normalize(string|BackedEnum $channel): string
{
return $channel instanceof BackedEnum ? (string) $channel->value : $channel;
}
}
11 changes: 8 additions & 3 deletions stubs/StubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use BackedEnum;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\ChannelNormalizer;
use Yiisoft\Queue\Enum\JobStatus;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\QueueInterface;
Expand All @@ -15,8 +16,12 @@
*/
final class StubAdapter implements AdapterInterface
{
public function __construct(private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME)
{
private string $channelName;

public function __construct(
string|BackedEnum $channelName = QueueInterface::DEFAULT_CHANNEL_NAME
) {
$this->channelName = ChannelNormalizer::normalize($channelName);
}

public function runExisting(callable $handlerCallback): void
Expand All @@ -40,7 +45,7 @@ public function subscribe(callable $handlerCallback): void
public function withChannel(string|BackedEnum $channel): AdapterInterface
{
$new = clone $this;
$new->channelName = $channel instanceof BackedEnum ? (string) $channel->value : $channel;
$new->channelName = ChannelNormalizer::normalize($channel);
return $new;
}

Expand Down
12 changes: 10 additions & 2 deletions tests/Unit/Adapter/SynchronousAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,26 @@ public function testStatusNotMessage(): void
$adapter->status('1');
}

public static function dataWithChannel(): iterable
public static function dataChannels(): iterable
{
yield 'string' => ['test', 'test'];
yield 'string-enum' => ['red', StringEnum::RED];
yield 'integer-enum' => ['1', IntEnum::ONE];
}

#[DataProvider('dataWithChannel')]
#[DataProvider('dataChannels')]
public function testWithChannel(string $expected, mixed $channel): void
{
$adapter = (new SynchronousAdapter(new StubWorker(), new StubQueue()))->withChannel($channel);

$this->assertSame($expected, $adapter->getChannelName());
}

#[DataProvider('dataChannels')]
public function testChannelInConstructor(string $expected, mixed $channel): void
{
$adapter = new SynchronousAdapter(new StubWorker(), new StubQueue(), $channel);

$this->assertSame($expected, $adapter->getChannelName());
}
}
12 changes: 10 additions & 2 deletions tests/Unit/Stubs/StubAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,26 @@ public function testBase(): void
$adapter->subscribe(static fn() => null);
}

public static function dataWithChannel(): iterable
public static function dataChannels(): iterable
{
yield 'string' => ['test', 'test'];
yield 'string-enum' => ['red', StringEnum::RED];
yield 'integer-enum' => ['1', IntEnum::ONE];
}

#[DataProvider('dataWithChannel')]
#[DataProvider('dataChannels')]
public function testWithChannel(string $expected, mixed $channel): void
{
$adapter = (new StubAdapter())->withChannel($channel);

$this->assertSame($expected, $adapter->getChannelName());
}

#[DataProvider('dataChannels')]
public function testChannelInConstructor(string $expected, mixed $channel): void
{
$adapter = new StubAdapter($channel);

$this->assertSame($expected, $adapter->getChannelName());
}
}

0 comments on commit c10a09f

Please sign in to comment.