1919use Symfony \Component \Messenger \Transport \Serialization \Serializer ;
2020use Symfony \Component \Serializer as SerializerComponent ;
2121use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
22+ use Symfony \Component \Serializer \SerializerInterface as SerializerComponentInterface ;
2223
2324class SerializerTest extends TestCase
2425{
@@ -74,11 +75,23 @@ public function testUsesTheCustomFormatAndContext()
7475
7576 public function testEncodedWithSymfonySerializerForStamps ()
7677 {
77- $ serializer = new Serializer ();
78+ $ serializer = new Serializer (
79+ $ symfonySerializer = $ this ->createMock (SerializerComponentInterface::class)
80+ );
7881
79- $ envelope = (new Envelope (new DummyMessage ('Hello ' )))
82+ $ envelope = (new Envelope ($ message = new DummyMessage ('test ' )))
8083 ->with ($ serializerStamp = new SerializerStamp ([ObjectNormalizer::GROUPS => ['foo ' ]]))
81- ->with ($ validationStamp = new ValidationStamp (['foo ' , 'bar ' ]))
84+ ->with ($ validationStamp = new ValidationStamp (['foo ' , 'bar ' ]));
85+
86+ $ symfonySerializer
87+ ->expects ($ this ->at (2 ))
88+ ->method ('serialize ' )->with (
89+ $ message ,
90+ 'json ' ,
91+ [
92+ ObjectNormalizer::GROUPS => ['foo ' ],
93+ ]
94+ )
8295 ;
8396
8497 $ encoded = $ serializer ->encode ($ envelope );
@@ -88,10 +101,40 @@ public function testEncodedWithSymfonySerializerForStamps()
88101 $ this ->assertArrayHasKey ('type ' , $ encoded ['headers ' ]);
89102 $ this ->assertArrayHasKey ('X-Message-Stamp- ' .SerializerStamp::class, $ encoded ['headers ' ]);
90103 $ this ->assertArrayHasKey ('X-Message-Stamp- ' .ValidationStamp::class, $ encoded ['headers ' ]);
104+ }
91105
92- $ decoded = $ serializer ->decode ($ encoded );
106+ public function testDecodeWithSymfonySerializerStamp ()
107+ {
108+ $ serializer = new Serializer (
109+ $ symfonySerializer = $ this ->createMock (SerializerComponentInterface::class)
110+ );
111+
112+ $ symfonySerializer
113+ ->expects ($ this ->at (0 ))
114+ ->method ('deserialize ' )
115+ ->with ('[{"context":{"groups":["foo"]}}] ' , SerializerStamp::class.'[] ' , 'json ' , [])
116+ ->willReturn ([new SerializerStamp (['groups ' => ['foo ' ]])])
117+ ;
118+
119+ $ symfonySerializer
120+ ->expects ($ this ->at (1 ))
121+ ->method ('deserialize ' )->with (
122+ '{} ' ,
123+ DummyMessage::class,
124+ 'json ' ,
125+ [
126+ ObjectNormalizer::GROUPS => ['foo ' ],
127+ ]
128+ )
129+ ->willReturn (new DummyMessage ('test ' ))
130+ ;
93131
94- $ this ->assertEquals ($ serializerStamp , $ decoded ->last (SerializerStamp::class));
95- $ this ->assertEquals ($ validationStamp , $ decoded ->last (ValidationStamp::class));
132+ $ serializer ->decode ([
133+ 'body ' => '{} ' ,
134+ 'headers ' => [
135+ 'type ' => DummyMessage::class,
136+ 'X-Message-Stamp- ' .SerializerStamp::class => '[{"context":{"groups":["foo"]}}] ' ,
137+ ],
138+ ]);
96139 }
97140}
0 commit comments