Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Settings/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Yiisoft\Queue\Amqp\Settings;

use PhpAmqpLib\Wire\AMQPTable;
use Yiisoft\Queue\Provider\QueueProviderInterface;
use Yiisoft\Queue\DefaultQueue;

final class Queue implements QueueSettingsInterface
{
public function __construct(
private string $queueName = QueueProviderInterface::DEFAULT_QUEUE,
private string $queueName = DefaultQueue::NAME,
private bool $passive = false,
private bool $durable = true,
private bool $exclusive = false,
Expand Down
26 changes: 8 additions & 18 deletions tests/Integration/DelayMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Yiisoft\Queue\Amqp\Tests\Integration;

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Amqp\Adapter;
Expand All @@ -14,17 +13,15 @@
use Yiisoft\Queue\Amqp\Settings\Exchange as ExchangeSettings;
use Yiisoft\Queue\Amqp\Tests\Support\FakeAdapter;
use Yiisoft\Queue\Amqp\Tests\Support\FileHelper;
use Yiisoft\Queue\Cli\LoopInterface;
use Yiisoft\Queue\Cli\SignalLoop;
use Yiisoft\Queue\Message\Serializer\JsonMessageEncoder;
use Yiisoft\Queue\Message\Serializer\MessageSerializer;
use Yiisoft\Queue\Amqp\Tests\Support\TestMessage as Message;
use Yiisoft\Queue\Middleware\CallableFactory;
use Yiisoft\Queue\Middleware\Push\PushMiddlewareConfig;
use Yiisoft\Queue\Middleware\Push\PushMiddlewareFactory;
use Yiisoft\Queue\Provider\QueueProviderInterface;
use Yiisoft\Queue\Queue;
use Yiisoft\Queue\Worker\WorkerInterface;
use Yiisoft\Queue\AsyncQueueProducer;
use Yiisoft\Queue\DefaultQueue;
use Yiisoft\Queue\QueueProducerInterface;
use Yiisoft\Test\Support\Container\SimpleContainer;
use LogicException;

Expand Down Expand Up @@ -87,21 +84,14 @@ public function testMainFlowWithFakeAdapter(): void
);
}

private function makeQueue(AdapterInterface $adapter, mixed ...$middlewareDefinitions): Queue
private function makeQueue(AdapterInterface $adapter, mixed ...$middlewareDefinitions): QueueProducerInterface
{
return new Queue(
$this->createMock(WorkerInterface::class),
$this->createMock(LoopInterface::class),
return new AsyncQueueProducer(
$this->createMock(LoggerInterface::class),
new PushMiddlewareConfig(
new PushMiddlewareFactory(
new SimpleContainer(),
new CallableFactory($this->createMock(ContainerInterface::class)),
),
),
new PushMiddlewareConfig(new PushMiddlewareFactory(new SimpleContainer())),
$adapter,
QueueProviderInterface::DEFAULT_QUEUE,
...$middlewareDefinitions,
DefaultQueue::NAME,
$middlewareDefinitions,
);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/QueueProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testWithQueueAndExchangeSettings(): void
$this->getLoop(),
);

$queue = $this->getQueueWithAdapter($adapter);
$queue = $this->getProducer($adapter);

$fileHelper = new FileHelper();
$time = time();
Expand All @@ -57,7 +57,7 @@ public function testWithQueueAndExchangeSettings(): void

self::assertNull($fileHelper->get('test-with-queue-settings'));

$queue->run();
$this->getConsumer($adapter)->run();

$result = $fileHelper->get('test-with-queue-settings');
self::assertNotNull($result);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/QueueSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testArgumentsXExpires(): void
$this->getLoop(),
);

$this->getQueueWithAdapter($adapter)
$this->getProducer($adapter)
->push(
Message::fromPayload('ext-simple', ['payload' => time()]),
);
Expand Down
21 changes: 7 additions & 14 deletions tests/Unit/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Amqp\Adapter;
use Yiisoft\Queue\Amqp\Exception\NotImplementedException;
use Yiisoft\Queue\Amqp\QueueProvider;
Expand All @@ -27,7 +26,6 @@
use Yiisoft\Queue\Message\Serializer\MessageSerializer;
use Yiisoft\Queue\Amqp\Tests\Support\TestMessage as Message;
use Yiisoft\Queue\Message\Serializer\MessageSerializerInterface;
use Yiisoft\Queue\Queue;

