Skip to content

Commit

Permalink
Allow to use backed enumerations as channel name
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 12, 2024
1 parent 2c202fc commit c485844
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Adapter;

use BackedEnum;
use InvalidArgumentException;
use Yiisoft\Queue\Enum\JobStatus;
use Yiisoft\Queue\Message\MessageInterface;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function push(MessageInterface $message): MessageInterface;
*/
public function subscribe(callable $handlerCallback): void;

public function withChannel(string $channel): self;
public function withChannel(string|BackedEnum $channel): self;

public function getChannelName(): string;
}
5 changes: 4 additions & 1 deletion src/Adapter/SynchronousAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Adapter;

use BackedEnum;
use InvalidArgumentException;
use Yiisoft\Queue\Enum\JobStatus;
use Yiisoft\Queue\Message\MessageInterface;
Expand Down Expand Up @@ -74,8 +75,10 @@ public function subscribe(callable $handlerCallback): void
$this->runExisting($handlerCallback);
}

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

if ($channel === $this->channel) {
return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions stubs/StubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Stubs;

use BackedEnum;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Enum\JobStatus;
use Yiisoft\Queue\Message\MessageInterface;
Expand Down Expand Up @@ -36,11 +37,10 @@ public function subscribe(callable $handlerCallback): void
{
}

public function withChannel(string $channel): AdapterInterface
public function withChannel(string|BackedEnum $channel): AdapterInterface
{
$new = clone $this;
$new->channelName = $channel;

$new->channelName = $channel instanceof BackedEnum ? (string) $channel->value : $channel;
return $new;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/App/FakeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Tests\App;

use BackedEnum;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Enum\JobStatus;
use Yiisoft\Queue\Message\MessageInterface;
Expand Down Expand Up @@ -35,12 +36,11 @@ public function subscribe(callable $handlerCallback): void
//skip
}

public function withChannel(string $channel): AdapterInterface
public function withChannel(string|BackedEnum $channel): AdapterInterface
{
$instance = clone $this;
$instance->pushMessages = [];
$instance->channel = $channel;

$instance->channel = $channel instanceof BackedEnum ? (string) $channel->value : $channel;
return $instance;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Benchmark/Support/VoidAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Tests\Benchmark\Support;

use BackedEnum;
use InvalidArgumentException;
use RuntimeException;
use Yiisoft\Queue\Adapter\AdapterInterface;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function subscribe(callable $handlerCallback): void
throw new RuntimeException('Method is not implemented');
}

public function withChannel(string $channel): AdapterInterface
public function withChannel(string|BackedEnum $channel): AdapterInterface
{
throw new RuntimeException('Method is not implemented');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\Unit;
namespace Yiisoft\Queue\Tests\Unit\Adapter;

use PHPUnit\Framework\Attributes\DataProvider;
use Yiisoft\Queue\Adapter\SynchronousAdapter;
use Yiisoft\Queue\Enum\JobStatus;
use Yiisoft\Queue\Message\IdEnvelope;
use Yiisoft\Queue\Message\Message;
use Yiisoft\Queue\QueueInterface;
use Yiisoft\Queue\Stubs\StubQueue;
use Yiisoft\Queue\Stubs\StubWorker;
use Yiisoft\Queue\Tests\TestCase;
use Yiisoft\Queue\Message\IdEnvelope;
use Yiisoft\Queue\Tests\Unit\Support\IntEnum;
use Yiisoft\Queue\Tests\Unit\Support\StringEnum;

final class SynchronousAdapterTest extends TestCase
{
Expand Down Expand Up @@ -92,4 +98,19 @@ public function testStatusNotMessage(): void
$this->expectExceptionMessage('There is no message with the given ID.');
$adapter->status('1');
}

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

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

$this->assertSame($expected, $adapter->getChannelName());
}
}
18 changes: 18 additions & 0 deletions tests/Unit/Stubs/StubAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Yiisoft\Queue\Tests\Unit\Stubs;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Message\Message;
use Yiisoft\Queue\Stubs\StubAdapter;
use Yiisoft\Queue\Tests\Unit\Support\IntEnum;
use Yiisoft\Queue\Tests\Unit\Support\StringEnum;

final class StubAdapterTest extends TestCase
{
Expand All @@ -21,4 +24,19 @@ public function testBase(): void
$adapter->runExisting(static fn() => null);
$adapter->subscribe(static fn() => null);
}

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

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

$this->assertSame($expected, $adapter->getChannelName());
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Support/IntEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\Unit\Support;

enum IntEnum: int
{
case ONE = 1;
case TWO = 2;
}
11 changes: 11 additions & 0 deletions tests/Unit/Support/StringEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\Unit\Support;

enum StringEnum: string
{
case RED = 'red';
case GREEN = 'green';
}

0 comments on commit c485844

Please sign in to comment.