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

This commit is contained in:
Maxime Steinhausser 2020-08-13 14:31:53 +02:00
parent 8ca1bac7d2
commit 118579ced7
2 changed files with 52 additions and 3 deletions

View File

@ -73,7 +73,7 @@ final class Envelope
{
$cloned = clone $this;
unset($cloned->stamps[$stampFqcn]);
unset($cloned->stamps[$this->resolveAlias($stampFqcn)]);
return $cloned;
}
@ -84,6 +84,7 @@ final class Envelope
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)) {
@ -96,7 +97,7 @@ final class Envelope
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;
}
/**
@ -105,7 +106,7 @@ final class Envelope
public function all(string $stampFqcn = null): array
{
if (null !== $stampFqcn) {
return $this->stamps[$stampFqcn] ?? [];
return $this->stamps[$this->resolveAlias($stampFqcn)] ?? [];
}
return $this->stamps;
@ -118,4 +119,14 @@ final class Envelope
{
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());
}
}

View File

@ -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>
@ -114,6 +118,40 @@ class EnvelopeTest extends TestCase
$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());
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