final class QueueTest extends UnitTestCase
{
Expand All @@ -41,7 +39,7 @@ public function testStatus(): void
$adapter = $this->getAdapter();
$adapterClass = $adapter::class;

$queue = $this->getDefaultQueue($adapter);
$queue = $this->getProducer($adapter);

$message = Message::fromPayload('ext-simple', null);
$queue->push(
Expand All @@ -65,15 +63,15 @@ public function testRun(): void

$this->queueSettings = new QueueSettings($this->queueName);

$queue = $this->getDefaultQueue($this->getAdapter());
$queue = $this->getProducer($this->getAdapter());

$queue->push(
Message::fromPayload('ext-simple', ['file_name' => $fileName, 'payload' => ['time' => $time]]),
);

self::assertNull($fileHelper->get($fileName));

$queue->run();
$this->getConsumer($this->getAdapter())->run();

$result = $fileHelper->get($fileName);
self::assertNotNull($result);
Expand All @@ -96,14 +94,14 @@ public function testListenWithException(): void
new MessageSerializer(new JsonMessageEncoder()),
$this->getLoop(),
);
$queue = $this->getDefaultQueue($adapter);
$queue = $this->getProducer($adapter);

$time = time();
$queue->push(Message::fromPayload('exception-listen', ['payload' => ['time' => $time]]));

$this->expectException(MessageFailureException::class);

$queue->listen();
$this->getConsumer($adapter)->listen();

$this->expectExceptionMessage((string) $time);
}
Expand All @@ -124,12 +122,12 @@ public function testListen(): void
new MessageSerializer(new JsonMessageEncoder()),
$mockLoop,
);
$queue = $this->getDefaultQueue($adapter);
$queue = $this->getProducer($adapter);

$queue->push(
Message::fromPayload('ext-simple', ['file_name' => 'test-listen' . $time, 'payload' => ['time' => $time]]),
);
$queue->listen();
$this->getConsumer($adapter)->listen();
}

public function testImmutable(): void
Expand Down Expand Up @@ -290,9 +288,4 @@ public function testSubscribeSkipsBasicQosWhenNotConfigured(): void
$adapter = new Adapter($queueProvider, $this->createMock(MessageSerializerInterface::class), $loop);
$adapter->subscribe(static fn() => null);
}

private function getDefaultQueue(AdapterInterface $adapter): Queue
{
return $this->getQueueWithAdapter($adapter);
}
}
41 changes: 14 additions & 27 deletions tests/Unit/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPUnit\Util\Exception as PHPUnitException;
use Psr\Container\ContainerInterface;
use Psr\Log\NullLogger;
use Yiisoft\Injector\Injector;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Amqp\Adapter;
use Yiisoft\Queue\Amqp\QueueProvider;
Expand All @@ -17,17 +16,20 @@
use Yiisoft\Queue\Amqp\Tests\Support\MainTestCase;
use Yiisoft\Queue\Cli\LoopInterface;
use Yiisoft\Queue\Cli\SignalLoop;
use Yiisoft\Queue\AsyncQueueProducer;
use Yiisoft\Queue\Message\Serializer\JsonMessageEncoder;
use Yiisoft\Queue\Message\Serializer\MessageSerializer;
use Yiisoft\Queue\Message\Handler\HandlerResolver;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\CallableFactory;
use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher;
use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareFactory;
use Yiisoft\Queue\Middleware\FailureHandling\FailureMiddlewareDispatcher;
use Yiisoft\Queue\Middleware\FailureHandling\FailureMiddlewareFactory;
use Yiisoft\Queue\Middleware\Push\PushMiddlewareConfig;
use Yiisoft\Queue\Middleware\Push\PushMiddlewareFactory;
use Yiisoft\Queue\Queue;
use Yiisoft\Queue\QueueConsumer;
use Yiisoft\Queue\QueueConsumerInterface;
use Yiisoft\Queue\QueueProducerInterface;
use Yiisoft\Queue\Worker\Worker;
use Yiisoft\Queue\Worker\WorkerInterface;
use Yiisoft\Test\Support\Container\SimpleContainer;
Expand All @@ -43,7 +45,6 @@ abstract class UnitTestCase extends MainTestCase
{
public ?QueueSettings $queueSettings = null;
public ?QueueProvider $queueProvider = null;
protected ?Queue $queue = null;
protected ?WorkerInterface $worker = null;
protected ?ContainerInterface $container = null;
protected ?AdapterInterface $adapter = null;
Expand All @@ -69,37 +70,32 @@ protected function tearDown(): void
parent::tearDown();
}

