From 47b2dc40380a324c1b43bf530c6cabda2aac1892 Mon Sep 17 00:00:00 2001 From: Gustavo Freze Date: Fri, 4 Oct 2024 23:05:05 -0300 Subject: [PATCH] Release/3.3.1 (#19) --- src/Internal/JsonSerializer.php | 10 ++---- tests/IterableSerializerTest.php | 2 +- tests/Models/Serializable/Decimal.php | 17 ++++++++++ tests/SerializerAdapterTest.php | 49 ++++++++++++++++----------- 4 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 tests/Models/Serializable/Decimal.php diff --git a/src/Internal/JsonSerializer.php b/src/Internal/JsonSerializer.php index 3c674b3..d6a024a 100644 --- a/src/Internal/JsonSerializer.php +++ b/src/Internal/JsonSerializer.php @@ -8,15 +8,11 @@ { public function serialize(array $data): string { - $isSingleItem = count($data) === 1; - $dataToSerialize = $isSingleItem ? ($data[0] ?? null) : $data; + $isSingleItem = static fn(array $data): bool => array_keys($data) !== range(0, count($data) - 1); + $dataToSerialize = $isSingleItem($data) ? $data : ($data[0] ?? null); $json = json_encode($dataToSerialize, JSON_PRESERVE_ZERO_FRACTION); - if (!is_string($json) || $json === 'null') { - return $isSingleItem ? '{}' : '[]'; - } - - return $json; + return is_string($json) ? $json : '{}'; } } diff --git a/tests/IterableSerializerTest.php b/tests/IterableSerializerTest.php index 903f80d..e275df1 100644 --- a/tests/IterableSerializerTest.php +++ b/tests/IterableSerializerTest.php @@ -82,7 +82,7 @@ public function testSerializeMultipleInvalidItemsReturnsEmptyJsonArray(): void $actual = $serializer->toJson(); /** @Then the output should be an empty JSON array */ - self::assertSame('[[],[]]', $actual); + self::assertSame('[]', $actual); } public static function toJsonDataProvider(): array diff --git a/tests/Models/Serializable/Decimal.php b/tests/Models/Serializable/Decimal.php new file mode 100644 index 0000000..f32e67c --- /dev/null +++ b/tests/Models/Serializable/Decimal.php @@ -0,0 +1,17 @@ +toArray(); + /** @When the toArray method is called on the object */ + $actual = $object->toArray(); /** @Then the result should match the expected structure */ self::assertSame($expected, $actual); } - #[DataProvider('shippingDataProviderForJson')] - public function testSerializeToJson(Shipping $shipping, string $expected): void + #[DataProvider('dataProviderForToJson')] + public function testSerializeToJson(Serializer $object, string $expected): void { - /** @When the toJson method is called on the Shipping object */ - $actual = $shipping->toJson(); + /** @When the toJson method is called on the object */ + $actual = $object->toJson(); /** @Then the result should match the expected JSON string */ self::assertJsonStringEqualsJsonString($expected, $actual); } - public function testSerializeSingleInvalidItemReturnsEmptyJsonObject(): void + public function testSerializeSingleInvalidItemReturnsReturnsEmptyArray(): void { /** @Given a single invalid item (e.g., a function that cannot be serialized) */ $service = new Service(action: fn(): int => 0); - /** @When attempting to serialize the invalid item */ + /** @When attempting to serialize the object with the invalid item */ $actual = $service->toJson(); - /** @Then the output should be an empty JSON object */ - self::assertSame('{}', $actual); + /** @Then the invalid item should be serialized as an empty array in the JSON output */ + self::assertSame('{"action":[]}', $actual); } - public static function shippingDataProviderForArray(): array + public static function dataProviderForToArray(): array { $shippingWithNoAddresses = new Shipping(id: 1, addresses: new Addresses()); $shippingWithSingleAddress = new Shipping( @@ -87,12 +88,16 @@ public static function shippingDataProviderForArray(): array ); return [ + 'Decimal object' => [ + 'object' => new Decimal(value: 9.99), + 'expected' => ['value' => 9.99] + ], 'Shipping object with no addresses' => [ - 'shipping' => $shippingWithNoAddresses, + 'object' => $shippingWithNoAddresses, 'expected' => ['id' => 1, 'addresses' => []] ], 'Shipping object with a single address' => [ - 'shipping' => $shippingWithSingleAddress, + 'object' => $shippingWithSingleAddress, 'expected' => [ 'id' => 2, 'addresses' => [ @@ -107,7 +112,7 @@ public static function shippingDataProviderForArray(): array ] ], 'Shipping object with multiple addresses' => [ - 'shipping' => $shippingWithMultipleAddresses, + 'object' => $shippingWithMultipleAddresses, 'expected' => [ 'id' => 100000, 'addresses' => [ @@ -131,15 +136,19 @@ public static function shippingDataProviderForArray(): array ]; } - public static function shippingDataProviderForJson(): array + public static function dataProviderForToJson(): array { return [ + 'Decimal object' => [ + 'object' => new Decimal(value: 9.99), + 'expected' => '{"value":9.99}' + ], 'Shipping object with no addresses' => [ - 'shipping' => new Shipping(id: 1, addresses: new Addresses()), + 'object' => new Shipping(id: 1, addresses: new Addresses()), 'expected' => '{"id":1,"addresses":[]}' ], 'Shipping object with a single address' => [ - 'shipping' => new Shipping( + 'object' => new Shipping( id: 2, addresses: new Addresses( elements: [ @@ -156,7 +165,7 @@ public static function shippingDataProviderForJson(): array 'expected' => '{"id":2,"addresses":[{"city":"São Paulo","state":"SP","street":"Avenida Paulista","number":100,"country":"BR"}]}' ], 'Shipping object with multiple addresses' => [ - 'shipping' => new Shipping( + 'object' => new Shipping( id: 100000, addresses: new Addresses( elements: [