Skip to content

Commit

Permalink
JsonProducer should check for errors
Browse files Browse the repository at this point in the history
resolves #46
  • Loading branch information
prolic committed Jul 7, 2017
1 parent bc002f4 commit 484d4a1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/JsonProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

namespace Humus\Amqp;

use Humus\Amqp\Exception\InvalidArgumentException;

/**
* Class JsonProducer
* @package Humus\Amqp
Expand All @@ -48,6 +50,16 @@ public function publish(
) {
$attributes = array_merge_recursive($this->defaultAttributes, $attributes);

$this->exchange->publish(json_encode($message), $routingKey, $flags, $attributes);
try {
$message = json_encode($message);
} catch (\Throwable $e) {
throw new InvalidArgumentException('Exception during json encoding', 0, $e);
}

if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidArgumentException('Error during json encoding');
}

$this->exchange->publish($message, $routingKey, $flags, $attributes);
}
}
43 changes: 43 additions & 0 deletions tests/AbstractJsonProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

use Humus\Amqp\Channel;
use Humus\Amqp\Envelope;
use Humus\Amqp\Exception\InvalidArgumentException;
use Humus\Amqp\Exchange;
use Humus\Amqp\Queue;
use Humus\Amqp\Constants;
use Humus\Amqp\JsonProducer;
use HumusTest\Amqp\Helper\CanCreateConnection;
use HumusTest\Amqp\Helper\DeleteOnTearDownTrait;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;

/**
* Class AbstractJsonProducerTest
Expand Down Expand Up @@ -399,4 +401,45 @@ public function it_uses_return_callback()
$this->assertInstanceOf(Envelope::class, $result[1][4]);
$this->assertEquals(['foo' => 'bar'], json_decode($result[1][5], true));
}

/**
* @test
*/
public function it_throws_exception_when_data_could_not_be_encoded_to_json()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Error during json encoding');

$exchange = $this->prophesize(Exchange::class);
$exchange->publish(Argument::any(), Argument::any(), Argument::any(), Argument::any())->shouldNotBeCalled();

$producer = new JsonProducer($exchange->reveal());

$text = "\xB1\x31";

$producer->publish($text);
}

/**
* @test
*/
public function it_throws_exception_when_data_could_not_be_encoded_to_json_2()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Exception during json encoding');

$exchange = $this->prophesize(Exchange::class);
$exchange->publish(Argument::any(), Argument::any(), Argument::any(), Argument::any())->shouldNotBeCalled();

$producer = new JsonProducer($exchange->reveal());

$message = new class() implements \JsonSerializable {
public function jsonSerialize()
{
throw new \Exception('foo');
}
};

$producer->publish($message);
}
}

0 comments on commit 484d4a1

Please sign in to comment.