diff --git a/README.md b/README.md index 82d8136b..38a4814b 100644 --- a/README.md +++ b/README.md @@ -185,10 +185,9 @@ use Yiisoft\Queue\Adapter\SynchronousAdapter; [ 'channel1' => new SynchronousAdapter(), - 'channel2' => static fn(SynchronousAdapter $adapter) => $adapter->withChannel('channel2'), - 'channel3' => [ + 'channel2' => new SynchronousAdapter(), // a second instance for a different queue processing pipeline + 'channel3' => [ // use a yiisoft/factory syntax for adapter creation 'class' => SynchronousAdapter::class, - '__constructor' => ['channel' => 'channel3'], ], ] ``` diff --git a/config/di.php b/config/di.php index a2fb3c01..273be5d1 100644 --- a/config/di.php +++ b/config/di.php @@ -31,7 +31,7 @@ return [ AdapterFactoryQueueProvider::class => [ '__construct()' => [ - 'definitions' => $params['yiisoft/queue']['channels'], + 'definitions' => $params['yiisoft/queue']['queues'], ], ], QueueProviderInterface::class => AdapterFactoryQueueProvider::class, @@ -61,12 +61,12 @@ MessageSerializerInterface::class => JsonMessageSerializer::class, RunCommand::class => [ '__construct()' => [ - 'channels' => array_keys($params['yiisoft/queue']['channels']), + 'queues' => array_keys($params['yiisoft/queue']['queues']), ], ], ListenAllCommand::class => [ '__construct()' => [ - 'channels' => array_keys($params['yiisoft/queue']['channels']), + 'queues' => array_keys($params['yiisoft/queue']['queues']), ], ], ]; diff --git a/config/params.php b/config/params.php index 2195a8cb..90b8492f 100644 --- a/config/params.php +++ b/config/params.php @@ -22,8 +22,8 @@ ], 'yiisoft/queue' => [ 'handlers' => [], - 'channels' => [ - QueueProviderInterface::DEFAULT_CHANNEL => AdapterInterface::class, + 'queues' => [ + QueueProviderInterface::DEFAULT_QUEUE => AdapterInterface::class, ], 'middlewares-push' => [], 'middlewares-consume' => [], diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index 38f707d0..6c8e7fdc 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -4,7 +4,6 @@ namespace Yiisoft\Queue\Adapter; -use BackedEnum; use InvalidArgumentException; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\MessageInterface; @@ -38,8 +37,4 @@ public function push(MessageInterface $message): MessageInterface; * @param callable(MessageInterface): bool $handlerCallback The handler which will handle messages. Returns false if it cannot continue handling messages. */ public function subscribe(callable $handlerCallback): void; - - public function withChannel(string|BackedEnum $channel): self; - - public function getChannel(): string; } diff --git a/src/Adapter/SynchronousAdapter.php b/src/Adapter/SynchronousAdapter.php index 5c8f7743..189ec2b2 100644 --- a/src/Adapter/SynchronousAdapter.php +++ b/src/Adapter/SynchronousAdapter.php @@ -4,12 +4,9 @@ namespace Yiisoft\Queue\Adapter; -use BackedEnum; use InvalidArgumentException; -use Yiisoft\Queue\ChannelNormalizer; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\MessageInterface; -use Yiisoft\Queue\Provider\QueueProviderInterface; use Yiisoft\Queue\QueueInterface; use Yiisoft\Queue\Worker\WorkerInterface; use Yiisoft\Queue\Message\IdEnvelope; @@ -20,15 +17,11 @@ final class SynchronousAdapter implements AdapterInterface { private array $messages = []; private int $current = 0; - private string $channel; public function __construct( private readonly WorkerInterface $worker, private readonly QueueInterface $queue, - string|BackedEnum $channel = QueueProviderInterface::DEFAULT_CHANNEL, - ) { - $this->channel = ChannelNormalizer::normalize($channel); - } + ) {} public function __destruct() { @@ -80,24 +73,4 @@ public function subscribe(callable $handlerCallback): void { $this->runExisting($handlerCallback); } - - public function withChannel(string|BackedEnum $channel): self - { - $channel = ChannelNormalizer::normalize($channel); - - if ($channel === $this->channel) { - return $this; - } - - $new = clone $this; - $new->channel = $channel; - $new->messages = []; - - return $new; - } - - public function getChannel(): string - { - return $this->channel; - } } diff --git a/src/ChannelNormalizer.php b/src/ChannelNormalizer.php deleted file mode 100644 index 1b178188..00000000 --- a/src/ChannelNormalizer.php +++ /dev/null @@ -1,18 +0,0 @@ -value : $channel; - } -} diff --git a/src/Command/ListenAllCommand.php b/src/Command/ListenAllCommand.php index 8d61a4b6..66f88f7c 100644 --- a/src/Command/ListenAllCommand.php +++ b/src/Command/ListenAllCommand.php @@ -25,7 +25,7 @@ final class ListenAllCommand extends Command public function __construct( private readonly QueueProviderInterface $queueProvider, private readonly LoopInterface $loop, - private readonly array $channels, + private readonly array $queues, ) { parent::__construct(); } @@ -36,36 +36,36 @@ public function __construct( public function configure(): void { $this->addArgument( - 'channel', + 'queue', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, - 'Queue channel name list to connect to', - $this->channels, + 'Queue name list to connect to', + $this->queues, ) ->addOption( 'pause', 'p', InputOption::VALUE_REQUIRED, - 'Pause between queue channel iterations in seconds. May save some CPU. Default: 1', + 'Pause between queue iterations in seconds. May save some CPU. Default: 1', 1, ) ->addOption( 'maximum', 'm', InputOption::VALUE_REQUIRED, - 'Maximum number of messages to process in each channel before switching to another channel. ' + 'Maximum number of messages to process in each queue before switching to another queue. ' . 'Default is 0 (no limits).', 0, ); - $this->addUsage('[channel1 [channel2 [...]]] [--timeout=] [--maximum=]'); + $this->addUsage('[queue1 [queue2 [...]]] [--timeout=] [--maximum=]'); } protected function execute(InputInterface $input, OutputInterface $output): int { $queues = []; - /** @var string $channel */ - foreach ($input->getArgument('channel') as $channel) { - $queues[] = $this->queueProvider->get($channel); + /** @var string $queue */ + foreach ($input->getArgument('queue') as $queue) { + $queues[] = $this->queueProvider->get($queue); } $pauseSeconds = (int) $input->getOption('pause'); diff --git a/src/Command/ListenCommand.php b/src/Command/ListenCommand.php index 7b2859dc..24b778ab 100644 --- a/src/Command/ListenCommand.php +++ b/src/Command/ListenCommand.php @@ -26,17 +26,17 @@ public function __construct( public function configure(): void { $this->addArgument( - 'channel', + 'queue', InputArgument::OPTIONAL, - 'Queue channel name to connect to', - QueueProviderInterface::DEFAULT_CHANNEL, + 'Queue name to connect to', + QueueProviderInterface::DEFAULT_QUEUE, ); } protected function execute(InputInterface $input, OutputInterface $output): int { $this->queueProvider - ->get($input->getArgument('channel')) + ->get($input->getArgument('queue')) ->listen(); return 0; diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 377a5dc0..7f1aeab7 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -20,7 +20,7 @@ final class RunCommand extends Command { public function __construct( private readonly QueueProviderInterface $queueProvider, - private readonly array $channels, + private readonly array $queues, ) { parent::__construct(); } @@ -28,28 +28,28 @@ public function __construct( public function configure(): void { $this->addArgument( - 'channel', + 'queue', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, - 'Queue channel name list to connect to.', - $this->channels, + 'Queue name list to connect to.', + $this->queues, ) ->addOption( 'maximum', 'm', InputOption::VALUE_REQUIRED, - 'Maximum number of messages to process in each channel. Default is 0 (no limits).', + 'Maximum number of messages to process in each queue. Default is 0 (no limits).', 0, ) - ->addUsage('[channel1 [channel2 [...]]] --maximum 100'); + ->addUsage('[queue1 [queue2 [...]]] --maximum 100'); } protected function execute(InputInterface $input, OutputInterface $output): int { - /** @var string $channel */ - foreach ($input->getArgument('channel') as $channel) { - $output->write("Processing channel $channel... "); + /** @var string $queue */ + foreach ($input->getArgument('queue') as $queue) { + $output->write("Processing queue $queue... "); $count = $this->queueProvider - ->get($channel) + ->get($queue) ->run((int) $input->getOption('maximum')); $output->writeln("Messages processed: $count."); diff --git a/src/Debug/QueueCollector.php b/src/Debug/QueueCollector.php index d0950178..0f48c230 100644 --- a/src/Debug/QueueCollector.php +++ b/src/Debug/QueueCollector.php @@ -47,18 +47,18 @@ public function collectStatus(string $id, JobStatus $status): void } public function collectPush( - ?string $channel, + ?string $queueName, MessageInterface $message, string|array|callable|MiddlewarePushInterface ...$middlewareDefinitions, ): void { if (!$this->isActive()) { return; } - if ($channel === null) { - $channel = 'null'; + if ($queueName === null) { + $queueName = 'null'; } - $this->pushes[$channel][] = [ + $this->pushes[$queueName][] = [ 'message' => $message, 'middlewares' => $middlewareDefinitions, ]; @@ -69,7 +69,7 @@ public function collectWorkerProcessing(MessageInterface $message, QueueInterfac if (!$this->isActive()) { return; } - $this->processingMessages[$queue->getChannel()][] = $message; + $this->processingMessages[$queue->getName()][] = $message; } public function getSummary(): array diff --git a/src/Debug/QueueDecorator.php b/src/Debug/QueueDecorator.php index 5e177250..f0bc7353 100644 --- a/src/Debug/QueueDecorator.php +++ b/src/Debug/QueueDecorator.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Debug; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\MessageInterface; @@ -30,7 +31,7 @@ public function push( string|array|callable|MiddlewarePushInterface ...$middlewareDefinitions, ): MessageInterface { $message = $this->queue->push($message, ...$middlewareDefinitions); - $this->collector->collectPush($this->queue->getChannel(), $message, ...$middlewareDefinitions); + $this->collector->collectPush($this->queue->getName(), $message, ...$middlewareDefinitions); return $message; } @@ -44,13 +45,13 @@ public function listen(): void $this->queue->listen(); } - public function withAdapter(AdapterInterface $adapter): static + public function withAdapter(AdapterInterface $adapter, string|BackedEnum|null $queueName = null): static { - return new self($this->queue->withAdapter($adapter), $this->collector); + return new self($this->queue->withAdapter($adapter, $queueName), $this->collector); } - public function getChannel(): string + public function getName(): string { - return $this->queue->getChannel(); + return $this->queue->getName(); } } diff --git a/src/Debug/QueueProviderInterfaceProxy.php b/src/Debug/QueueProviderInterfaceProxy.php index a1ec5e6e..62ff0976 100644 --- a/src/Debug/QueueProviderInterfaceProxy.php +++ b/src/Debug/QueueProviderInterfaceProxy.php @@ -15,14 +15,15 @@ public function __construct( private readonly QueueCollector $collector, ) {} - public function get(string|BackedEnum $channel): QueueInterface + public function get(string|BackedEnum $queueName): QueueInterface { - $queue = $this->queueProvider->get($channel); + $queue = $this->queueProvider->get($queueName); + return new QueueDecorator($queue, $this->collector); } - public function has(string|BackedEnum $channel): bool + public function has(string|BackedEnum $queueName): bool { - return $this->queueProvider->has($channel); + return $this->queueProvider->has($queueName); } } diff --git a/src/Middleware/FailureHandling/FailureMiddlewareDispatcher.php b/src/Middleware/FailureHandling/FailureMiddlewareDispatcher.php index c5c23b68..226aaca5 100644 --- a/src/Middleware/FailureHandling/FailureMiddlewareDispatcher.php +++ b/src/Middleware/FailureHandling/FailureMiddlewareDispatcher.php @@ -37,18 +37,17 @@ public function dispatch( FailureHandlingRequest $request, MessageFailureHandlerInterface $finishHandler, ): FailureHandlingRequest { - /** @var string $channel It is always string in this context */ - $channel = $request->getQueue()->getChannel(); - if (!isset($this->middlewareDefinitions[$channel]) || $this->middlewareDefinitions[$channel] === []) { - $channel = self::DEFAULT_PIPELINE; + $queueName = $request->getQueue()->getName(); + if (!isset($this->middlewareDefinitions[$queueName]) || $this->middlewareDefinitions[$queueName] === []) { + $queueName = self::DEFAULT_PIPELINE; } - $definitions = array_reverse($this->middlewareDefinitions[$channel]); + $definitions = array_reverse($this->middlewareDefinitions[$queueName]); - if (!isset($this->stack[$channel])) { - $this->stack[$channel] = new MiddlewareFailureStack($this->buildMiddlewares(...$definitions), $finishHandler); + if (!isset($this->stack[$queueName])) { + $this->stack[$queueName] = new MiddlewareFailureStack($this->buildMiddlewares(...$definitions), $finishHandler); } - return $this->stack[$channel]->handleFailure($request); + return $this->stack[$queueName]->handleFailure($request); } /** diff --git a/src/Provider/AdapterFactoryQueueProvider.php b/src/Provider/AdapterFactoryQueueProvider.php index 1e52875d..91abe60b 100644 --- a/src/Provider/AdapterFactoryQueueProvider.php +++ b/src/Provider/AdapterFactoryQueueProvider.php @@ -9,7 +9,7 @@ use Yiisoft\Definitions\Exception\InvalidConfigException; use Yiisoft\Factory\StrictFactory; use Yiisoft\Queue\Adapter\AdapterInterface; -use Yiisoft\Queue\ChannelNormalizer; +use Yiisoft\Queue\QueueNameNormalizer; use Yiisoft\Queue\QueueInterface; use function array_key_exists; @@ -32,7 +32,7 @@ final class AdapterFactoryQueueProvider implements QueueProviderInterface /** * @param QueueInterface $baseQueue Base queue for queues creation. - * @param array $definitions Adapter definitions indexed by channel names. + * @param array $definitions Adapter definitions indexed by queueName names. * @param ContainerInterface|null $container Container to use for dependencies resolving. * @param bool $validate If definitions should be validated when set. * @@ -52,50 +52,50 @@ public function __construct( } } - public function get(string|BackedEnum $channel): QueueInterface + public function get(string|BackedEnum $queueName): QueueInterface { - $channel = ChannelNormalizer::normalize($channel); + $queueName = QueueNameNormalizer::normalize($queueName); - $queue = $this->getOrTryToCreate($channel); + $queue = $this->getOrTryToCreate($queueName); if ($queue === null) { - throw new ChannelNotFoundException($channel); + throw new QueueNotFoundException($queueName); } return $queue; } - public function has(string|BackedEnum $channel): bool + public function has(string|BackedEnum $queueName): bool { - $channel = ChannelNormalizer::normalize($channel); - return $this->factory->has($channel); + $queueName = QueueNameNormalizer::normalize($queueName); + return $this->factory->has($queueName); } /** * @throws InvalidQueueConfigException */ - private function getOrTryToCreate(string $channel): ?QueueInterface + private function getOrTryToCreate(string $queueName): ?QueueInterface { - if (array_key_exists($channel, $this->queues)) { - return $this->queues[$channel]; + if (array_key_exists($queueName, $this->queues)) { + return $this->queues[$queueName]; } - if ($this->factory->has($channel)) { - $adapter = $this->factory->create($channel); + if ($this->factory->has($queueName)) { + $adapter = $this->factory->create($queueName); if (!$adapter instanceof AdapterInterface) { throw new InvalidQueueConfigException( sprintf( - 'Adapter must implement "%s". For channel "%s" got "%s" instead.', + 'Adapter must implement "%s". For queueName "%s" got "%s" instead.', AdapterInterface::class, - $channel, + $queueName, get_debug_type($adapter), ), ); } - $this->queues[$channel] = $this->baseQueue->withAdapter($adapter->withChannel($channel)); + $this->queues[$queueName] = $this->baseQueue->withAdapter($adapter, $queueName); } else { - $this->queues[$channel] = null; + $this->queues[$queueName] = null; } - return $this->queues[$channel]; + return $this->queues[$queueName]; } } diff --git a/src/Provider/ChannelNotFoundException.php b/src/Provider/ChannelNotFoundException.php deleted file mode 100644 index ccd43bd3..00000000 --- a/src/Provider/ChannelNotFoundException.php +++ /dev/null @@ -1,27 +0,0 @@ -providers = $providers; } - public function get(string|BackedEnum $channel): QueueInterface + public function get(string|BackedEnum $queueName): QueueInterface { foreach ($this->providers as $provider) { - if ($provider->has($channel)) { - return $provider->get($channel); + if ($provider->has($queueName)) { + return $provider->get($queueName); } } - throw new ChannelNotFoundException($channel); + throw new QueueNotFoundException($queueName); } - public function has(string|BackedEnum $channel): bool + public function has(string|BackedEnum $queueName): bool { foreach ($this->providers as $provider) { - if ($provider->has($channel)) { + if ($provider->has($queueName)) { return true; } } diff --git a/src/Provider/PrototypeQueueProvider.php b/src/Provider/PrototypeQueueProvider.php deleted file mode 100644 index 84cdcf7d..00000000 --- a/src/Provider/PrototypeQueueProvider.php +++ /dev/null @@ -1,34 +0,0 @@ -baseQueue->withAdapter($this->baseAdapter->withChannel($channel)); - } - - public function has(string|BackedEnum $channel): bool - { - return true; - } -} diff --git a/src/Provider/QueueNotFoundException.php b/src/Provider/QueueNotFoundException.php new file mode 100644 index 00000000..bc302d40 --- /dev/null +++ b/src/Provider/QueueNotFoundException.php @@ -0,0 +1,27 @@ +adapterPushHandler = new AdapterPushHandler(); } - public function getChannel(): string + public function getName(): string { - $this->checkAdapter(); - return $this->adapter->getChannel(); + return $this->name; } public function push( @@ -109,10 +111,13 @@ public function status(string|int $id): JobStatus return $this->adapter->status($id); } - public function withAdapter(AdapterInterface $adapter): static + public function withAdapter(AdapterInterface $adapter, string|BackedEnum|null $queueName = null): static { $new = clone $this; $new->adapter = $adapter; + if ($queueName !== null) { + $new->name = QueueNameNormalizer::normalize($queueName); + } return $new; } diff --git a/src/QueueInterface.php b/src/QueueInterface.php index 1985c32e..3722e00f 100644 --- a/src/QueueInterface.php +++ b/src/QueueInterface.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue; +use BackedEnum; use InvalidArgumentException; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Message\MessageInterface; @@ -40,7 +41,16 @@ public function listen(): void; */ public function status(string|int $id): JobStatus; - public function withAdapter(AdapterInterface $adapter): static; + /** + * @param AdapterInterface $adapter Adapter to use. + * @param string|null $queueName Queue name to use. + * + * @return static A new queue with the given adapter and queue name. + */ + public function withAdapter(AdapterInterface $adapter, string|BackedEnum|null $queueName = null): static; - public function getChannel(): string; + /** + * Returns the logical name of the queue. + */ + public function getName(): string; } diff --git a/src/QueueNameNormalizer.php b/src/QueueNameNormalizer.php new file mode 100644 index 00000000..c5406bd0 --- /dev/null +++ b/src/QueueNameNormalizer.php @@ -0,0 +1,18 @@ +value : $queueName; + } +} diff --git a/stubs/StubAdapter.php b/stubs/StubAdapter.php index e7332e2d..1ead6703 100644 --- a/stubs/StubAdapter.php +++ b/stubs/StubAdapter.php @@ -6,7 +6,7 @@ use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; -use Yiisoft\Queue\ChannelNormalizer; +use Yiisoft\Queue\QueueNameNormalizer; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\MessageInterface; use Yiisoft\Queue\Provider\QueueProviderInterface; @@ -19,9 +19,9 @@ final class StubAdapter implements AdapterInterface private string $channel; public function __construct( - string|BackedEnum $channel = QueueProviderInterface::DEFAULT_CHANNEL + string|BackedEnum $channel = QueueProviderInterface::DEFAULT_QUEUE ) { - $this->channel = ChannelNormalizer::normalize($channel); + $this->channel = QueueNameNormalizer::normalize($channel); } public function runExisting(callable $handlerCallback): void @@ -42,10 +42,10 @@ public function subscribe(callable $handlerCallback): void { } - public function withChannel(string|BackedEnum $channel): AdapterInterface + public function withChannel(string|BackedEnum $channel): self { $new = clone $this; - $new->channel = ChannelNormalizer::normalize($channel); + $new->channel = QueueNameNormalizer::normalize($channel); return $new; } diff --git a/stubs/StubQueue.php b/stubs/StubQueue.php index 4d2ed10a..c7a4fea2 100644 --- a/stubs/StubQueue.php +++ b/stubs/StubQueue.php @@ -4,20 +4,28 @@ namespace Yiisoft\Queue\Stubs; -use LogicException; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\MessageInterface; use Yiisoft\Queue\Middleware\Push\MiddlewarePushInterface; use Yiisoft\Queue\QueueInterface; +use Yiisoft\Queue\QueueNameNormalizer; /** * Stub queue that does nothing. Job status is always "done". + * + * @template T of AdapterInterface */ final class StubQueue implements QueueInterface { - public function __construct(private ?AdapterInterface $adapter = null) - { + /** + * @param T|null $adapter + */ + public function __construct( + private ?AdapterInterface $adapter = null, + private string $name = 'default' + ) { } public function push( @@ -41,25 +49,32 @@ public function status(int|string $id): JobStatus return JobStatus::DONE; } + /** + * @return T|null + */ public function getAdapter(): ?AdapterInterface { return $this->adapter; } - public function withAdapter(AdapterInterface $adapter): static + /** + * @param T $adapter + * @return static + */ + public function withAdapter(AdapterInterface $adapter, string|BackedEnum|null $queueName = null): static { $new = clone $this; $new->adapter = $adapter; + if ($queueName !== null) { + $new->name = QueueNameNormalizer::normalize($queueName); + } + return $new; } - public function getChannel(): string + public function getName(): string { - if ($this->adapter === null) { - throw new LogicException('Adapter is not set.'); - } - - return $this->adapter->getChannel(); + return $this->name; } } diff --git a/tests/App/DummyQueue.php b/tests/App/DummyQueue.php index 9286ab64..f15ee673 100644 --- a/tests/App/DummyQueue.php +++ b/tests/App/DummyQueue.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Tests\App; +use BackedEnum; use Exception; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\JobStatus; @@ -14,7 +15,7 @@ final class DummyQueue implements QueueInterface { public function __construct( - private readonly string $channel, + private readonly string $name, ) {} public function push( @@ -36,13 +37,13 @@ public function status(string|int $id): JobStatus throw new Exception('`status()` method is not implemented yet.'); } - public function withAdapter(AdapterInterface $adapter): static + public function withAdapter(AdapterInterface $adapter, string|BackedEnum|null $queueName = null): static { throw new Exception('`withAdapter()` method is not implemented yet.'); } - public function getChannel(): string + public function getName(): string { - return $this->channel; + return $this->name; } } diff --git a/tests/App/FakeAdapter.php b/tests/App/FakeAdapter.php index db5cb39d..4cad928a 100644 --- a/tests/App/FakeAdapter.php +++ b/tests/App/FakeAdapter.php @@ -6,7 +6,7 @@ use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; -use Yiisoft\Queue\ChannelNormalizer; +use Yiisoft\Queue\QueueNameNormalizer; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\MessageInterface; @@ -37,16 +37,10 @@ public function subscribe(callable $handlerCallback): void //skip } - public function withChannel(string|BackedEnum $channel): AdapterInterface + public function withChannel(string|BackedEnum $channel): self { - $instance = clone $this; - $instance->pushMessages = []; - $instance->channel = ChannelNormalizer::normalize($channel); - return $instance; - } - - public function getChannel(): string - { - return $this->channel; + $new = clone $this; + $new->channel = QueueNameNormalizer::normalize($channel); + return $new; } } diff --git a/tests/Benchmark/QueueBench.php b/tests/Benchmark/QueueBench.php index f738e13b..27c37fa1 100644 --- a/tests/Benchmark/QueueBench.php +++ b/tests/Benchmark/QueueBench.php @@ -60,7 +60,7 @@ public function __construct() new SimpleLoop(0), $logger, new PushMiddlewareDispatcher(new MiddlewareFactoryPush($container, $callableFactory)), - $this->adapter, + adapter: $this->adapter, ); } diff --git a/tests/Benchmark/Support/VoidAdapter.php b/tests/Benchmark/Support/VoidAdapter.php index 3a946d4a..8cbcc14e 100644 --- a/tests/Benchmark/Support/VoidAdapter.php +++ b/tests/Benchmark/Support/VoidAdapter.php @@ -4,7 +4,6 @@ namespace Yiisoft\Queue\Tests\Benchmark\Support; -use BackedEnum; use InvalidArgumentException; use RuntimeException; use Yiisoft\Queue\Adapter\AdapterInterface; @@ -44,11 +43,6 @@ public function subscribe(callable $handlerCallback): void throw new RuntimeException('Method is not implemented'); } - public function withChannel(string|BackedEnum $channel): AdapterInterface - { - throw new RuntimeException('Method is not implemented'); - } - public function getChannel(): string { throw new RuntimeException('Method is not implemented'); diff --git a/tests/Integration/MiddlewareTest.php b/tests/Integration/MiddlewareTest.php index 57b62d62..02e02ddf 100644 --- a/tests/Integration/MiddlewareTest.php +++ b/tests/Integration/MiddlewareTest.php @@ -63,6 +63,7 @@ public function testFullStackPush(): void $this->createMock(LoopInterface::class), $this->createMock(LoggerInterface::class), $pushMiddlewareDispatcher, + 'test', new SynchronousAdapter( $this->createMock(WorkerInterface::class), $this->createMock(QueueInterface::class), @@ -136,7 +137,7 @@ public function testFullStackFailure(): void $callableFactory = new CallableFactory($container); $queue->expects(self::exactly(7))->method('push')->willReturnCallback($queueCallback); - $queue->method('getChannel')->willReturn('simple'); + $queue->method('getName')->willReturn('simple'); $middlewares = [ 'simple' => [ diff --git a/tests/Unit/Adapter/SynchronousAdapterTest.php b/tests/Unit/Adapter/SynchronousAdapterTest.php index f05ac078..52deb0b1 100644 --- a/tests/Unit/Adapter/SynchronousAdapterTest.php +++ b/tests/Unit/Adapter/SynchronousAdapterTest.php @@ -5,17 +5,10 @@ namespace Yiisoft\Queue\Tests\Unit\Adapter; use InvalidArgumentException; -use PHPUnit\Framework\Attributes\DataProvider; -use Yiisoft\Queue\Adapter\SynchronousAdapter; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\IdEnvelope; use Yiisoft\Queue\Message\Message; -use Yiisoft\Queue\Provider\QueueProviderInterface; -use Yiisoft\Queue\Stubs\StubQueue; -use Yiisoft\Queue\Stubs\StubWorker; use Yiisoft\Queue\Tests\TestCase; -use Yiisoft\Queue\Tests\Unit\Support\IntEnum; -use Yiisoft\Queue\Tests\Unit\Support\StringEnum; final class SynchronousAdapterTest extends TestCase { @@ -50,35 +43,6 @@ public function testIdSetting(): void self::assertCount(3, array_unique($ids)); } - public function testWithSameChannel(): void - { - $adapter = $this->getAdapter(); - self::assertEquals($adapter, $adapter->withChannel(QueueProviderInterface::DEFAULT_CHANNEL)); - } - - public function testWithAnotherChannel(): void - { - $adapter = $this->getAdapter(); - $adapter->push(new Message('test', null)); - $adapterNew = $adapter->withChannel('test'); - - self::assertNotEquals($adapter, $adapterNew); - - $executed = false; - $adapterNew->runExisting(function () use (&$executed) { - $executed = true; - }); - - self::assertFalse($executed); - - $executed = false; - $adapter->runExisting(function () use (&$executed) { - $executed = true; - }); - - self::assertTrue($executed); - } - public function testStatusIdLessZero(): void { $adapter = $this->getAdapter(); @@ -95,29 +59,6 @@ public function testStatusNotMessage(): void $adapter->status('1'); } - public static function dataChannels(): iterable - { - yield 'string' => ['test', 'test']; - yield 'string-enum' => ['red', StringEnum::RED]; - yield 'integer-enum' => ['1', IntEnum::ONE]; - } - - #[DataProvider('dataChannels')] - public function testWithChannel(string $expected, mixed $channel): void - { - $adapter = (new SynchronousAdapter(new StubWorker(), new StubQueue()))->withChannel($channel); - - $this->assertSame($expected, $adapter->getChannel()); - } - - #[DataProvider('dataChannels')] - public function testChannelInConstructor(string $expected, mixed $channel): void - { - $adapter = new SynchronousAdapter(new StubWorker(), new StubQueue(), $channel); - - $this->assertSame($expected, $adapter->getChannel()); - } - protected function needsRealAdapter(): bool { return true; diff --git a/tests/Unit/Command/ListenCommandTest.php b/tests/Unit/Command/ListenCommandTest.php index 2e2971ea..46dda71f 100644 --- a/tests/Unit/Command/ListenCommandTest.php +++ b/tests/Unit/Command/ListenCommandTest.php @@ -13,24 +13,57 @@ final class ListenCommandTest extends TestCase { - public function testConfigure(): void + public function testExecuteWithDefaultQueue(): void { - $command = new ListenCommand($this->createMock(QueueProviderInterface::class)); - $channelArgument = $command->getNativeDefinition()->getArgument('channel'); - $this->assertEquals('channel', $channelArgument->getName()); + $queue = $this->createMock(QueueInterface::class); + $queue->expects($this->once()) + ->method('listen'); + + $queueProvider = $this->createMock(QueueProviderInterface::class); + $queueProvider->expects($this->once()) + ->method('get') + ->with($this->equalTo('yii-queue')) + ->willReturn($queue); + + $input = new StringInput(''); + $command = new ListenCommand($queueProvider); + $exitCode = $command->run($input, $this->createMock(OutputInterface::class)); + + $this->assertEquals(0, $exitCode); } - public function testExecute(): void + public function testExecuteWithCustomQueue(): void { $queue = $this->createMock(QueueInterface::class); - $queue->expects($this->once())->method('listen'); - $queueFactory = $this->createMock(QueueProviderInterface::class); - $queueFactory->method('get')->willReturn($queue); - $input = new StringInput('channel'); + $queue->expects($this->once()) + ->method('listen'); - $command = new ListenCommand($queueFactory); + $queueProvider = $this->createMock(QueueProviderInterface::class); + $queueProvider->expects($this->once()) + ->method('get') + ->with($this->equalTo('custom-queue')) + ->willReturn($queue); + + $input = new StringInput('custom-queue'); + $command = new ListenCommand($queueProvider); $exitCode = $command->run($input, $this->createMock(OutputInterface::class)); $this->assertEquals(0, $exitCode); } + + public function testExecuteReturnsZero(): void + { + $queue = $this->createMock(QueueInterface::class); + $queue->expects($this->once()) + ->method('listen'); + + $queueProvider = $this->createMock(QueueProviderInterface::class); + $queueProvider->method('get')->willReturn($queue); + + $input = new StringInput(''); + $command = new ListenCommand($queueProvider); + $exitCode = $command->run($input, $this->createMock(OutputInterface::class)); + + $this->assertSame(0, $exitCode); + } } diff --git a/tests/Unit/Command/RunCommandTest.php b/tests/Unit/Command/RunCommandTest.php index 3e38305e..672db54e 100644 --- a/tests/Unit/Command/RunCommandTest.php +++ b/tests/Unit/Command/RunCommandTest.php @@ -13,23 +13,121 @@ final class RunCommandTest extends TestCase { - public function testConfigure(): void + public function testExecuteWithSingleQueue(): void { - $command = new RunCommand($this->createMock(QueueProviderInterface::class), []); - $channelArgument = $command->getNativeDefinition()->getArgument('channel'); - $this->assertEquals('channel', $channelArgument->getName()); + $queue = $this->createMock(QueueInterface::class); + $queue->expects($this->once()) + ->method('run') + ->with($this->equalTo(0)) + ->willReturn(5); + + $queueProvider = $this->createMock(QueueProviderInterface::class); + $queueProvider->expects($this->once()) + ->method('get') + ->with($this->equalTo('test-queue')) + ->willReturn($queue); + + $input = new StringInput('test-queue'); + $output = $this->createMock(OutputInterface::class); + $output->expects($this->once()) + ->method('write') + ->with($this->equalTo('Processing queue test-queue... ')); + $output->expects($this->once()) + ->method('writeln') + ->with($this->equalTo('Messages processed: 5.')); + + $command = new RunCommand($queueProvider, []); + $exitCode = $command->run($input, $output); + + $this->assertEquals(0, $exitCode); } - public function testExecute(): void + public function testExecuteWithMultipleQueues(): void + { + $queue1 = $this->createMock(QueueInterface::class); + $queue1->expects($this->once()) + ->method('run') + ->with($this->equalTo(0)) + ->willReturn(3); + + $queue2 = $this->createMock(QueueInterface::class); + $queue2->expects($this->once()) + ->method('run') + ->with($this->equalTo(0)) + ->willReturn(7); + + $queueProvider = $this->createMock(QueueProviderInterface::class); + $queueProvider->expects($this->exactly(2)) + ->method('get') + ->willReturnOnConsecutiveCalls($queue1, $queue2); + + $output = $this->createMock(OutputInterface::class); + $output->expects($this->exactly(2)) + ->method('write'); + $output->expects($this->exactly(2)) + ->method('writeln'); + + $input = new StringInput('queue1 queue2'); + $command = new RunCommand($queueProvider, []); + $exitCode = $command->run($input, $output); + + $this->assertEquals(0, $exitCode); + } + + public function testExecuteWithMaximumOption(): void { $queue = $this->createMock(QueueInterface::class); - $queue->expects($this->once())->method('run'); + $queue->expects($this->once()) + ->method('run') + ->with($this->equalTo(100)) + ->willReturn(10); + $queueProvider = $this->createMock(QueueProviderInterface::class); - $queueProvider->method('get')->willReturn($queue); - $input = new StringInput('channel'); + $queueProvider->expects($this->once()) + ->method('get') + ->with($this->equalTo('test-queue')) + ->willReturn($queue); + + $input = new StringInput('test-queue --maximum=100'); + $output = $this->createMock(OutputInterface::class); + $output->expects($this->once()) + ->method('write') + ->with($this->equalTo('Processing queue test-queue... ')); + $output->expects($this->once()) + ->method('writeln') + ->with($this->equalTo('Messages processed: 10.')); $command = new RunCommand($queueProvider, []); - $exitCode = $command->run($input, $this->createMock(OutputInterface::class)); + $exitCode = $command->run($input, $output); + + $this->assertEquals(0, $exitCode); + } + + public function testExecuteWithDefaultQueues(): void + { + $queue = $this->createMock(QueueInterface::class); + $queue->expects($this->once()) + ->method('run') + ->with($this->equalTo(0)) + ->willReturn(2); + + $queueProvider = $this->createMock(QueueProviderInterface::class); + $queueProvider->expects($this->once()) + ->method('get') + ->with($this->equalTo('default-queue')) + ->willReturn($queue); + + $input = new StringInput(''); + $output = $this->createMock(OutputInterface::class); + $output->expects($this->once()) + ->method('write') + ->with($this->equalTo('Processing queue default-queue... ')); + $output->expects($this->once()) + ->method('writeln') + ->with($this->equalTo('Messages processed: 2.')); + + $command = new RunCommand($queueProvider, ['default-queue']); + $exitCode = $command->run($input, $output); $this->assertEquals(0, $exitCode); } diff --git a/tests/Unit/Debug/QueueDecoratorTest.php b/tests/Unit/Debug/QueueDecoratorTest.php index 50ec6eef..8d10822b 100644 --- a/tests/Unit/Debug/QueueDecoratorTest.php +++ b/tests/Unit/Debug/QueueDecoratorTest.php @@ -86,17 +86,17 @@ public function testListen(): void $decorator->listen(); } - public function testGetChannel(): void + public function testGetName(): void { $queue = $this->createMock(QueueInterface::class); - $queue->expects($this->once())->method('getChannel')->willReturn('hello'); + $queue->expects($this->once())->method('getName')->willReturn('hello'); $collector = new QueueCollector(); $decorator = new QueueDecorator( $queue, $collector, ); - $this->assertEquals('hello', $decorator->getChannel()); + $this->assertEquals('hello', $decorator->getName()); } public function testImmutable(): void diff --git a/tests/Unit/Middleware/Push/MiddlewareDispatcherTest.php b/tests/Unit/Middleware/Push/MiddlewareDispatcherTest.php index 9be05b16..301540a3 100644 --- a/tests/Unit/Middleware/Push/MiddlewareDispatcherTest.php +++ b/tests/Unit/Middleware/Push/MiddlewareDispatcherTest.php @@ -27,9 +27,12 @@ public function testCallableMiddlewareCalled(): void $dispatcher = $this->createDispatcher()->withMiddlewares( [ - static fn(PushRequest $request, AdapterInterface $adapter): PushRequest => $request - ->withMessage(new Message('test', 'New closure test data')) - ->withAdapter($adapter->withChannel('closure-channel')), + static function (PushRequest $request, AdapterInterface $adapter): PushRequest { + /** @var FakeAdapter $adapter */ + return $request + ->withMessage(new Message('test', 'New closure test data')) + ->withAdapter($adapter->withChannel('closure-channel')); + }, ], ); @@ -78,12 +81,9 @@ public function testMiddlewareFullStackCalled(): void return $handler->handlePush($request); }; $middleware2 = static function (PushRequest $request, MessageHandlerPushInterface $handler): PushRequest { - /** - * @noinspection NullPointerExceptionInspection - * - * @psalm-suppress PossiblyNullReference - */ - $request = $request->withAdapter($request->getAdapter()->withChannel('new channel')); + /** @var FakeAdapter $adapter */ + $adapter = $request->getAdapter(); + $request = $request->withAdapter($adapter->withChannel('new channel')); return $handler->handlePush($request); }; @@ -92,10 +92,6 @@ public function testMiddlewareFullStackCalled(): void $request = $dispatcher->dispatch($request, $this->getRequestHandler()); $this->assertSame('new test data', $request->getMessage()->getData()); - /** - * @psalm-suppress NoInterfaceProperties - * @psalm-suppress PossiblyNullPropertyFetch - */ $this->assertSame('new channel', $request->getAdapter()->channel); } diff --git a/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php b/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php index 50044f49..320bdf02 100644 --- a/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php +++ b/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php @@ -8,7 +8,7 @@ use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Stubs\StubLoop; use Yiisoft\Queue\Provider\AdapterFactoryQueueProvider; -use Yiisoft\Queue\Provider\ChannelNotFoundException; +use Yiisoft\Queue\Provider\QueueNotFoundException; use Yiisoft\Queue\Provider\InvalidQueueConfigException; use Yiisoft\Queue\Stubs\StubQueue; use Yiisoft\Queue\Stubs\StubAdapter; @@ -23,16 +23,17 @@ public function testBase(): void $provider = new AdapterFactoryQueueProvider( new StubQueue(), [ - 'channel1' => StubAdapter::class, + 'queue1' => StubAdapter::class, ], ); - $queue = $provider->get('channel1'); + /** @var StubQueue $queue */ + $queue = $provider->get('queue1'); $this->assertInstanceOf(StubQueue::class, $queue); - $this->assertSame('channel1', $queue->getChannel()); + $this->assertSame('queue1', $queue->getName()); $this->assertInstanceOf(StubAdapter::class, $queue->getAdapter()); - $this->assertTrue($provider->has('channel1')); + $this->assertTrue($provider->has('queue1')); $this->assertFalse($provider->has('not-exist-channel')); } @@ -41,12 +42,12 @@ public function testGetTwice(): void $provider = new AdapterFactoryQueueProvider( new StubQueue(), [ - 'channel1' => StubAdapter::class, + 'queue1' => StubAdapter::class, ], ); - $queue1 = $provider->get('channel1'); - $queue2 = $provider->get('channel1'); + $queue1 = $provider->get('queue1'); + $queue2 = $provider->get('queue1'); $this->assertSame($queue1, $queue2); } @@ -56,12 +57,12 @@ public function testGetNotExistChannel(): void $provider = new AdapterFactoryQueueProvider( new StubQueue(), [ - 'channel1' => StubAdapter::class, + 'queue1' => StubAdapter::class, ], ); - $this->expectException(ChannelNotFoundException::class); - $this->expectExceptionMessage('Channel "not-exist-channel" not found.'); + $this->expectException(QueueNotFoundException::class); + $this->expectExceptionMessage('Queue with name "not-exist-channel" not found.'); $provider->get('not-exist-channel'); } @@ -69,7 +70,7 @@ public function testInvalidQueueConfig(): void { $baseQueue = new StubQueue(); $definitions = [ - 'channel1' => [ + 'queue1' => [ 'class' => StubAdapter::class, '__construct()' => 'hello', ], @@ -87,19 +88,20 @@ public function testInvalidQueueConfigOnGet(): void $provider = new AdapterFactoryQueueProvider( new StubQueue(), [ - 'channel1' => StubLoop::class, + 'queue1' => StubLoop::class, ], ); $this->expectException(InvalidQueueConfigException::class); $this->expectExceptionMessage( sprintf( - 'Adapter must implement "%s". For channel "channel1" got "%s" instead.', + 'Adapter must implement "%s". For queueName "%s" got "%s" instead.', AdapterInterface::class, + 'queue1', StubLoop::class, ), ); - $provider->get('channel1'); + $provider->get('queue1'); } public function testGetHasByStringEnum(): void @@ -113,8 +115,35 @@ public function testGetHasByStringEnum(): void $queue = $provider->get(StringEnum::RED); - $this->assertSame('red', $queue->getChannel()); + $this->assertSame('red', $queue->getName()); $this->assertTrue($provider->has(StringEnum::RED)); $this->assertFalse($provider->has(StringEnum::GREEN)); } + + public function testQueueNameAndAdapterChannelConfiguration(): void + { + $provider = new AdapterFactoryQueueProvider( + new StubQueue(), + [ + 'mail-queue' => [ + 'class' => StubAdapter::class, + '__construct()' => ['channel-name'], + ], + 'log-queue' => StubAdapter::class, + ], + ); + + /** @var StubQueue $mailQueue */ + $mailQueue = $provider->get('mail-queue'); + /** @var StubQueue $logQueue */ + $logQueue = $provider->get('log-queue'); + + $this->assertSame('mail-queue', $mailQueue->getName()); + $this->assertInstanceOf(StubAdapter::class, $mailQueue->getAdapter()); + $this->assertSame('channel-name', $mailQueue->getAdapter()->getChannel()); + + $this->assertSame('log-queue', $logQueue->getName()); + $this->assertInstanceOf(StubAdapter::class, $logQueue->getAdapter()); + $this->assertSame('yii-queue', $logQueue->getAdapter()->getChannel()); + } } diff --git a/tests/Unit/Provider/CompositeQueueProviderTest.php b/tests/Unit/Provider/CompositeQueueProviderTest.php index 2c2e5c39..97293a85 100644 --- a/tests/Unit/Provider/CompositeQueueProviderTest.php +++ b/tests/Unit/Provider/CompositeQueueProviderTest.php @@ -5,7 +5,7 @@ namespace Yiisoft\Queue\Tests\Unit\Provider; use Yiisoft\Queue\Provider\AdapterFactoryQueueProvider; -use Yiisoft\Queue\Provider\ChannelNotFoundException; +use Yiisoft\Queue\Provider\QueueNotFoundException; use Yiisoft\Queue\Provider\CompositeQueueProvider; use Yiisoft\Queue\Stubs\StubAdapter; use Yiisoft\Queue\Stubs\StubQueue; @@ -31,8 +31,8 @@ public function testBase(): void $this->assertTrue($provider->has('channel2')); $this->assertFalse($provider->has('channel3')); - $this->assertSame('channel1', $provider->get('channel1')->getChannel()); - $this->assertSame('channel2', $provider->get('channel2')->getChannel()); + $this->assertSame('channel1', $provider->get('channel1')->getName()); + $this->assertSame('channel2', $provider->get('channel2')->getName()); } public function testNotFound(): void @@ -44,8 +44,8 @@ public function testNotFound(): void ), ); - $this->expectException(ChannelNotFoundException::class); - $this->expectExceptionMessage('Channel "not-exists" not found.'); + $this->expectException(QueueNotFoundException::class); + $this->expectExceptionMessage('Queue with name "not-exists" not found.'); $provider->get('not-exists'); } } diff --git a/tests/Unit/Provider/PrototypeQueueProviderTest.php b/tests/Unit/Provider/PrototypeQueueProviderTest.php deleted file mode 100644 index 3429ee19..00000000 --- a/tests/Unit/Provider/PrototypeQueueProviderTest.php +++ /dev/null @@ -1,28 +0,0 @@ -get('test-channel'); - - $this->assertInstanceOf(StubQueue::class, $queue); - $this->assertSame('test-channel', $queue->getChannel()); - $this->assertTrue($provider->has('test-channel')); - $this->assertTrue($provider->has('yet-another-channel')); - } -} diff --git a/tests/Unit/Provider/ChannelNotFoundExceptionTest.php b/tests/Unit/Provider/QueueNotFoundExceptionTest.php similarity index 67% rename from tests/Unit/Provider/ChannelNotFoundExceptionTest.php rename to tests/Unit/Provider/QueueNotFoundExceptionTest.php index c7bf0bed..326b5771 100644 --- a/tests/Unit/Provider/ChannelNotFoundExceptionTest.php +++ b/tests/Unit/Provider/QueueNotFoundExceptionTest.php @@ -5,11 +5,11 @@ namespace Yiisoft\Queue\Tests\Unit\Provider; use PHPUnit\Framework\Attributes\DataProvider; -use Yiisoft\Queue\Provider\ChannelNotFoundException; +use Yiisoft\Queue\Provider\QueueNotFoundException; use Yiisoft\Queue\Tests\TestCase; use Yiisoft\Queue\Tests\Unit\Support\StringEnum; -final class ChannelNotFoundExceptionTest extends TestCase +final class QueueNotFoundExceptionTest extends TestCase { public static function dataBase(): iterable { @@ -20,10 +20,10 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(string $expectedChannel, mixed $channel): void { - $exception = new ChannelNotFoundException($channel); + $exception = new QueueNotFoundException($channel); - $this->assertSame( - 'Channel "' . $expectedChannel . '" not found.', + $this->assertStringContainsString( + '"' . $expectedChannel . '" not found.', $exception->getMessage(), ); } diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index a3c5cc1d..f4ef3bb5 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -146,21 +146,13 @@ public function testRunWithSignalLoop(): void self::assertEquals(2, $this->executionTimes); } - public function testGetChannel(): void + public function testGetName(): void { $queue = $this ->getQueue() - ->withAdapter(new StubAdapter('test-channel')); + ->withAdapter(new StubAdapter('test-channel'), 'test-queue'); - $this->assertSame('test-channel', $queue->getChannel()); - } - - public function testGetChannelWithoutAdapter(): void - { - $queue = $this->getQueue(); - - $this->expectException(AdapterNotConfiguredException::class); - $queue->getChannel(); + $this->assertSame('test-queue', $queue->getName()); } protected function needsRealAdapter(): bool diff --git a/tests/Unit/Stubs/StubQueueTest.php b/tests/Unit/Stubs/StubQueueTest.php index 7bb5ee32..63f61ff6 100644 --- a/tests/Unit/Stubs/StubQueueTest.php +++ b/tests/Unit/Stubs/StubQueueTest.php @@ -4,7 +4,6 @@ namespace Yiisoft\Queue\Tests\Unit\Stubs; -use LogicException; use PHPUnit\Framework\TestCase; use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\Message; @@ -25,15 +24,6 @@ public function testBase(): void $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(); diff --git a/tests/Unit/WorkerTest.php b/tests/Unit/WorkerTest.php index 5febf399..eb960c47 100644 --- a/tests/Unit/WorkerTest.php +++ b/tests/Unit/WorkerTest.php @@ -201,7 +201,7 @@ public function testJobFailureIsHandledSuccessfully(): void $message = new Message('simple', null); /** @var MockObject&QueueInterface $queue */ $queue = $this->createMock(QueueInterface::class); - $queue->method('getChannel')->willReturn('test-channel'); + $queue->method('getName')->willReturn('test-queue'); $originalException = new RuntimeException('Consume failed'); /** @var MiddlewareConsumeInterface&MockObject $consumeMiddleware */ @@ -221,7 +221,7 @@ public function testJobFailureIsHandledSuccessfully(): void /** @var MiddlewareFactoryFailureInterface&MockObject $failureMiddlewareFactory */ $failureMiddlewareFactory = $this->createMock(MiddlewareFactoryFailureInterface::class); $failureMiddlewareFactory->method('createFailureMiddleware')->willReturn($failureMiddleware); - $failureDispatcher = new FailureMiddlewareDispatcher($failureMiddlewareFactory, ['test-channel' => ['simple']]); + $failureDispatcher = new FailureMiddlewareDispatcher($failureMiddlewareFactory, ['test-queue' => ['simple']]); $worker = new Worker( ['simple' => fn() => null],