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