Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to find an envelope instance from the given stack #216

Closed
wants to merge 3 commits into from
Closed
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
13 changes: 13 additions & 0 deletions src/Message/EnvelopeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ public static function fromMessage(MessageInterface $message): self;
public function getMessage(): MessageInterface;

public function withMessage(MessageInterface $message): self;

/**
* Finds an envelope in the current envelope stack or creates a new one from the message.
*
* @template T of EnvelopeInterface
*
* @psalm-param class-string<T> $className
* @throws NotEnvelopInterfaceException Implementation MUST throw this exception if the given class does not
* implement {@see EnvelopeInterface}.
*
* @psalm-return T
*/
public function getEnvelopeFromStack(string $className): self;
}
36 changes: 36 additions & 0 deletions src/Message/EnvelopeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,42 @@
);
}

/**
* Finds an envelope in the current envelope stack or creates a new one from the message.
*
* @template T of EnvelopeInterface
*
* @psalm-param class-string<T> $className
* @throws NotEnvelopInterfaceException is thrown if the given class does not implement {@see EnvelopeInterface}.
*
* @psalm-return T

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidReturnType

src/Message/EnvelopeTrait.php:56:22: InvalidReturnType: The declared return type 'T:fn-yiisoft\queue\message\envelopetrait::getenvelopefromstack as Yiisoft\Queue\Message\EnvelopeInterface' for Yiisoft\Queue\Message\EnvelopeTrait::getEnvelopeFromStack is incorrect, got 'Yiisoft\Queue\Message\EnvelopeInterface' (see https://psalm.dev/011)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidReturnType

src/Message/EnvelopeTrait.php:56:22: InvalidReturnType: The declared return type 'T:fn-yiisoft\queue\message\envelopetrait::getenvelopefromstack as Yiisoft\Queue\Message\EnvelopeInterface' for Yiisoft\Queue\Message\EnvelopeTrait::getEnvelopeFromStack is incorrect, got 'Yiisoft\Queue\Message\EnvelopeInterface' (see https://psalm.dev/011)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

InvalidReturnType

src/Message/EnvelopeTrait.php:56:22: InvalidReturnType: The declared return type 'T:fn-yiisoft\queue\message\envelopetrait::getenvelopefromstack as Yiisoft\Queue\Message\EnvelopeInterface' for Yiisoft\Queue\Message\EnvelopeTrait::getEnvelopeFromStack is incorrect, got 'Yiisoft\Queue\Message\EnvelopeInterface' (see https://psalm.dev/011)
*/
public function getEnvelopeFromStack(string $className): EnvelopeInterface
{
if (!is_a($className, EnvelopeInterface::class, true)) {
throw new NotEnvelopInterfaceException($className);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new NotEnvelopInterfaceException($className);
throw new NotEnvelopeException($className);

}

if (static::class === $className) {
return $this;
}

if ($this->message instanceof EnvelopeInterface) {
return $this->message->getEnvelopeFromStack($className);
}

return $className::fromMessage($this->message);
}

public static function getEnvelopeFromMessage(MessageInterface $message): EnvelopeInterface
{
if ($message instanceof EnvelopeInterface) {
return $message->getEnvelopeFromStack(static::class);
}

return static::fromMessage($message);
}

public function getEnvelopeMetadata(): array
{
return [];
Expand Down
22 changes: 22 additions & 0 deletions src/Message/NotEnvelopInterfaceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Message;

use InvalidArgumentException;
use Throwable;

final class NotEnvelopInterfaceException extends InvalidArgumentException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final class NotEnvelopInterfaceException extends InvalidArgumentException
final class NotEnvelopeException extends InvalidArgumentException

{
public function __construct(string $className = '', int $code = 0, ?Throwable $previous = null)
{
$message = sprintf(
'The given class "%s" does not implement "%s".',
$className,
EnvelopeInterface::class
);

parent::__construct($message, $code, $previous);
}
}
65 changes: 65 additions & 0 deletions tests/Unit/Message/EnvelopeTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\Unit\Message;

use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Message\EnvelopeInterface;
use Yiisoft\Queue\Message\IdEnvelope;
use Yiisoft\Queue\Message\Message;
use Yiisoft\Queue\Message\NotEnvelopInterfaceException;
use Yiisoft\Queue\Middleware\FailureHandling\FailureEnvelope;

class EnvelopeTraitTest extends TestCase
{
public function testGetEnvelopeFromStack(): void
{
$message = new Message('handler', 'data');
$envelope = new FailureEnvelope(new IdEnvelope($message, 'id value'), ['fail' => true]);
$result = $envelope->getEnvelopeFromStack(IdEnvelope::class);

$this->assertInstanceOf(IdEnvelope::class, $result);
$this->assertEquals('id value', $result->getId());
}

public function testGetEnvelopeFromStackNotInStack(): void
{
$message = new Message('handler', 'data', [IdEnvelope::MESSAGE_ID_KEY => 'id value']);
$envelope = new FailureEnvelope($message, ['fail' => true]);
$result = $envelope->getEnvelopeFromStack(IdEnvelope::class);

$this->assertInstanceOf(IdEnvelope::class, $result);
$this->assertEquals('id value', $result->getId());
}

public function testGetEnvelopeFromStackNotEnvelope(): void
{
$this->expectException(NotEnvelopInterfaceException::class);
$interface = EnvelopeInterface::class;
$this->expectExceptionMessage("The given class \"foo\" does not implement \"$interface\".");

$message = new Message('handler', 'data', [IdEnvelope::MESSAGE_ID_KEY => 'id value']);
$envelope = new FailureEnvelope($message, ['fail' => true]);
$envelope->getEnvelopeFromStack('foo');
}

public function testGetEnvelopeFromMessage(): void
{
$message = new Message('handler', 'data');
$envelope = new FailureEnvelope(new IdEnvelope($message, 'id value'), ['fail' => true]);
$result = IdEnvelope::getEnvelopeFromMessage($envelope);

$this->assertInstanceOf(IdEnvelope::class, $result);
$this->assertEquals('id value', $result->getId());
}

public function testGetEnvelopeFromMessageNotInStack(): void
{
$message = new Message('handler', 'data', [IdEnvelope::MESSAGE_ID_KEY => 'id value']);
$result = IdEnvelope::getEnvelopeFromMessage($message);

$this->assertInstanceOf(IdEnvelope::class, $result);
$this->assertEquals('id value', $result->getId());
}
}
Loading