-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from php-enqueue/kafka-serializer
[rdkafka] Add abilito change the way a message is serialized.
- Loading branch information
Showing
12 changed files
with
368 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Enqueue\RdKafka; | ||
|
||
class JsonSerializer implements Serializer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toString(RdKafkaMessage $message) | ||
{ | ||
$json = json_encode([ | ||
'body' => $message->getBody(), | ||
'properties' => $message->getProperties(), | ||
'headers' => $message->getHeaders(), | ||
]); | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The malformed json given. Error %s and message %s', | ||
json_last_error(), | ||
json_last_error_msg() | ||
)); | ||
} | ||
|
||
return $json; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toMessage($string) | ||
{ | ||
$data = json_decode($string, true); | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The malformed json given. Error %s and message %s', | ||
json_last_error(), | ||
json_last_error_msg() | ||
)); | ||
} | ||
|
||
return new RdKafkaMessage($data['body'], $data['properties'], $data['headers']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Enqueue\RdKafka; | ||
|
||
interface Serializer | ||
{ | ||
/** | ||
* @param RdKafkaMessage $message | ||
* | ||
* @return string | ||
*/ | ||
public function toString(RdKafkaMessage $message); | ||
|
||
/** | ||
* @param string $string | ||
* | ||
* @return RdKafkaMessage | ||
*/ | ||
public function toMessage($string); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Enqueue\RdKafka; | ||
|
||
trait SerializerAwareTrait | ||
{ | ||
/** | ||
* @var Serializer | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @param Serializer $serializer | ||
*/ | ||
public function setSerializer(Serializer $serializer) | ||
{ | ||
$this->serializer = $serializer; | ||
} | ||
|
||
/** | ||
* @return Serializer | ||
*/ | ||
public function getSerializer() | ||
{ | ||
return $this->serializer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Enqueue\RdKafka\Tests; | ||
|
||
use Enqueue\RdKafka\JsonSerializer; | ||
use Enqueue\RdKafka\RdKafkaMessage; | ||
use Enqueue\RdKafka\Serializer; | ||
use Enqueue\Test\ClassExtensionTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class JsonSerializerTest extends TestCase | ||
{ | ||
use ClassExtensionTrait; | ||
|
||
public function testShouldImplementSerializerInterface() | ||
{ | ||
$this->assertClassImplements(Serializer::class, JsonSerializer::class); | ||
} | ||
|
||
public function testCouldBeConstructedWithoutAnyArguments() | ||
{ | ||
new JsonSerializer(); | ||
} | ||
|
||
public function testShouldConvertMessageToJsonString() | ||
{ | ||
$serializer = new JsonSerializer(); | ||
|
||
$message = new RdKafkaMessage('theBody', ['aProp' => 'aPropVal'], ['aHeader' => 'aHeaderVal']); | ||
|
||
$json = $serializer->toString($message); | ||
|
||
$this->assertSame('{"body":"theBody","properties":{"aProp":"aPropVal"},"headers":{"aHeader":"aHeaderVal"}}', $json); | ||
} | ||
|
||
public function testThrowIfFailedToEncodeMessageToJson() | ||
{ | ||
$serializer = new JsonSerializer(); | ||
|
||
$resource = fopen(__FILE__, 'r'); | ||
|
||
//guard | ||
$this->assertInternalType('resource', $resource); | ||
|
||
$message = new RdKafkaMessage('theBody', ['aProp' => $resource]); | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('The malformed json given.'); | ||
$serializer->toString($message); | ||
} | ||
|
||
public function testShouldConvertJsonStringToMessage() | ||
{ | ||
$serializer = new JsonSerializer(); | ||
|
||
$message = $serializer->toMessage('{"body":"theBody","properties":{"aProp":"aPropVal"},"headers":{"aHeader":"aHeaderVal"}}'); | ||
|
||
$this->assertInstanceOf(RdKafkaMessage::class, $message); | ||
|
||
$this->assertSame('theBody', $message->getBody()); | ||
$this->assertSame(['aProp' => 'aPropVal'], $message->getProperties()); | ||
$this->assertSame(['aHeader' => 'aHeaderVal'], $message->getHeaders()); | ||
} | ||
|
||
public function testThrowIfFailedToDecodeJsonToMessage() | ||
{ | ||
$serializer = new JsonSerializer(); | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('The malformed json given.'); | ||
$serializer->toMessage('{]'); | ||
} | ||
} |
Oops, something went wrong.