1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koded\Stdlib\Serializer; |
4
|
|
|
|
5
|
|
|
use Koded\Stdlib\Interfaces\Serializer; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use function Koded\Stdlib\{json_serialize, json_unserialize}; |
8
|
|
|
|
9
|
|
|
class JsonSerializerTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
const SERIALIZED_JSON = '{"object":{},"array":[],"str":"php","float":2.5,"int":7,"bool":false}'; |
13
|
|
|
|
14
|
|
|
/** @var JsonSerializer */ |
15
|
|
|
private $SUT; |
16
|
|
|
|
17
|
|
|
/** @dataProvider data */ |
18
|
|
|
public function test_serialize($data) |
19
|
|
|
{ |
20
|
|
|
$this->assertEquals(self::SERIALIZED_JSON, $this->SUT->serialize($data)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function test_serialize_iterable() |
24
|
|
|
{ |
25
|
|
|
$data = json_unserialize(self::SERIALIZED_JSON); |
26
|
|
|
$iter = new \ArrayIterator($data); |
27
|
|
|
|
28
|
|
|
$this->assertEquals(self::SERIALIZED_JSON, $this->SUT->serialize($iter)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function test_expects_array_if_data_is_empty_array() |
32
|
|
|
{ |
33
|
|
|
$this->assertSame('[]', json_serialize([])); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function test_expects_stdClass_if_data_is_object() |
37
|
|
|
{ |
38
|
|
|
$this->assertSame('{}', json_serialize(new \stdClass)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function test_expects_trailing_zero_to_be_removed() |
42
|
|
|
{ |
43
|
|
|
$this->assertEquals('[2.5]', json_serialize([2.50])); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function test_unserialize() |
47
|
|
|
{ |
48
|
|
|
$this->assertEquals(json_decode(self::SERIALIZED_JSON, false), $this->SUT->unserialize(self::SERIALIZED_JSON)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function test_unserialize_error() |
52
|
|
|
{ |
53
|
|
|
$this->assertSame('', $this->SUT->unserialize(''), 'Returns empty string on JSON unserialize error'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testName() |
57
|
|
|
{ |
58
|
|
|
$this->assertSame(Serializer::JSON, $this->SUT->type()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function data() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
[ |
65
|
|
|
require __DIR__ . '/../fixtures/config-test.php' |
66
|
|
|
] |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function setUp(): void |
71
|
|
|
{ |
72
|
|
|
$this->SUT = new JsonSerializer; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|