16
16
use Symfony \Component \Messenger \Exception \MessageDecodingFailedException ;
17
17
use Symfony \Component \Messenger \Stamp \NonSendableStampInterface ;
18
18
use Symfony \Component \Messenger \Tests \Fixtures \DummyMessage ;
19
+ use Symfony \Component \Messenger \Tests \Fixtures \DummyMessageTyped ;
19
20
use Symfony \Component \Messenger \Transport \Serialization \PhpSerializer ;
20
21
21
22
class PhpSerializerTest extends TestCase
22
23
{
23
- public function testEncodedIsDecodable ()
24
+ public function testEncodedIsDecodable (): void
24
25
{
25
26
$ serializer = new PhpSerializer ();
26
27
@@ -31,53 +32,53 @@ public function testEncodedIsDecodable()
31
32
$ this ->assertEquals ($ envelope , $ serializer ->decode ($ encoded ));
32
33
}
33
34
34
- public function testDecodingFailsWithMissingBodyKey ()
35
+ public function testDecodingFailsWithMissingBodyKey (): void
35
36
{
37
+ $ serializer = new PhpSerializer ();
38
+
36
39
$ this ->expectException (MessageDecodingFailedException::class);
37
40
$ this ->expectExceptionMessage ('Encoded envelope should have at least a "body", or maybe you should implement your own serializer ' );
38
41
39
- $ serializer = new PhpSerializer ();
40
-
41
42
$ serializer ->decode ([]);
42
43
}
43
44
44
- public function testDecodingFailsWithBadFormat ()
45
+ public function testDecodingFailsWithBadFormat (): void
45
46
{
47
+ $ serializer = new PhpSerializer ();
48
+
46
49
$ this ->expectException (MessageDecodingFailedException::class);
47
50
$ this ->expectExceptionMessageMatches ('/Could not decode/ ' );
48
51
49
- $ serializer = new PhpSerializer ();
50
-
51
52
$ serializer ->decode ([
52
53
'body ' => '{"message": "bar"} ' ,
53
54
]);
54
55
}
55
56
56
- public function testDecodingFailsWithBadBase64Body ()
57
+ public function testDecodingFailsWithBadBase64Body (): void
57
58
{
59
+ $ serializer = new PhpSerializer ();
60
+
58
61
$ this ->expectException (MessageDecodingFailedException::class);
59
62
$ this ->expectExceptionMessageMatches ('/Could not decode/ ' );
60
63
61
- $ serializer = new PhpSerializer ();
62
-
63
64
$ serializer ->decode ([
64
65
'body ' => 'x ' ,
65
66
]);
66
67
}
67
68
68
- public function testDecodingFailsWithBadClass ()
69
+ public function testDecodingFailsWithBadClass (): void
69
70
{
71
+ $ serializer = new PhpSerializer ();
72
+
70
73
$ this ->expectException (MessageDecodingFailedException::class);
71
74
$ this ->expectExceptionMessageMatches ('/class "ReceivedSt0mp" not found/ ' );
72
75
73
- $ serializer = new PhpSerializer ();
74
-
75
76
$ serializer ->decode ([
76
77
'body ' => 'O:13:"ReceivedSt0mp":0:{} ' ,
77
78
]);
78
79
}
79
80
80
- public function testEncodedSkipsNonEncodeableStamps ()
81
+ public function testEncodedSkipsNonEncodeableStamps (): void
81
82
{
82
83
$ serializer = new PhpSerializer ();
83
84
@@ -89,7 +90,7 @@ public function testEncodedSkipsNonEncodeableStamps()
89
90
$ this ->assertStringNotContainsString ('DummyPhpSerializerNonSendableStamp ' , $ encoded ['body ' ]);
90
91
}
91
92
92
- public function testNonUtf8IsBase64Encoded ()
93
+ public function testNonUtf8IsBase64Encoded (): void
93
94
{
94
95
$ serializer = new PhpSerializer ();
95
96
@@ -99,6 +100,22 @@ public function testNonUtf8IsBase64Encoded()
99
100
$ this ->assertTrue ((bool ) preg_match ('//u ' , $ encoded ['body ' ]), 'Encodes non-UTF8 payloads ' );
100
101
$ this ->assertEquals ($ envelope , $ serializer ->decode ($ encoded ));
101
102
}
103
+
104
+ /**
105
+ * @requires PHP >= 7.4
106
+ */
107
+ public function testDecodingFailsForPropertyTypeMismatch (): void
108
+ {
109
+ $ serializer = new Ph
F438
pSerializer ();
110
+ $ encodedEnvelope = $ serializer ->encode (new Envelope (new DummyMessageTyped ('true ' )));
111
+ // Simulate a change of property type in the code base
112
+ $ encodedEnvelope ['body ' ] = str_replace ('s:4:\"true\" ' , 'b:1 ' , $ encodedEnvelope ['body ' ]);
113
+
114
+ $ this ->expectException (MessageDecodingFailedException::class);
115
+ $ this ->expectExceptionMessageMatches ('/Could not unserialize/ ' );
116
+
117
+ $ serializer ->decode ($ encodedEnvelope );
118
+ }
102
119
}
103
120
104
121
class DummyPhpSerializerNonSendableStamp implements NonSendableStampInterface
0 commit comments