20
20
use Symfony \Component \Messenger \Transport \Serialization \Serializer ;
21
21
use Symfony \Component \Serializer as SerializerComponent ;
22
22
use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
23
+ use Symfony \Component \Serializer \SerializerInterface as SerializerComponentInterface ;
23
24
24
25
class SerializerTest extends TestCase
25
26
{
@@ -75,11 +76,23 @@ public function testUsesTheCustomFormatAndContext()
75
76
76
77
public function testEncodedWithSymfonySerializerForStamps ()
77
78
{
78
- $ serializer = new Serializer ();
79
+ $ serializer = new Serializer (
80
+ $ symfonySerializer = $ this ->createMock (SerializerComponentInterface::class)
81
+ );
79
82
80
- $ envelope = (new Envelope (new DummyMessage ('Hello ' )))
83
+ $ envelope = (new Envelope ($ message = new DummyMessage ('test ' )))
81
84
->with ($ serializerStamp = new SerializerStamp ([ObjectNormalizer::GROUPS => ['foo ' ]]))
82
- ->with ($ validationStamp = new ValidationStamp (['foo ' , 'bar ' ]))
85
+ ->with ($ validationStamp = new ValidationStamp (['foo ' , 'bar ' ]));
86
+
87
+ $ symfonySerializer
88
+ ->expects ($ this ->at (2 ))
89
+ ->method ('serialize ' )->with (
90
+ $ message ,
91
+ 'json ' ,
92
+ [
93
+ ObjectNormalizer::GROUPS => ['foo ' ],
94
+ ]
95
+ )
83
96
;
84
97
85
98
$ encoded = $ serializer ->encode ($ envelope );
@@ -89,11 +102,41 @@ public function testEncodedWithSymfonySerializerForStamps()
89
102
$ this ->assertArrayHasKey ('type ' , $ encoded ['headers ' ]);
90
103
$ this ->assertArrayHasKey ('X-Message-Stamp- ' .SerializerStamp::class, $ encoded ['headers ' ]);
91
104
$ this ->assertArrayHasKey ('X-Message-Stamp- ' .ValidationStamp::class, $ encoded ['headers ' ]);
105
+ }
106
+
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
+ ;
92
119
93
- $ decoded = $ serializer ->decode ($ encoded );
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
+ ;
94
132
95
- $ this ->assertEquals ($ serializerStamp , $ decoded ->last (SerializerStamp::class));
96
- $ 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
+ ]);
97
140
}
98
141
99
142
public function testDecodingFailsWithBadFormat ()
0 commit comments