|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Lendable\Json\Unit; |
| 6 | + |
| 7 | +use Lendable\Json\DeserializationFailed; |
| 8 | +use Lendable\Json\InvalidDeserializedData; |
| 9 | +use Lendable\Json\SerializationFailed; |
| 10 | +use Lendable\Json\Serializer; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * @covers \Lendable\Json\Serializer |
| 15 | + * @covers \Lendable\Json\SerializationFailed |
| 16 | + * @covers \Lendable\Json\DeserializationFailed |
| 17 | + * @covers \Lendable\Json\InvalidDeserializedData |
| 18 | + */ |
| 19 | +final class SerializerTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Serializer |
| 23 | + */ |
| 24 | + private $serializer; |
| 25 | + |
| 26 | + /** |
| 27 | + * @test |
| 28 | + */ |
| 29 | + public function it_can_serialize_an_array_of_scalars_to_json(): void |
| 30 | + { |
| 31 | + $result = $this->serializer->serialize(['foo' => 'bar', 'baz' => [1.03, true, 'foobar']]); |
| 32 | + |
| 33 | + $this->assertSame('{"foo":"bar","baz":[1.03,true,"foobar"]}', $result); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @test |
| 38 | + */ |
| 39 | + public function it_throws_when_serializing_if_an_error_encountered(): void |
| 40 | + { |
| 41 | + $this->expectException(SerializationFailed::class); |
| 42 | + $this->expectExceptionMessage('Failed to serialize data to JSON. Error code: 5, error message: Malformed UTF-8 characters, possibly incorrectly encoded.'); |
| 43 | + |
| 44 | + $this->serializer->serialize(["\xf0\x28\x8c\xbc" => 'bar']); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @test |
| 49 | + */ |
| 50 | + public function it_can_deserialize_from_a_json_string_to_php_scalars(): void |
| 51 | + { |
| 52 | + $result = $this->serializer->deserialize('{"foo":"bar","baz":[1.03,true,"foobar"]}'); |
| 53 | + |
| 54 | + $this->assertSame(['foo' => 'bar', 'baz' => [1.03, true, 'foobar']], $result); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @test |
| 59 | + */ |
| 60 | + public function it_throws_when_deserializing_if_an_error_encountered(): void |
| 61 | + { |
| 62 | + $this->expectException(DeserializationFailed::class); |
| 63 | + $this->expectExceptionMessage('Failed to deserialize data from JSON. Error code: 4, error message: Syntax error.'); |
| 64 | + |
| 65 | + $this->serializer->deserialize('{"unclosed":"bad","object":"json"'); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @test |
| 70 | + */ |
| 71 | + public function it_throws_when_deserializing_if_the_result_is_not_an_array(): void |
| 72 | + { |
| 73 | + $this->expectExceptionMessage(InvalidDeserializedData::class); |
| 74 | + $this->expectExceptionMessage('Expected array when deserializing JSON, got "boolean".'); |
| 75 | + |
| 76 | + $this->serializer->deserialize('true'); |
| 77 | + } |
| 78 | + |
| 79 | + protected function setUp(): void |
| 80 | + { |
| 81 | + $this->serializer = new Serializer(); |
| 82 | + } |
| 83 | +} |
0 commit comments