10000 [Messenger] PhpSerializer: TypeError should throw MessageDecodingFail… · symfony/symfony@6acc9d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6acc9d8

Browse files
committed
[Messenger] PhpSerializer: TypeError should throw MessageDecodingFailedException
Actually, the fix should handle more cases than only TypeError.
1 parent a67e8bd commit 6acc9d8

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php

+28-15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
class PhpSerializerTest extends TestCase
2222
{
23-
public function testEncodedIsDecodable()
23+
public function testEncodedIsDecodable(): void
2424
{
2525
$serializer = new PhpSerializer();
2626

@@ -31,53 +31,53 @@ public function testEncodedIsDecodable()
3131
$this->assertEquals($envelope, $serializer->decode($encoded));
3232
}
3333

34-
public function testDecodingFailsWithMissingBodyKey()
34+
public function testDecodingFailsWithMissingBodyKey(): void
3535
{
36+
$serializer = new PhpSerializer();
37+
3638
$this->expectException(MessageDecodingFailedException::class);
3739
$this->expectExceptionMessage('Encoded envelope should have at least a "body", or maybe you should implement your own serializer');
3840

39-
$serializer = new PhpSerializer();
40-
4141
$serializer->decode([]);
4242
}
4343

44-
public function testDecodingFailsWithBadFormat()
44+
public function testDecodingFailsWithBadFormat(): void
4545
{
46+
$serializer = new PhpSerializer();
47+
4648
$this->expectException(MessageDecodingFailedException::class);
4749
$this->expectExceptionMessageMatches('/Could not decode/');
4850

49-
$serializer = new PhpSerializer();
50-
5151
$serializer->decode([
5252
'body' => '{"message": "bar"}',
5353
]);
5454
}
5555

56-
public function testDecodingFailsWithBadBase64Body()
56+
public function testDecodingFailsWithBadBase64Body(): void
5757
{
58+
$serializer = new PhpSerializer();
59+
5860
$this->expectException(MessageDecodingFailedException::class);
5961
$this->expectExceptionMessageMatches('/Could not decode/');
6062

61-
$serializer = new PhpSerializer();
62-
6363
$serializer->decode([
6464
'body' => 'x',
6565
]);
6666
}
6767

68-
public function testDecodingFailsWithBadClass()
68+
public function testDecodingFailsWithBadClass(): void
6969
{
70+
$serializer = new PhpSerializer();
71+
7072
$this->expectException(MessageDecodingFailedException::class);
7173
$this->expectExceptionMessageMatches('/class "ReceivedSt0mp" not found/');
7274

73-
$serializer = new PhpSerializer();
74-
7575
$serializer->decode([
7676
'body' => 'O:13:"ReceivedSt0mp":0:{}',
7777
]);
7878
}
7979

80-
public function testEncodedSkipsNonEncodeableStamps()
80+
public function testEncodedSkipsNonEncodeableStamps(): void
8181
{
8282
$serializer = new PhpSerializer();
8383

@@ -89,7 +89,7 @@ public function testEncodedSkipsNonEncodeableStamps()
8989
$this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']);
9090
}
9191

92-
public function testNonUtf8IsBase64Encoded()
92+
public function testNonUtf8IsBase64Encoded(): void
9393
{
9494
$serializer = new PhpSerializer();
9595

@@ -99,6 +99,19 @@ public function testNonUtf8IsBase64Encoded()
9999
$this->assertTrue((bool) preg_match('//u', $encoded['body']), 'Encodes non-UTF8 payloads');
100100
$this->assertEquals($envelope, $serializer->decode($encoded));
101101
}
102+
103+
public function testDecodingFailsForPropertyTypeMismatch(): void
104+
{
105+
$serializer = new PhpSerializer();
106+
$encodedEnvelope = $serializer->encode(new Envelope(new DummyMessage('true')));
107+
// Simulate a change of property type in the code base
108+
$encodedEnvelope['body'] = str_replace('s:4:\"true\"', 'b:1', $encodedEnvelope['body']);
109+
110+
$this->expectException(MessageDecodingFailedException::class);
111+
$this->expectExceptionMessageMatches('/Could not decode/');
112+
113+
$serializer->decode($encodedEnvelope);
114+
}
102115
}
103116

104117
class DummyPhpSerializerNonSendableStamp implements NonSendableStampInterface

src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,20 @@ private function safelyUnserialize(string $contents)
7373
});
7474

7575
try {
76-
$meta = unserialize($contents);
76+
/** @var Envelope */
77+
$envelope = unserialize($contents);
78+
} catch (\Throwable $e) {
79+
if ($e instanceof MessageDecodingFailedException) {
80+
throw $e;
81+
}
82+
83+
throw new MessageDecodingFailedException('Could not unserialize Envelope: '.$e->getMessage(), 0, $e);
7784
} finally {
7885
restore_error_handler();
7986
ini_set('unserialize_callback_func', $prevUnserializeHandler);
8087
}
8188

82-
return $meta;
89+
return $envelope;
8390
}
8491

8592
/**

0 commit comments

Comments
 (0)
0