Uses the SerializerStamp when deserializing the envelope
This commit is contained in:
parent
1628995268
commit
ab55e8e7ef
@ -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"]}}]',
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user