protected function getQueue(): Queue
protected function getProducer(AdapterInterface $adapter): QueueProducerInterface
{
return $this->queue ??= new Queue(
$this->getWorker(),
$this->getLoop(),
return new AsyncQueueProducer(
new NullLogger(),
$this->getPushMiddlewareConfig(),
$adapter,
);
}

protected function getQueueWithAdapter(AdapterInterface $adapter): Queue
protected function getConsumer(AdapterInterface $adapter): QueueConsumerInterface
{
return new Queue(
return new QueueConsumer(
$this->getWorker(),
$this->getLoop(),
new NullLogger(),
$this->getPushMiddlewareConfig(),
$adapter,
);
}

protected function getWorker(): WorkerInterface
{
return $this->worker ??= new Worker(
$this->getMessageHandlers(),
new NullLogger(),
new Injector($this->getContainer()),
$this->getContainer(),
$this->getConsumeMiddlewareDispatcher(),
$this->getFailureMiddlewareDispatcher(),
new CallableFactory($this->getContainer()),
new HandlerResolver($this->getMessageHandlers(), $this->getContainer()),
);
}

Expand Down Expand Up @@ -133,20 +129,14 @@ protected function getContainerDefinitions(): array
protected function getConsumeMiddlewareDispatcher(): ConsumeMiddlewareDispatcher
{
return new ConsumeMiddlewareDispatcher(
new ConsumeMiddlewareFactory(
$this->getContainer(),
new CallableFactory($this->getContainer()),
),
new ConsumeMiddlewareFactory($this->getContainer()),
);
}

protected function getFailureMiddlewareDispatcher(): FailureMiddlewareDispatcher
{
return new FailureMiddlewareDispatcher(
new FailureMiddlewareFactory(
$this->getContainer(),
new CallableFactory($this->getContainer()),
),
new FailureMiddlewareFactory($this->getContainer()),
[],
);
}
Expand All @@ -168,10 +158,7 @@ protected function getLoop(): LoopInterface
protected function getPushMiddlewareConfig(): PushMiddlewareConfig
{
return new PushMiddlewareConfig(
new PushMiddlewareFactory(
$this->getContainer(),
new CallableFactory($this->getContainer()),
),
new PushMiddlewareFactory($this->getContainer()),
);
}

Expand Down
38 changes: 16 additions & 22 deletions tests/yii
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use PhpAmqpLib\Connection\AMQPStreamConnection;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Application;
use Yiisoft\Injector\Injector;
use Yiisoft\Queue\Amqp\Adapter;
use Yiisoft\Queue\Amqp\QueueProvider;
use Yiisoft\Queue\Amqp\Settings\Exchange as ExchangeSettings;
Expand All @@ -14,39 +13,35 @@ use Yiisoft\Queue\Amqp\Tests\Support\SimpleMessageHandler;
use Yiisoft\Queue\Cli\SignalLoop;
use Yiisoft\Queue\Command\ListenCommand;
use Yiisoft\Queue\Command\RunCommand;
use Yiisoft\Queue\DefaultQueue;
use Yiisoft\Queue\Message\Handler\HandlerResolver;
use Yiisoft\Queue\Message\Serializer\JsonMessageEncoder;
use Yiisoft\Queue\Message\Serializer\MessageSerializer;
use Yiisoft\Queue\Middleware\CallableFactory;
use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher;
use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareFactory;
use Yiisoft\Queue\Middleware\FailureHandling\FailureMiddlewareDispatcher;
use Yiisoft\Queue\Middleware\FailureHandling\FailureMiddlewareFactory;
use Yiisoft\Queue\Middleware\Push\PushMiddlewareConfig;
use Yiisoft\Queue\Middleware\Push\PushMiddlewareFactory;
use Yiisoft\Queue\Provider\PredefinedQueueProvider;
use Yiisoft\Queue\Queue;
use Yiisoft\Queue\Provider\QueueProviderInterface;
use Yiisoft\Queue\QueueConsumer;
use Yiisoft\Queue\Worker\Worker;
use Yiisoft\Test\Support\Container\SimpleContainer;

require_once dirname(__DIR__) . '/vendor/autoload.php';

$logger = new NullLogger();
$container = new SimpleContainer([]);
$injector = new Injector($container);
$callableFactory = new CallableFactory($container);
$worker = new \Yiisoft\Queue\Worker\Worker(
[
'simple' => new SimpleMessageHandler(new FileHelper()),
],
$worker = new Worker(
$logger,
$injector,
$container,
new ConsumeMiddlewareDispatcher(new ConsumeMiddlewareFactory($container, $callableFactory)),
new FailureMiddlewareDispatcher(new FailureMiddlewareFactory($container, $callableFactory), []),
$callableFactory,
new ConsumeMiddlewareDispatcher(new ConsumeMiddlewareFactory($container)),
new FailureMiddlewareDispatcher(new FailureMiddlewareFactory($container), []),
new HandlerResolver(
[
'simple' => new SimpleMessageHandler(new FileHelper()),
],
$container,
),
);
$loop = new SignalLoop();
$pushMiddlewareConfig = new PushMiddlewareConfig(new PushMiddlewareFactory($container, $callableFactory));
$adapter = new Adapter(
new QueueProvider(
new AMQPStreamConnection(
Expand All @@ -55,20 +50,19 @@ $adapter = new Adapter(
getenv('RABBITMQ_USER'),
getenv('RABBITMQ_PASSWORD'),
),
new QueueSettings(queueName: QueueProviderInterface::DEFAULT_QUEUE),
new QueueSettings(queueName: DefaultQueue::NAME),
new ExchangeSettings(exchangeName: QueueProvider::EXCHANGE_NAME_DEFAULT),
),
new MessageSerializer(new JsonMessageEncoder()),
$loop,
);
$queue = new Queue(
$consumer = new QueueConsumer(
$worker,
$loop,
$logger,
$pushMiddlewareConfig,
$adapter,
);
$queueFactory = new PredefinedQueueProvider([QueueProviderInterface::DEFAULT_QUEUE => $queue]);
$queueFactory = new PredefinedQueueProvider([DefaultQueue::NAME => ['consumer' => $consumer]]);

$application = new Application();
$application->add(new ListenCommand($queueFactory));
Expand Down
Loading