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
9 changes: 9 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@
>
<projectFiles>
<directory name="src" />
<directory name="stubs" />
<directory name="tests" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<directory name="tests" />
</errorLevel>
</PropertyNotSetInConstructor>
</issueHandlers>
</psalm>
11 changes: 11 additions & 0 deletions src/Debug/QueueDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Yiisoft\Queue\MessageStatus;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\Push\MiddlewarePushInterface;
use Yiisoft\Queue\QueueInterface;

final class QueueDecorator implements QueueInterface
Expand Down Expand Up @@ -44,4 +45,14 @@ public function getName(): string
{
return $this->queue->getName();
}

public function withMiddlewares(MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions): self
{
return new self($this->queue->withMiddlewares(...$middlewareDefinitions), $this->collector);
}

public function withMiddlewaresAdded(MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions): self
{
return new self($this->queue->withMiddlewaresAdded(...$middlewareDefinitions), $this->collector);
}
}
16 changes: 13 additions & 3 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ public function __construct(
) {
$this->name = StringNormalizer::normalize($name);
$this->middlewareDefinitions = $middlewareDefinitions;
$this->finalPushHandler = $this->isSynchronous()
? new SynchronousPushHandler($worker, $this)
: new AdapterPushHandler($this->adapter);
$this->finalPushHandler = $this->createFinalPushHandler();
}

public function __clone()
{
$this->finalPushHandler = $this->createFinalPushHandler();
}

public function getName(): string
Expand Down Expand Up @@ -189,6 +192,13 @@ public function handlePush(MessageInterface $message): MessageInterface
};
}

private function createFinalPushHandler(): MessageHandlerPushInterface
{
return $this->isSynchronous()
? new SynchronousPushHandler($this->worker, $this)
: new AdapterPushHandler($this->adapter);
}

/**
* @psalm-assert-if-false !null $this->adapter
*/
Expand Down
15 changes: 15 additions & 0 deletions src/QueueInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Queue;

use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\Push\MiddlewarePushInterface;

interface QueueInterface
{
Expand Down Expand Up @@ -40,4 +41,18 @@ public function status(string|int $id): MessageStatus;
* Returns the logical name of the queue.
*/
public function getName(): string;

/**
* Creates a new instance with the specified middlewares. All the existing middlewares are replaced.
*
* @param MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions The middleware definitions.
*/
public function withMiddlewares(MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions): self;

/**
* Creates a new instance with the specified middlewares added after the existing ones.
*
* @param MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions The middleware definitions.
*/
public function withMiddlewaresAdded(MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions): self;
Comment on lines +45 to +57
}
11 changes: 11 additions & 0 deletions stubs/StubQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Yiisoft\Queue\MessageStatus;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\Push\MiddlewarePushInterface;
use Yiisoft\Queue\QueueInterface;

