Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 118579c

Browse files
committed
[Messenger] Fix BC layer for stamps moved into separate packages
1 parent 8ca1bac commit 118579c

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/Symfony/Component/Messenger/Envelope.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function withoutAll(string $stampFqcn): self
7373
{
7474
$cloned = clone $this;
7575

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

7878
return $cloned;
7979
}
@@ -84,6 +84,7 @@ public function withoutAll(string $stampFqcn): self
8484
public function withoutStampsOfType(string $type): self
8585
{
8686
$cloned = clone $this;
87+
$type = $this->resolveAlias($type);
8788

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

9798
public function last(string $stampFqcn): ?StampInterface
9899
{
99-
return isset($this->stamps[$stampFqcn]) ? end($this->stamps[$stampFqcn]) : null;
100+
return isset($this->stamps[$stampFqcn = $this->resolveAlias($stampFqcn)]) ? end($this->stamps[$stampFqcn]) : null;
100101
}
101102

102103
/**
@@ -105,7 +106,7 @@ public function last(string $stampFqcn): ?StampInterface
105106
public function all(string $stampFqcn = null): array
106107
{
107108
if (null !== $stampFqcn) {
108-
return $this->stamps[$stampFqcn] ?? [];
109+
return $this->stamps[$this->resolveAlias($stampFqcn)] ?? [];
109110
}
110111

111112
return $this->stamps;
@@ -118,4 +119,14 @@ public function getMessage(): object
118119
{
119120
return $this->message;
120121
}
122+
123+
/**
124+
* BC to be removed in 6.0.
125+
*/
126+
private function resolveAlias(string $fqcn): string
127+
{
128+
static $resolved;
129+
130+
return $resolved[$fqcn] ?? ($resolved[$fqcn] = (new \ReflectionClass($fqcn))->getName());
131+
}
121132
}

src/Symfony/Component/Messenger/Tests/EnvelopeTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
namespace Symfony\Component\Messenger\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineReceivedStamp;
16+
use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisReceivedStamp;
1517
use Symfony\Component\Messenger\Envelope;
1618
use Symfony\Component\Messenger\Stamp\DelayStamp;
1719
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
1820
use Symfony\Component\Messenger\Stamp\StampInterface;
1921
use Symfony\Component\Messenger\Stamp\ValidationStamp;
2022
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
23+
use Symfony\Component\Messenger\Transport\Doctrine\DoctrineReceivedStamp as LegacyDoctrineReceivedStamp;
24+
use Symfony\Component\Messenger\Transport\RedisExt\RedisReceivedStamp as LegacyRedisReceivedStamp;
2125

2226
/**
2327
* @author Maxime Steinhausser <[email protected]>
@@ -114,6 +118,40 @@ public function testWrapWithEnvelope()
114118
$this->assertCount(1, $envelope->all(DelayStamp::class));
115119
$this->assertCount(1, $envelope->all(ReceivedStamp::class));
116120
}
121+
122+
/**
123+
* To be removed in 6.0.
124+
*
125+
* @group legacy
126+
*/
127+
public function testWithAliases()
128+
{
129+
$envelope = new Envelope(new \stdClass(), [
130+
$s1 = new DoctrineReceivedStamp(1),
131+
$s2 = new RedisReceivedStamp(2),
132+
$s3 = new DoctrineReceivedStamp(3),
133+
]);
134+
135+
self::assertSame([
136+
DoctrineReceivedStamp::class => [$s1, $s3],
137+
RedisReceivedStamp::class => [$s2],
138+
], $envelope->all());
139+
140+
self::assertSame([$s1, $s3], $envelope->all(DoctrineReceivedStamp::class));
141+
self::assertSame([$s2], $envelope->all(RedisReceivedStamp::class));
142+
143+
self::assertSame([$s1, $s3], $envelope->all(LegacyDoctrineReceivedStamp::class));
144+
self::assertSame([$s2], $envelope->all(LegacyRedisReceivedStamp::class));
145+
146+
self::assertSame($s3, $envelope->last(LegacyDoctrineReceivedStamp::class));
147+
self::assertSame($s2, $envelope->last(LegacyRedisReceivedStamp::class));
148+
149+
self::assertSame([RedisReceivedStamp::class => [$s2]], $envelope->withoutAll(LegacyDoctrineReceivedStamp::class)->all());
150+
self::assertSame([DoctrineReceivedStamp::class => [$s1, $s3]], $envelope->withoutAll(LegacyRedisReceivedStamp::class)->all());
151+
152+
self::assertSame([RedisReceivedStamp::class => [$s2]], $envelope->withoutStampsOfType(LegacyDoctrineReceivedStamp::class)->all());
153+
self::assertSame([DoctrineReceivedStamp::class => [$s1, $s3]], $envelope->withoutStampsOfType(LegacyRedisReceivedStamp::class)->all());
154+
}
117155
}
118156

119157
interface DummyFooBarStampInterface extends StampInterface

0 commit comments

Comments
 (0)