Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect serialisation #263

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/Checkout/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ private function normalize(array $props)
return !is_null($value);
});
foreach ($array as $key => $value) {
// customization
if ($value instanceof DateTime) {
$array[$key] = CheckoutUtils::formatDate($value);
} elseif (is_array($value)) {
$array[$key] = $this->normalize($value);
} elseif (is_object($value)) {
$array[$key] = $this->normalize(get_object_vars($value));
if (PHP_VERSION_ID >= 70100 && is_iterable($value)) {
$array[$key] = $this->normalize(iterator_to_array($value));
} else {
$array[$key] = $this->normalize(get_object_vars($value));
}
}
$array = $this->applyKeysTransformations($array, $key);
}
Expand Down
22 changes: 22 additions & 0 deletions test/Checkout/Tests/IterableObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Checkout\Tests;

use ArrayIterator;
use IteratorAggregate;

class IterableObject implements IteratorAggregate
{
public $items;

public function __construct(array $items)
{
$this->items = $items;
}

#[\ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($this->items);
}
}
135 changes: 135 additions & 0 deletions test/Checkout/Tests/JsonSerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Checkout\Tests;

use Checkout\CheckoutUtils;
use Checkout\JsonSerializer;
use DateTime;
use PHPUnit\Framework\TestCase;

class JsonSerializerTest extends TestCase
{
public function testSerializeWithSimpleArray()
{
$serializer = new JsonSerializer();
$input = [
'name' => 'John Doe',
'age' => 30,
'null_value' => null
];
$expected = json_encode([
'name' => 'John Doe',
'age' => 30
]);

$this->assertEquals($expected, $serializer->serialize($input));
}

public function testSerializeWithDateTime()
{
$serializer = new JsonSerializer();
$date = new DateTime('2023-08-22');
$input = [
'name' => 'John Doe',
'created_at' => $date
];
$expected = json_encode([
'name' => 'John Doe',
'created_at' => CheckoutUtils::formatDate($date)
]);

$this->assertEquals($expected, $serializer->serialize($input));
}

public function testSerializeWithNestedArray()
{
$serializer = new JsonSerializer();
$input = [
'user' => [
'name' => 'John Doe',
'address' => [
'city' => 'New York',
'zip' => '10001',
'null_value' => null
]
]
];
$expected = json_encode([
'user' => [
'name' => 'John Doe',
'address' => [
'city' => 'New York',
'zip' => '10001'
]
]
]);

$this->assertEquals($expected, $serializer->serialize($input));
}

public function testSerializeWithObject()
{
$serializer = new JsonSerializer();
$input = (object)[
'name' => 'John Doe',
'age' => 30
];
$expected = json_encode([
'name' => 'John Doe',
'age' => 30
]);

$this->assertEquals($expected, $serializer->serialize($input));
}

public function testSerializeWithKeysTransformation()
{
$serializer = new JsonSerializer();
$input = [
'three_ds' => 'enabled',
'if_match' => 'etag_value'
];
$expected = json_encode([
'3ds' => 'enabled',
'if-match' => 'etag_value'
]);

$this->assertEquals($expected, $serializer->serialize($input));
}

public function testSerializeWithIterableObject()
{
$serializer = new JsonSerializer();

$iterableObject = new IterableObject([
'item1' => 'value1',
'item2' => 'value2'
]);

$input = [
'iterable' => $iterableObject
];

if (PHP_VERSION_ID >= 70100) {
// PHP >= 8.0
$expected = json_encode([
'iterable' => [
'item1' => 'value1',
'item2' => 'value2'
]
]);
} else {
// PHP 5.6 - 7.x
$expected = json_encode([
'iterable' => [
'items' => [
'item1' => 'value1',
'item2' => 'value2'
]
]
]);
}

$this->assertEquals($expected, $serializer->serialize($input));
}
}
Loading