8000 Changing to MessageDecodingFailedException so that invalid messages a… · symfony/symfony@4be827d · GitHub
[go: up one dir, main page]

Skip to content

Commit 4be827d

Browse files
committed
Changing to MessageDecodingFailedException so that invalid messages are rejected
1 parent 88042a3 commit 4be827d

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public function testEncodedIsDecodable()
2828
$this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope)));
2929
}
3030

31+
public function testDecodingFailsWithMissingBodyKey()
32+
{
33+
$this->expectException(MessageDecodingFailedException::class);
34+
$this->expectExceptionMessage('Encoded envelope should have at least a "body".');
35+
36+
$serializer = new PhpSerializer();
37+
38+
$serializer->decode([]);
39+
}
40+
3141
public function testDecodingFailsWithBadFormat()
3242
{
3343
$this->expectException(MessageDecodingFailedException::class);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,37 @@ public function testDecodingFailsWithBadFormat()
108108
]);
109109
}
110110

111+
/**
112+
* @dataProvider getMissingKeyTests
113+
*/
114+
public function testDecodingFailsWithMissingKeys(array $data, string $expectedMessage)
115+
{
116+
$this->expectException(MessageDecodingFailedException::class);
117+
$this->expectExceptionMessage($expectedMessage);
118+
119+
$serializer = new Serializer();
120+
121+
$serializer->decode($data);
122+
}
123+
124+
public function getMissingKeyTests()
125+
{
126+
yield 'no_body' => [
127+
['headers' => ['type' => 'bar']],
128+
'Encoded envelope should have at least a "body" and some "headers".',
129+
];
130+
131+
yield 'no_headers' => [
132+
['body' => '{}'],
133+
'Encoded envelope should have at least a "body" and some "headers".',
134+
];
135+
136+
yield 'no_headers_type' => [
137+
['body' => '{}', 'headers' => ['foo' => 'bar']],
138+
'Encoded envelope does not have a "type" header.',
139+
];
140+
}
141+
111142
public function testDecodingFailsWithBadClass()
112143
{
113144
$this->expectException(MessageDecodingFailedException::class);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Messenger\Transport\Serialization;
1313

1414
use Symfony\Component\Messenger\Envelope;
15-
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
1615
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
1716

1817
/**
@@ -28,7 +27,7 @@ class PhpSerializer implements SerializerInterface
2827
public function decode(array $encodedEnvelope): Envelope
2928
{
3029
if (empty($encodedEnvelope['body'])) {
31-
throw new InvalidArgumentException('Encoded envelope should have at least a "body".');
30+
throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".');
3231
}
3332

3433
return $this->safelyUnserialize($encodedEnvelope['body']);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Messenger\Transport\Serialization;
1313

1414
use Symfony\Component\Messenger\Envelope;
15-
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
1615
use Symfony\Component\Messenger\Exception\LogicException;
1716
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
1817
use Symfony\Component\Messenger\Stamp\SerializerStamp;
@@ -63,11 +62,11 @@ public static function create(): self
6362
public function decode(array $encodedEnvelope): Envelope
6463
{
6564
if (empty($encodedEnvelope['body']) || empty($encodedEnvelope['headers'])) {
66-
throw new InvalidArgumentException('Encoded envelope should have at least a "body" and some "headers".');
65+
throw new MessageDecodingFailedException('Encoded envelope should have at least a "body" and some "headers".');
6766
}
6867

6968
if (empty($encodedEnvelope['headers']['type'])) {
70-
throw new InvalidArgumentException('Encoded envelope does not have a "type" header.');
69+
throw new MessageDecodingFailedException('Encoded envelope does not have a "type" header.');
7170
}
7271

7372
$stamps = $this->decodeStamps($encodedEnvelope);

0 commit comments

Comments
 (0)
0