|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\RdKafka\Tests; |
| 4 | + |
| 5 | +use Enqueue\RdKafka\JsonSerializer; |
| 6 | +use Enqueue\RdKafka\RdKafkaMessage; |
| 7 | +use Enqueue\RdKafka\Serializer; |
| 8 | +use Enqueue\Test\ClassExtensionTrait; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class JsonSerializerTest extends TestCase |
| 12 | +{ |
| 13 | + use ClassExtensionTrait; |
| 14 | + |
| 15 | + public function testShouldImplementSerializerInterface() |
| 16 | + { |
| 17 | + $this->assertClassImplements(Serializer::class, JsonSerializer::class); |
| 18 | + } |
| 19 | + |
| 20 | + public function testCouldBeConstructedWithoutAnyArguments() |
| 21 | + { |
| 22 | + new JsonSerializer(); |
| 23 | + } |
| 24 | + |
| 25 | + public function testShouldConvertMessageToJsonString() |
| 26 | + { |
| 27 | + $serializer = new JsonSerializer(); |
| 28 | + |
| 29 | + $message = new RdKafkaMessage('theBody', ['aProp' => 'aPropVal'], ['aHeader' => 'aHeaderVal']); |
| 30 | + |
| 31 | + $json = $serializer->toString($message); |
| 32 | + |
| 33 | + $this->assertSame('{"body":"theBody","properties":{"aProp":"aPropVal"},"headers":{"aHeader":"aHeaderVal"}}', $json); |
| 34 | + } |
| 35 | + |
| 36 | + public function testThrowIfFailedToEncodeMessageToJson() |
| 37 | + { |
| 38 | + $serializer = new JsonSerializer(); |
| 39 | + |
| 40 | + $message = new RdKafkaMessage('theBody', ['aProp' => STDIN]); |
| 41 | + |
| 42 | + $this->expectException(\LogicException::class); |
| 43 | + $this->expectExceptionMessage('The malformed json given. Error 8 and message Type is not supported'); |
| 44 | + $serializer->toString($message); |
| 45 | + } |
| 46 | + |
| 47 | + public function testShouldConvertJsonStringToMessage() |
| 48 | + { |
| 49 | + $serializer = new JsonSerializer(); |
| 50 | + |
| 51 | + $message = $serializer->toMessage('{"body":"theBody","properties":{"aProp":"aPropVal"},"headers":{"aHeader":"aHeaderVal"}}'); |
| 52 | + |
| 53 | + $this->assertInstanceOf(RdKafkaMessage::class, $message); |
| 54 | + |
| 55 | + $this->assertSame('theBody', $message->getBody()); |
| 56 | + $this->assertSame(['aProp' => 'aPropVal'], $message->getProperties()); |
| 57 | + $this->assertSame(['aHeader' => 'aHeaderVal'], $message->getHeaders()); |
| 58 | + } |
| 59 | + |
| 60 | + public function testThrowIfFailedToDecodeJsonToMessage() |
| 61 | + { |
| 62 | + $serializer = new JsonSerializer(); |
| 63 | + |
| 64 | + $this->expectException(\LogicException::class); |
| 65 | + $this->expectExceptionMessage('The malformed json given. Error 2 and message State mismatch (invalid or malformed JSON)'); |
| 66 | + $serializer->toMessage('{]'); |
| 67 | + } |
| 68 | +} |
0 commit comments