2020use Symfony \Component \Messenger \Transport \Serialization \Serializer ;
2121use Symfony \Component \Serializer as SerializerComponent ;
2222use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
23+ use Symfony \Component \Serializer \SerializerInterface as SerializerComponentInterface ;
2324
2425class SerializerTest extends TestCase
2526{
@@ -75,11 +76,23 @@ public function testUsesTheCustomFormatAndContext()
7576
7677 public function testEncodedWithSymfonySerializerForStamps ()
7778 {
78- $ serializer = new Serializer ();
79+ $ serializer = new Serializer (
80+ $ symfonySerializer = $ this ->createMock (SerializerComponentInterface::class)
81+ );
7982
80- $ envelope = (new Envelope (new DummyMessage ('Hello ' )))
83+ $ envelope = (new Envelope ($ message = new DummyMessage ('test ' )))
8184 ->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+ )
8396 ;
8497
8598 $ encoded = $ serializer ->encode ($ envelope );
@@ -89,11 +102,41 @@ public function testEncodedWithSymfonySerializerForStamps()
89102 $ this ->assertArrayHasKey ('type ' , $ encoded ['headers ' ]);
90103 $ this ->assertArrayHasKey ('X-Message-Stamp- ' .SerializerStamp::class, $ encoded ['headers ' ]);
91104 $ 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+ ;
92119
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+ ;
94132
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+ ]);
97140 }
98141
99142 public function testDecodingFailsWithBadFormat ()
0 commit comments