Skip to content

Commit

Permalink
Add envelope stack
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Jan 13, 2024
1 parent 8498f59 commit cc15b35
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Message/EnvelopeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
interface EnvelopeInterface extends MessageInterface
{
public const ENVELOPE_STACK_KEY = 'envelopes';

public function getMessage(): MessageInterface;

public function withMessage(MessageInterface $message): self;
Expand Down
13 changes: 12 additions & 1 deletion src/Message/EnvelopeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public function getData(): mixed

public function getMetadata(): array
{
return $this->message->getMetadata();
return array_merge($this->message->getMetadata(), [
self::ENVELOPE_STACK_KEY => array_merge(
$this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [],
[self::class],
),
...$this->getEnvelopeMetadata(),

Check failure on line 41 in src/Message/EnvelopeTrait.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

DuplicateArrayKey

src/Message/EnvelopeTrait.php:41:16: DuplicateArrayKey: String keys are not supported in unpacked arrays (see https://psalm.dev/151)
]);
}

public function getEnvelopeMetadata(): array
{
return [];
}
}
6 changes: 2 additions & 4 deletions src/Message/IdEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ public function getId(): string|int|null
return $this->id ?? $this->message->getMetadata()[self::MESSAGE_ID_KEY] ?? null;
}

public function getMetadata(): array
public function getEnvelopeMetadata(): array
{
return array_merge($this->message->getMetadata(), [
self::MESSAGE_ID_KEY => $this->getId(),
]);
return [self::MESSAGE_ID_KEY => $this->getId()];
}
}
47 changes: 47 additions & 0 deletions tests/Unit/EnvelopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Unit;

use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Message\EnvelopeInterface;
use Yiisoft\Queue\Message\IdEnvelope;
use Yiisoft\Queue\Message\Message;

final class EnvelopeTest extends TestCase
{
public function testEnvelopeStack(): void
{
$message = new Message('handler', 'test');
$message = new IdEnvelope($message, 'test-id');

$this->assertEquals('test', $message->getMessage()->getData());

$stack = $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY];
$this->assertIsArray($stack);

$this->assertEquals([
IdEnvelope::class,
], $stack);
}

public function testEnvelopeDuplicates(): void
{
$message = new Message('handler', 'test');
$message = new IdEnvelope($message, 'test-id');
$message = new IdEnvelope($message, 'test-id');
$message = new IdEnvelope($message, 'test-id');

$this->assertEquals('test', $message->getMessage()->getData());

$stack = $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY];
$this->assertIsArray($stack);

$this->assertEquals([
IdEnvelope::class,
IdEnvelope::class,
IdEnvelope::class,
], $stack);
}
}

0 comments on commit cc15b35

Please sign in to comment.