Skip to content

Commit

Permalink
Fix #140 (#147)
Browse files Browse the repository at this point in the history
* Fix #140

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
viktorprogger and StyleCIBot committed Feb 25, 2023
1 parent 26dd3bf commit 1975086
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/Queue.php
Expand Up @@ -134,7 +134,15 @@ public function withMiddlewaresAdded(MiddlewarePushInterface|callable|array|stri
return $instance;
}

protected function handle(MessageInterface $message): void
public function withChannelName(string $channel): self
{
$instance = clone $this;
$instance->channelName = $channel;

return $instance;
}

private function handle(MessageInterface $message): void
{
$this->worker->process($message, $this);
}
Expand Down
9 changes: 5 additions & 4 deletions src/QueueFactory.php
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Definitions\ArrayDefinition;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use Yiisoft\Definitions\Helpers\DefinitionValidator;
use Yiisoft\Factory\Factory;
use Yiisoft\Injector\Injector;
use Yiisoft\Yii\Queue\Adapter\AdapterInterface;
use Yiisoft\Yii\Queue\Exception\AdapterConfiguration\ChannelIncorrectlyConfigured;
Expand All @@ -30,7 +29,9 @@ final class QueueFactory implements QueueFactoryInterface
* "Definition" here is a {@see Factory} definition
* @param QueueInterface $queue A default queue implementation. `$queue->withAdapter()` will be returned
* with the `get` method
* @param Factory $yiiFactory
* @param ContainerInterface $container
* @param CallableFactory $callableFactory
* @param Injector $injector
* @param bool $enableRuntimeChannelDefinition A flag whether to enable a such behavior when there is no
* explicit channel adapter definition: `return $this->queue->withAdapter($this->adapter->withChannel($channel)`
* When this flag is set to false, only explicit definitions from the $definition parameter are used.
Expand Down Expand Up @@ -83,15 +84,15 @@ private function create(string $channel): QueueInterface
$this->checkDefinitionType($channel, $definition);
$adapter = $this->createFromDefinition($channel, $definition);

return $this->queue->withAdapter($adapter);
return $this->queue->withChannelName($channel)->withAdapter($adapter);
}

if ($this->enableRuntimeChannelDefinition === false) {
throw new ChannelNotConfiguredException($channel);
}

/** @psalm-suppress PossiblyNullReference */
return $this->queue->withAdapter($this->defaultAdapter->withChannel($channel));
return $this->queue->withChannelName($channel)->withAdapter($this->defaultAdapter->withChannel($channel));
}

private function checkDefinitionType(string $channel, mixed $definition): void
Expand Down
2 changes: 2 additions & 0 deletions src/QueueInterface.php
Expand Up @@ -50,4 +50,6 @@ public function status(string $id): JobStatus;
public function withAdapter(AdapterInterface $adapter): self;

public function getChannelName(): string;

public function withChannelName(string $channel): self;
}
76 changes: 76 additions & 0 deletions tests/Integration/QueueFactoryTest.php
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Tests\Integration;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Yiisoft\Injector\Injector;
use Yiisoft\Yii\Queue\Adapter\SynchronousAdapter;
use Yiisoft\Yii\Queue\Cli\LoopInterface;
use Yiisoft\Yii\Queue\Middleware\CallableFactory;
use Yiisoft\Yii\Queue\Middleware\Push\MiddlewareFactoryPushInterface;
use Yiisoft\Yii\Queue\Middleware\Push\PushMiddlewareDispatcher;
use Yiisoft\Yii\Queue\Queue;
use Yiisoft\Yii\Queue\QueueFactory;
use Yiisoft\Yii\Queue\Tests\App\FakeAdapter;
use Yiisoft\Yii\Queue\Worker\WorkerInterface;

final class QueueFactoryTest extends TestCase
{
public function testQuickChange(): void
{
$worker = $this->createMock(WorkerInterface::class);
$queue = new Queue(
$worker,
$this->createMock(LoopInterface::class),
$this->createMock(LoggerInterface::class),
new PushMiddlewareDispatcher($this->createMock(MiddlewareFactoryPushInterface::class)),
);
$container = $this->createMock(ContainerInterface::class);
$factory = new QueueFactory(
[],
$queue,
$container,
new CallableFactory($container),
new Injector($container),
true,
new SynchronousAdapter($worker, $queue)
);

$adapter = $factory->get('test-channel');

self::assertEquals('test-channel', $adapter->getChannelName());
}

public function testConfiguredChange(): void
{
$worker = $this->createMock(WorkerInterface::class);
$queue = new Queue(
$worker,
$this->createMock(LoopInterface::class),
$this->createMock(LoggerInterface::class),
new PushMiddlewareDispatcher($this->createMock(MiddlewareFactoryPushInterface::class)),
);
$container = $this->createMock(ContainerInterface::class);
$factory = new QueueFactory(
[
'test-channel' => [
'class' => FakeAdapter::class,
'withChannel()' => ['test-channel'],
],
],
$queue,
$container,
new CallableFactory($container),
new Injector($container),
true,
new SynchronousAdapter($worker, $queue)
);
$queue = $factory->get('test-channel');

self::assertEquals('test-channel', $queue->getChannelName());
}
}
23 changes: 16 additions & 7 deletions tests/Unit/QueueFactoryTest.php
Expand Up @@ -8,7 +8,6 @@
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use stdClass;
use Yiisoft\Factory\Factory;
use Yiisoft\Injector\Injector;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Yii\Queue\Adapter\AdapterInterface;
Expand All @@ -25,7 +24,12 @@ public function testRuntimeDefinitionSuccessful(): void
$queue = $this->createMock(QueueInterface::class);
$queue
->expects(self::once())
->method('withAdapter');
->method('withAdapter')
->willReturn($queue);
$queue
->expects(self::once())
->method('withChannelName')
->willReturn($queue);

$adapter = $this->createMock(AdapterInterface::class);
$adapter
Expand Down Expand Up @@ -108,6 +112,11 @@ public function testSuccessfulDefinitionWithDefaultAdapter(): void
->method('withAdapter')
->with($adapterNew)
->willReturn($queue);
$queue
->expects(self::once())
->method('withChannelName')
->with('test')
->willReturn($queue);

$factory = new QueueFactory(
['test' => $adapterNew],
Expand All @@ -132,6 +141,11 @@ public function testSuccessfulDefinitionWithoutDefaultAdapter(): void
->method('withAdapter')
->with($adapterNew)
->willReturn($queue);
$queue
->expects(self::once())
->method('withChannelName')
->with('test')
->willReturn($queue);

$factory = new QueueFactory(
['test' => $adapterNew],
Expand All @@ -145,11 +159,6 @@ public function testSuccessfulDefinitionWithoutDefaultAdapter(): void
$factory->get('test');
}

private function createYiiFactory(): Factory
{
return new Factory(new SimpleContainer());
}

private function getContainer(array $instances = []): ContainerInterface
{
return new SimpleContainer($instances);
Expand Down

0 comments on commit 1975086

Please sign in to comment.