-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix incorrect serialisation. Thanks to @Westie for reporting the issue
- Loading branch information
1 parent
ac75764
commit 5337148
Showing
4 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
{ | ||
return new ArrayIterator($this->items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |