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 bb83076

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

File tree

Expand file treeCollapse file tree

2 files changed

+70
-10
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+70
-10
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
+52-8Lines changed: 52 additions & 8 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,24 +75,67 @@ 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+
87+
$symfonySerializer
88+
->expects($this->at(2))
89+
->method('serialize')->with(
90+
$message,
91+
'json',
92+
[
93+
ObjectNormalizer::GROUPS => ['foo']
94+
]
95+
)
8296
;
8397

8498
$encoded = $serializer->encode($envelope);
8599

86100
$this->assertArrayHasKey('body', $encoded);
87101
$this->assertArrayHasKey('headers', $encoded);
88102
$this->assertArrayHasKey('type', $encoded['headers']);
89-
$this->assertArrayHasKey('X-Message-Stamp-'.SerializerStamp::class, $encoded['headers']);
90-
$this->assertArrayHasKey('X-Message-Stamp-'.ValidationStamp::class, $encoded['headers']);
103+
$this->assertArrayHasKey('X-Message-Stamp-' . SerializerStamp::class, $encoded['headers']);
104+
$this->assertArrayHasKey('X-Message-Stamp-' . ValidationStamp::class, $encoded['headers']);
105+
}
91106

92-
$decoded = $serializer->decode($encoded);
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+
;
119+
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+
;
93132

94-
$this->assertEquals($serializerStamp, $decoded->last(SerializerStamp::class));
95-
$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+
]);
96140
}
97141
}

‎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 ($serializerStamp !== null) {
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.