bug #30903 [Messenger] Uses the SerializerStamp when deserializing the envelope (sroze)

This PR was merged into the 4.2 branch.

Discussion
----------

[Messenger] Uses the `SerializerStamp` when deserializing the envelope

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29970
| License       | MIT
| Doc PR        | ø

Uses the `SerializerStamp` when decoding the message.

Commits
-------

ab55e8e7ef Uses the SerializerStamp when deserializing the envelope
This commit is contained in:
Samuel ROZE 2019-04-06 17:25:35 +02:00
commit f88a79b550
2 changed files with 67 additions and 8 deletions

View File

@ -19,6 +19,7 @@ use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
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 @@ class SerializerTest extends TestCase
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 @@ class SerializerTest extends TestCase
$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)
);
$this->assertEquals($serializerStamp, $decoded->last(SerializerStamp::class));
$this->assertEquals($validationStamp, $decoded->last(ValidationStamp::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'))
;
$serializer->decode([
'body' => '{}',
'headers' => [
'type' => DummyMessage::class,
'X-Message-Stamp-'.SerializerStamp::class => '[{"context":{"groups":["foo"]}}]',
],
]);
}
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\Messenger\Envelope;
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 @@ class Serializer implements SerializerInterface
}
$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 @@ class Serializer implements SerializerInterface
return $headers;
}
/**
* @param StampInterface[] $stamps
*/
private function findFirstSerializerStamp(array $stamps): ?SerializerStamp
{
foreach ($stamps as $stamp) {
if ($stamp instanceof SerializerStamp) {
return $stamp;
}
}
return null;
}
}