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,11 +75,23 @@ 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
+ $ symfonySerializer
87
+ ->expects ($ this ->at (2 ))
88
+ ->method ('serialize ' )->with (
89
+ $ message ,
90
+ 'json ' ,
91
+ [
92
+ ObjectNormalizer::GROUPS => ['foo ' ],
93
+ ]
94
+ )
82
95
;
83
96
84
97
$ encoded = $ serializer ->encode ($ envelope );
@@ -88,10 +101,40 @@ public function testEncodedWithSymfonySerializerForStamps()
88
101
$ this ->assertArrayHasKey ('type ' , $ encoded ['headers ' ]);
89
102
$ this ->assertArrayHasKey ('X-Message-Stamp- ' .SerializerStamp::class, $ encoded ['headers ' ]);
90
103
$ this ->assertArrayHasKey ('X-Message-Stamp- ' .ValidationStamp::class, $ encoded ['headers ' ]);
104
+ }
91
105
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
+ ;
93
131
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
+ ]);
96
139
}
97
140
}
0 commit comments