Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ab55e8e

Browse filesBrowse files
committed
Uses the SerializerStamp when deserializing the envelope
1 parent 1628995 commit ab55e8e
Copy full SHA for ab55e8e

File tree

2 files changed

+67
-8
lines changed
Filter options

2 files changed

+67
-8
lines changed

‎src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php
+49-6Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
2020
use Symfony\Component\Serializer as SerializerComponent;
2121
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
22+
use Symfony\Component\Serializer\SerializerInterface as SerializerComponentInterface;
2223

2324
class SerializerTest extends TestCase
2425
{
@@ -74,11 +75,23 @@ public function testUsesTheCustomFormatAndContext()
7475

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

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

8497
$encoded = $serializer->encode($envelope);
@@ -88,10 +101,40 @@ public function testEncodedWithSymfonySerializerForStamps()
88101
$this->assertArrayHasKey('type', $encoded['headers']);
89102
$this->assertArrayHasKey('X-Message-Stamp-'.SerializerStamp::class, $encoded['headers']);
90103
$this->assertArrayHasKey('X-Message-Stamp-'.ValidationStamp::class, $encoded['headers']);
104+
}
91105

92-
$decoded = $serializer->decode($encoded);
106+
public function testDecodeWithSymfonySerializerStamp()
107+
{
108+
$serializer = new Serializer(
109+
$symfonySerializer = $this->createMock(SerializerComponentInterface::class)
110+
);
111+
112+
$symfonySerializer
113+
->expects($this->at(0))
114+
->method('deserialize')
115+
->with('[{"context":{"groups":["foo"]}}]', SerializerStamp::class.'[]', 'json', [])
116+
->willReturn([new SerializerStamp(['groups' => ['foo']])])
117+
;
118+
119+
$symfonySerializer
120+
->expects($this->at(1))
121+
->method('deserialize')->with(
122+
'{}',
123+
DummyMessage::class,
124+
'json',
125+
[
126+
ObjectNormalizer::GROUPS => ['foo'],
127+
]
128+
)
129+
->willReturn(new DummyMessage('test'))
130+
;
93131

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

‎src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php
+18-2Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
1616
use Symfony\Component\Messenger\Exception\LogicException;
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\Normalizer\ArrayDenormalizer;
@@ -69,10 +70,11 @@ public function decode(array $encodedEnvelope): Envelope
6970
}
7071

7172
$stamps = $this->decodeStamps($encodedEnvelope);
73+
$serializerStamp = $this->findFirstSerializerStamp($stamps);
7274

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

7880
$message = $this->serializer->deserialize($encodedEnvelope['body'], $encodedEnvelope['headers']['type'], $this->format, $context);
@@ -129,4 +131,18 @@ private function encodeStamps(Envelope $envelope): array
129131

130132
return $headers;
131133
}
134+
135+
/**
136+
* @param StampInterface[] $stamps
137+
*/
138+
private function findFirstSerializerStamp(array $stamps): ?SerializerStamp
139+
{
140+
foreach ($stamps as $stamp) {
141+
if ($stamp instanceof SerializerStamp) {
142+
return $stamp;
143+
}
144+
}
145+
146+
return null;
147+
}
132148
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.