diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php index d68103f7ca682..34c0e579f82b8 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php @@ -19,6 +19,7 @@ use Symfony\Component\Messenger\Transport\Serialization\Serializer; use Symfony\Component\Serializer as SerializerComponent; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; +use Symfony\Component\Serializer\SerializerInterface as SerializerComponentInterface; class SerializerTest extends TestCase { @@ -74,11 +75,23 @@ public function testUsesTheCustomFormatAndContext() public function testEncodedWithSymfonySerializerForStamps() { - $serializer = new Serializer(); + $serializer = new Serializer( + $symfonySerializer = $this->createMock(SerializerComponentInterface::class) + ); - $envelope = (new Envelope(new DummyMessage('Hello'))) + $envelope = (new Envelope($message = new DummyMessage('test'))) ->with($serializerStamp = new SerializerStamp([ObjectNormalizer::GROUPS => ['foo']])) - ->with($validationStamp = new ValidationStamp(['foo', 'bar'])) + ->with($validationStamp = new ValidationStamp(['foo', 'bar'])); + + $symfonySerializer + ->expects($this->at(2)) + ->method('serialize')->with( + $message, + 'json', + [ + ObjectNormalizer::GROUPS => ['foo'], + ] + ) ; $encoded = $serializer->encode($envelope); @@ -88,10 +101,40 @@ public function testEncodedWithSymfonySerializerForStamps() $this->assertArrayHasKey('type', $encoded['headers']); $this->assertArrayHasKey('X-Message-Stamp-'.SerializerStamp::class, $encoded['headers']); $this->assertArrayHasKey('X-Message-Stamp-'.ValidationStamp::class, $encoded['headers']); + } - $decoded = $serializer->decode($encoded); + public function testDecodeWithSymfonySerializerStamp() + { + $serializer = new Serializer( + $symfonySerializer = $this->createMock(SerializerComponentInterface::class) + ); + + $symfonySerializer + ->expects($this->at(0)) + ->method('deserialize') + ->with('[{"context":{"groups":["foo"]}}]', SerializerStamp::class.'[]', 'json', []) + ->willReturn([new SerializerStamp(['groups' => ['foo']])]) + ; + + $symfonySerializer + ->expects($this->at(1)) + ->method('deserialize')->with( + '{}', + DummyMessage::class, + 'json', + [ + ObjectNormalizer::GROUPS => ['foo'], + ] + ) + ->willReturn(new DummyMessage('test')) + ; - $this->assertEquals($serializerStamp, $decoded->last(SerializerStamp::class)); - $this->assertEquals($validationStamp, $decoded->last(ValidationStamp::class)); + $serializer->decode([ + 'body' => '{}', + 'headers' => [ + 'type' => DummyMessage::class, + 'X-Message-Stamp-'.SerializerStamp::class => '[{"context":{"groups":["foo"]}}]', + ], + ]); } } diff --git a/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php b/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php index 3f0979bcef1d3..cc8ac58bb2ddc 100644 --- a/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php +++ b/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php @@ -15,6 +15,7 @@ use Symfony\Component\Messenger\Exception\InvalidArgumentException; use Symfony\Component\Messenger\Exception\LogicException; use Symfony\Component\Messenger\Stamp\SerializerStamp; +use Symfony\Component\Messenger\Stamp\StampInterface; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Encoder\XmlEncoder; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; @@ -69,10 +70,11 @@ public function decode(array $encodedEnvelope): Envelope } $stamps = $this->decodeStamps($encodedEnvelope); + $serializerStamp = $this->findFirstSerializerStamp($stamps); $context = $this->context; - if (isset($stamps[SerializerStamp::class])) { - $context = end($stamps[SerializerStamp::class])->getContext() + $context; + if (null !== $serializerStamp) { + $context = $serializerStamp->getContext() + $context; } $message = $this->serializer->deserialize($encodedEnvelope['body'], $encodedEnvelope['headers']['type'], $this->format, $context); @@ -129,4 +131,18 @@ private function encodeStamps(Envelope $envelope): array return $headers; } + + /** + * @param StampInterface[] $stamps + */ + private function findFirstSerializerStamp(array $stamps): ?SerializerStamp + { + foreach ($stamps as $stamp) { + if ($stamp instanceof SerializerStamp) { + return $stamp; + } + } + + return null; + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy