Skip to content

Commit c187370

Browse files
committed
Merge branch '4.2'
* 4.2: Restore previous state for libxml option fix tests Uses the SerializerStamp when deserializing the envelope
2 parents fbdcf7d + 22c6bcd commit c187370

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

Tests/Transport/Serialization/SerializerTest.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
2121
use Symfony\Component\Serializer as SerializerComponent;
2222
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
23+
use Symfony\Component\Serializer\SerializerInterface as SerializerComponentInterface;
2324

2425
class SerializerTest extends TestCase
2526
{
@@ -75,11 +76,23 @@ public function testUsesTheCustomFormatAndContext()
7576

7677
public function testEncodedWithSymfonySerializerForStamps()
7778
{
78-
$serializer = new Serializer();
79+
$serializer = new Serializer(
80+
$symfonySerializer = $this->createMock(SerializerComponentInterface::class)
81+
);
7982

80-
$envelope = (new Envelope(new DummyMessage('Hello')))
83+
$envelope = (new Envelope($message = new DummyMessage('test')))
8184
->with($serializerStamp = new SerializerStamp([ObjectNormalizer::GROUPS => ['foo']]))
82-
->with($validationStamp = new ValidationStamp(['foo', 'bar']))
85+
->with($validationStamp = new ValidationStamp(['foo', 'bar']));
86+
87+
$symfonySerializer
88+
->expects($this->at(2))
89+
->method('serialize')->with(
90+
$message,
91+
'json',
92+
[
93+
ObjectNormalizer::GROUPS => ['foo'],
94+
]
95+
)
8396
;
8497

8598
$encoded = $serializer->encode($envelope);
@@ -89,11 +102,41 @@ public function testEncodedWithSymfonySerializerForStamps()
89102
$this->assertArrayHasKey('type', $encoded['headers']);
90103
$this->assertArrayHasKey('X-Message-Stamp-'.SerializerStamp::class, $encoded['headers']);
91104
$this->assertArrayHasKey('X-Message-Stamp-'.ValidationStamp::class, $encoded['headers']);
105+
}
106+
107+
public function testDecodeWithSymfonySerializerStamp()
108+
{
109+
$serializer = new Serializer(
110+
$symfonySerializer = $this->createMock(SerializerComponentInterface::class)
111+
);
112+
113+
$symfonySerializer
114+
->expects($this->at(0))
115+
->method('deserialize')
116+
->with('[{"context":{"groups":["foo"]}}]', SerializerStamp::class.'[]', 'json', [])
117+
->willReturn([new SerializerStamp(['groups' => ['foo']])])
118+
;
92119

93-
$decoded = $serializer->decode($encoded);
120+
$symfonySerializer
121+
->expects($this->at(1))
122+
->method('deserialize')->with(
123+
'{}',
124+
DummyMessage::class,
125+
'json',
126+
[
127+
ObjectNormalizer::GROUPS => ['foo'],
128+
]
129+
)
130+
->willReturn(new DummyMessage('test'))
131+
;
94132

95-
$this->assertEquals($serializerStamp, $decoded->last(SerializerStamp::class));
96-
$this->assertEquals($validationStamp, $decoded->last(ValidationStamp::class));
133+
$serializer->decode([
134+
'body' => '{}',
135+
'headers' => [
136+
'type' => DummyMessage::class,
137+
'X-Message-Stamp-'.SerializerStamp::class => '[{"context":{"groups":["foo"]}}]',
138+
],
139+
]);
97140
}
98141

99142
public function testDecodingFailsWithBadFormat()

Transport/Serialization/Serializer.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Messenger\Exception\LogicException;
1616
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
1717
use Symfony\Component\Messenger\Stamp\SerializerStamp;
18+
use Symfony\Component\Messenger\Stamp\StampInterface;
1819
use Symfony\Component\Serializer\Encoder\JsonEncoder;
1920
use Symfony\Component\Serializer\Encoder\XmlEncoder;
2021
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
@@ -70,10 +71,11 @@ public function decode(array $encodedEnvelope): Envelope
7071
}
7172

7273
$stamps = $this->decodeStamps($encodedEnvelope);
74+
$serializerStamp = $this->findFirstSerializerStamp($stamps);
7375

7476
$context = $this->context;
75-
if (isset($stamps[SerializerStamp::class])) {
76-
$context = end($stamps[SerializerStamp::class])->getContext() + $context;
77+
if (null !== $serializerStamp) {
78+
$context = $serializerStamp->getContext() + $context;
7779
}
7880

7981
try {
@@ -138,4 +140,18 @@ private function encodeStamps(Envelope $envelope): array
138140

139141
return $headers;
140142
}
143+
144+
/**
145+
* @param StampInterface[] $stamps
146+
*/
147+
private function findFirstSerializerStamp(array $stamps): ?SerializerStamp
148+
{
149+
foreach ($stamps as $stamp) {
150+
if ($stamp instanceof SerializerStamp) {
151+
return $stamp;
152+
}
153+
}
154+
155+
return null;
156+
}
141157
}

0 commit comments

Comments
 (0)