/**
Expand Down Expand Up @@ -38,4 +39,14 @@ public function getName(): string
{
return $this->name;
}

public function withMiddlewares(MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions): self
{
return $this;
}

public function withMiddlewaresAdded(MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions): self
{
return $this;
}
}
11 changes: 11 additions & 0 deletions tests/App/DummyQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Exception;
use Yiisoft\Queue\MessageStatus;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\Push\MiddlewarePushInterface;
use Yiisoft\Queue\QueueInterface;

final class DummyQueue implements QueueInterface
Expand Down Expand Up @@ -36,4 +37,14 @@ public function getName(): string
{
return $this->name;
}

public function withMiddlewares(MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions): self
{
throw new Exception('`withMiddlewares()` method is not implemented yet.');
}

public function withMiddlewaresAdded(MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions): self
{
throw new Exception('`withMiddlewaresAdded()` method is not implemented yet.');
}
}
8 changes: 5 additions & 3 deletions tests/App/FakeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Queue\Tests\App;

use BackedEnum;
use Exception;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\StringNormalizer;
use Yiisoft\Queue\MessageStatus;
Expand All @@ -24,23 +25,24 @@ public function push(MessageInterface $message): MessageInterface

public function runExisting(callable $handlerCallback): void
{
//skip
throw new Exception('`runExisting()` method is not implemented yet.');
}

public function status(string|int $id): MessageStatus
{
//skip
throw new Exception('`status()` method is not implemented yet.');
}

public function subscribe(callable $handlerCallback): void
{
//skip
throw new Exception('`subscribe()` method is not implemented yet.');
}

public function withChannel(string|BackedEnum $channel): self
{
$new = clone $this;
$new->channel = StringNormalizer::normalize($channel);

return $new;
}
}
2 changes: 1 addition & 1 deletion tests/App/StaticMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Queue\Tests\App;

class StaticMessageHandler
final class StaticMessageHandler
{
public static bool $wasHandled = false;

Expand Down
7 changes: 0 additions & 7 deletions tests/Benchmark/MetadataBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Generator;
use PhpBench\Attributes\ParamProviders;
use PhpBench\Model\Tag;
use Yiisoft\Queue\Message\IdEnvelope;
use Yiisoft\Queue\Message\Message;
use Yiisoft\Queue\Message\MessageInterface;
Expand All @@ -17,7 +16,6 @@ final class MetadataBench
/**
* Create metadata as an array and read its value from an array.
*/
#[Tag('metadata_read')]
public function benchArrayRead(): void
{
$message = new Message('foo', 'bar', ['id' => 1]);
Expand All @@ -27,7 +25,6 @@ public function benchArrayRead(): void
/**
* Create metadata as an object and read its value immediately.
*/
#[Tag('metadata_read')]
public function benchEnvelopeRead(): void
{
$message = new IdEnvelope(new Message('foo', 'bar'), 1);
Expand All @@ -37,7 +34,6 @@ public function benchEnvelopeRead(): void
/**
* Create metadata as an array and read its value from an envelope object.
*/
#[Tag('metadata_read')]
public function benchEnvelopeReadRestored(): void
{
$message = IdEnvelope::fromMessage(new Message('foo', 'bar', ['id' => 1]));
Expand All @@ -63,7 +59,6 @@ public function provideEnvelopeStack(): Generator
* @psalm-param array{message: MessageInterface} $params
*/
#[ParamProviders('provideEnvelopeStack')]
#[Tag('metadata_read')]
public function benchEnvelopeReadFromStack(array $params): void
{
$id = IdEnvelope::fromMessage($params['message'])->getId();
Expand All @@ -82,7 +77,6 @@ public function provideEnvelopeStackCounts(): Generator
* @psalm-param array{0: int} $params
*/
#[ParamProviders('provideEnvelopeStackCounts')]
#[Tag('metadata_create')]
public function benchEnvelopeStackCreation(array $params): void
{
$message = new Message('foo', 'bar');
Expand All @@ -97,7 +91,6 @@ public function benchEnvelopeStackCreation(array $params): void
* @psalm-param array{0: int} $params
*/
#[ParamProviders('provideEnvelopeStackCounts')]
#[Tag('metadata_create')]
public function benchMetadataArrayCreation(array $params): void
{
$metadata = ['failure-meta' => []];
Expand Down
3 changes: 0 additions & 3 deletions tests/Benchmark/QueueBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Generator;
use PhpBench\Attributes\ParamProviders;
use PhpBench\Model\Tag;
use Psr\Log\NullLogger;
use Yiisoft\Injector\Injector;
use Yiisoft\Queue\Cli\SimpleLoop;
Expand Down Expand Up @@ -80,7 +79,6 @@ public function providePush(): Generator
}

#[ParamProviders('providePush')]
#[Tag('queue_push')]
public function benchPush(array $params): void
{
$this->queue->push($params['message']);
Expand All @@ -103,7 +101,6 @@ public function provideConsume(): Generator
}

#[ParamProviders('provideConsume')]
#[Tag('queue_consume')]
public function benchConsume(array $params): void
{
$this->adapter->message = $params['message'];
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Cli/SignalLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use const SIGTERM;
use const SIGTSTP;

/**
* @psalm-suppress UndefinedConstant
* @psalm-suppress PossiblyFalseArgument
*/
#[RequiresPhpExtension('pcntl')]
final class SignalLoopTest extends TestCase
{
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Debug/QueueCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ protected function setUp(): void
}

/**
* @param CollectorInterface|QueueCollector $collector
* @param QueueCollector $collector
* @psalm-suppress MoreSpecificImplementedParamType
*/
protected function collectTestData(CollectorInterface $collector): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Debug/QueueDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\QueueInterface;

class QueueDecoratorTest extends TestCase
final class QueueDecoratorTest extends TestCase
{
public function testStatus(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Debug/QueueProviderInterfaceProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Yiisoft\Queue\Provider\QueueProviderInterface;
use Yiisoft\Queue\QueueInterface;

class QueueProviderInterfaceProxyTest extends TestCase
final class QueueProviderInterfaceProxyTest extends TestCase
{
public function testGet(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Message/EnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testFromData(): void
public function testNonArrayStackIsNormalized(): void
{
$base = new Message('handler', 'data', [EnvelopeInterface::ENVELOPE_STACK_KEY => 'oops']);
$wrapped = new DummyEnvelope($base, 'id-1');
$wrapped = new DummyEnvelope($base);

$meta = $wrapped->getMetadata();
self::assertIsArray($meta[EnvelopeInterface::ENVELOPE_STACK_KEY]);
Expand Down
18 changes: 10 additions & 8 deletions tests/Unit/Message/JsonMessageSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use function sprintf;

use const JSON_THROW_ON_ERROR;

/**
* Testing message serialization options
*/
Expand All @@ -31,7 +33,7 @@ public function testTypeFormat(mixed $type): void

$this->expectExceptionMessage(sprintf('Message type must be a string. Got %s.', get_debug_type($type)));
$this->expectException(InvalidArgumentException::class);
$serializer->unserialize(json_encode($payload));
$serializer->unserialize(json_encode($payload, JSON_THROW_ON_ERROR));
}

public static function dataUnsupportedTypeFormat(): iterable
Expand All @@ -53,7 +55,7 @@ public function testDefaultMessageClassFallbackWrongClass(): void
],
];

$message = $serializer->unserialize(json_encode($payload));
$message = $serializer->unserialize(json_encode($payload, JSON_THROW_ON_ERROR));
$this->assertInstanceOf(Message::class, $message);
}

Expand All @@ -65,7 +67,7 @@ public function testDefaultMessageClassFallbackClassNotSet(): void
'data' => 'test',
'meta' => [],
];
$message = $serializer->unserialize(json_encode($payload));
$message = $serializer->unserialize(json_encode($payload, JSON_THROW_ON_ERROR));
$this->assertInstanceOf(Message::class, $message);
}

Expand All @@ -76,7 +78,7 @@ public function testPayloadFormat(mixed $payload): void

$this->expectExceptionMessage(sprintf('Payload must be array. Got %s.', get_debug_type($payload)));
$this->expectException(InvalidArgumentException::class);
$serializer->unserialize(json_encode($payload));
$serializer->unserialize(json_encode($payload, JSON_THROW_ON_ERROR));
}

public static function dataUnsupportedPayloadFormat(): iterable
Expand All @@ -95,7 +97,7 @@ public function testMetadataFormat(mixed $meta): void

$this->expectExceptionMessage(sprintf('Metadata must be an array. Got %s.', get_debug_type($meta)));
$this->expectException(InvalidArgumentException::class);
$serializer->unserialize(json_encode($payload));
$serializer->unserialize(json_encode($payload, JSON_THROW_ON_ERROR));
}

public static function dataUnsupportedMetadataFormat(): iterable
Expand All @@ -110,7 +112,7 @@ public function testUnserializeFromData(): void
$payload = ['type' => 'handler', 'data' => 'test'];
$serializer = $this->createSerializer();

$message = $serializer->unserialize(json_encode($payload));
$message = $serializer->unserialize(json_encode($payload, JSON_THROW_ON_ERROR));

$this->assertEquals($payload['data'], $message->getData());
$this->assertEquals([EnvelopeInterface::ENVELOPE_STACK_KEY => []], $message->getMetadata());
Expand All @@ -121,7 +123,7 @@ public function testUnserializeWithMetadata(): void
$payload = ['type' => 'handler', 'data' => 'test', 'meta' => ['int' => 1, 'str' => 'string', 'bool' => true]];
$serializer = $this->createSerializer();

$message = $serializer->unserialize(json_encode($payload));
$message = $serializer->unserialize(json_encode($payload, JSON_THROW_ON_ERROR));

$this->assertEquals($payload['data'], $message->getData());
$this->assertEquals(['int' => 1, 'str' => 'string', 'bool' => true, EnvelopeInterface::ENVELOPE_STACK_KEY => []], $message->getMetadata());
Expand All @@ -141,7 +143,7 @@ public function testUnserializeEnvelopeStack(): void
$serializer = $this->createSerializer();

/** @var IdEnvelope $message */
$message = $serializer->unserialize(json_encode($payload));
$message = $serializer->unserialize(json_encode($payload, JSON_THROW_ON_ERROR));

$this->assertEquals($payload['data'], $message->getData());
$this->assertEquals([IdEnvelope::class], $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY]);
Expand Down
Loading
Loading