Skip to content

Commit

Permalink
Fix incorrect serialisation. Thanks to @Westie for reporting the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko committed Aug 22, 2024
1 parent ac75764 commit 5337148
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
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 (version_compare(PHP_VERSION, '7.1.0') >= 0 && 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
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ parameters:
paths:
- lib
- test

ignoreErrors:
- '#Return type mixed of method .*::getIterator\(\) is not covariant with tentative return type Traversable of method IteratorAggregate::getIterator\(\).#'
21 changes: 21 additions & 0 deletions test/Checkout/Tests/IterableObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Checkout\Tests;

use ArrayIterator;
use IteratorAggregate;

class IterableObject implements IteratorAggregate
{
private $items;

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

public function getIterator()

Check failure on line 17 in test/Checkout/Tests/IterableObject.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.1)

Return type mixed of method Checkout\Tests\IterableObject::getIterator() is not covariant with tentative return type Traversable of method IteratorAggregate::getIterator().
{
return new ArrayIterator($this->items);
}
}
121 changes: 121 additions & 0 deletions test/Checkout/Tests/JsonSerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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
];
$expected = json_encode([
'iterable' => [
'item1' => 'value1',
'item2' => 'value2'
]
]);

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

0 comments on commit 5337148

Please sign in to comment.