Skip to content

[Messenger] Fix BC layer for stamps moved into separate packages #37826

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

Merged
merged 1 commit into from
Aug 17, 2020
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
17 changes: 14 additions & 3 deletions src/Symfony/Component/Messenger/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function withoutAll(string $stampFqcn): self
{
$cloned = clone $this;

unset($cloned->stamps[$stampFqcn]);
unset($cloned->stamps[$this->resolveAlias($stampFqcn)]);

return $cloned;
}
Expand All @@ -84,6 +84,7 @@ public function withoutAll(string $stampFqcn): self
public function withoutStampsOfType(string $type): self
{
$cloned = clone $this;
$type = $this->resolveAlias($type);

foreach ($cloned->stamps as $class => $stamps) {
if ($class === $type || is_subclass_of($class, $type)) {
Expand All @@ -96,7 +97,7 @@ public function withoutStampsOfType(string $type): self

public function last(string $stampFqcn): ?StampInterface
{
return isset($this->stamps[$stampFqcn]) ? end($this->stamps[$stampFqcn]) : null;
return isset($this->stamps[$stampFqcn = $this->resolveAlias($stampFqcn)]) ? end($this->stamps[$stampFqcn]) : null;
}

/**
Expand All @@ -105,7 +106,7 @@ public function last(string $stampFqcn): ?StampInterface
public function all(string $stampFqcn = null): array
{
if (null !== $stampFqcn) {
return $this->stamps[$stampFqcn] ?? [];
return $this->stamps[$this->resolveAlias($stampFqcn)] ?? [];
}

return $this->stamps;
Expand All @@ -118,4 +119,14 @@ public function getMessage(): object
{
return $this->message;
}

/**
* BC to be removed in 6.0.
*/
private function resolveAlias(string $fqcn): string
{
static $resolved;

return $resolved[$fqcn] ?? ($resolved[$fqcn] = (new \ReflectionClass($fqcn))->getName());
}
}
38 changes: 38 additions & 0 deletions src/Symfony/Component/Messenger/Tests/EnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
namespace Symfony\Component\Messenger\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineReceivedStamp;
use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisReceivedStamp;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Messenger\Stamp\ValidationStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Transport\Doctrine\DoctrineReceivedStamp as LegacyDoctrineReceivedStamp;
use Symfony\Component\Messenger\Transport\RedisExt\RedisReceivedStamp as LegacyRedisReceivedStamp;

/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
Expand Down Expand Up @@ -114,6 +118,40 @@ public function testWrapWithEnvelope()
$this->assertCount(1, $envelope->all(DelayStamp::class));
$this->assertCount(1, $envelope->all(ReceivedStamp::class));
}

/**
* To be removed in 6.0.
*
* @group legacy
*/
public function testWithAliases()
{
$envelope = new Envelope(new \stdClass(), [
$s1 = new DoctrineReceivedStamp(1),
$s2 = new RedisReceivedStamp(2),
$s3 = new DoctrineReceivedStamp(3),
]);

self::assertSame([
DoctrineReceivedStamp::class => [$s1, $s3],
RedisReceivedStamp::class => [$s2],
], $envelope->all());
Copy link
Contributor Author

@ogizanagi ogizanagi Aug 13, 2020

Choose a reason for hiding this comment

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

Unfortunately, I think we cannot do much for the case someone calls Envelope::all() with no type argument and still uses the alias instead of the new fqcn to do their things.


self::assertSame([$s1, $s3], $envelope->all(DoctrineReceivedStamp::class));
self::assertSame([$s2], $envelope->all(RedisReceivedStamp::class));

self::assertSame([$s1, $s3], $envelope->all(LegacyDoctrineReceivedStamp::class));
self::assertSame([$s2], $envelope->all(LegacyRedisReceivedStamp::class));

self::assertSame($s3, $envelope->last(LegacyDoctrineReceivedStamp::class));
self::assertSame($s2, $envelope->last(LegacyRedisReceivedStamp::class));

self::assertSame([RedisReceivedStamp::class => [$s2]], $envelope->withoutAll(LegacyDoctrineReceivedStamp::class)->all());
self::assertSame([DoctrineReceivedStamp::class => [$s1, $s3]], $envelope->withoutAll(LegacyRedisReceivedStamp::class)->all());

self::assertSame([RedisReceivedStamp::class => [$s2]], $envelope->withoutStampsOfType(LegacyDoctrineReceivedStamp::class)->all());
self::assertSame([DoctrineReceivedStamp::class => [$s1, $s3]], $envelope->withoutStampsOfType(LegacyRedisReceivedStamp::class)->all());
}
}

interface DummyFooBarStampInterface extends StampInterface
Expand Down