Skip to content

Commit

Permalink
Changing to MessageDecodingFailedException so that invalid messages a…
Browse files Browse the repository at this point in the history
…re rejected
  • Loading branch information
weaverryan committed Mar 31, 2019
1 parent 88042a3 commit 4be827d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
Expand Up @@ -28,6 +28,16 @@ public function testEncodedIsDecodable()
$this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope)));
}

public function testDecodingFailsWithMissingBodyKey()
{
$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessage('Encoded envelope should have at least a "body".');

$serializer = new PhpSerializer();

$serializer->decode([]);
}

public function testDecodingFailsWithBadFormat()
{
$this->expectException(MessageDecodingFailedException::class);
Expand Down
Expand Up @@ -108,6 +108,37 @@ public function testDecodingFailsWithBadFormat()
]);
}

/**
* @dataProvider getMissingKeyTests
*/
public function testDecodingFailsWithMissingKeys(array $data, string $expectedMessage)
{
$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessage($expectedMessage);

$serializer = new Serializer();

$serializer->decode($data);
}

public function getMissingKeyTests()
{
yield 'no_body' => [
['headers' => ['type' => 'bar']],
'Encoded envelope should have at least a "body" and some "headers".',
];

yield 'no_headers' => [
['body' => '{}'],
'Encoded envelope should have at least a "body" and some "headers".',
];

yield 'no_headers_type' => [
['body' => '{}', 'headers' => ['foo' => 'bar']],
'Encoded envelope does not have a "type" header.',
];
}

public function testDecodingFailsWithBadClass()
{
$this->expectException(MessageDecodingFailedException::class);
Expand Down
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Messenger\Transport\Serialization;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;

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

return $this->safelyUnserialize($encodedEnvelope['body']);
Expand Down
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Messenger\Transport\Serialization;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Stamp\SerializerStamp;
Expand Down Expand Up @@ -63,11 +62,11 @@ public static function create(): self
public function decode(array $encodedEnvelope): Envelope
{
if (empty($encodedEnvelope['body']) || empty($encodedEnvelope['headers'])) {
throw new InvalidArgumentException('Encoded envelope should have at least a "body" and some "headers".');
throw new MessageDecodingFailedException('Encoded envelope should have at least a "body" and some "headers".');
}

if (empty($encodedEnvelope['headers']['type'])) {
throw new InvalidArgumentException('Encoded envelope does not have a "type" header.');
throw new MessageDecodingFailedException('Encoded envelope does not have a "type" header.');
}

$stamps = $this->decodeStamps($encodedEnvelope);
Expand Down

0 comments on commit 4be827d

Please sign in to comment.