|
19 | 19 | use Symfony\Component\Messenger\Transport\Serialization\Serializer;
|
20 | 20 | use Symfony\Component\Serializer as SerializerComponent;
|
21 | 21 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
| 22 | +use Symfony\Component\Serializer\SerializerInterface as SerializerComponentInterface; |
22 | 23 |
|
23 | 24 | class SerializerTest extends TestCase
|
24 | 25 | {
|
@@ -74,24 +75,67 @@ public function testUsesTheCustomFormatAndContext()
|
74 | 75 |
|
75 | 76 | public function testEncodedWithSymfonySerializerForStamps()
|
76 | 77 | {
|
77 |
| - $serializer = new Serializer(); |
| 78 | + $serializer = new Serializer( |
| 79 | + $symfonySerializer = $this->createMock(SerializerComponentInterface::class) |
| 80 | + ); |
78 | 81 |
|
79 |
| - $envelope = (new Envelope(new DummyMessage('Hello'))) |
| 82 | + $envelope = (new Envelope($message = new DummyMessage('test'))) |
80 | 83 | ->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 | + ) |
82 | 96 | ;
|
83 | 97 |
|
84 | 98 | $encoded = $serializer->encode($envelope);
|
85 | 99 |
|
86 | 100 | $this->assertArrayHasKey('body', $encoded);
|
87 | 101 | $this->assertArrayHasKey('headers', $encoded);
|
88 | 102 | $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 | + } |
91 | 106 |
|
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 | + ; |
93 | 132 |
|
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 | + ]); |
96 | 140 | }
|
97 | 141 | }
|
0 commit comments