1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace KingsonDe\Marshal; |
6
|
|
|
|
7
|
|
|
use KingsonDe\Marshal\Data\CollectionCallable; |
8
|
|
|
use KingsonDe\Marshal\Data\FlexibleData; |
9
|
|
|
use KingsonDe\Marshal\Example\Mapper\UserMapper; |
10
|
|
|
use KingsonDe\Marshal\Example\Model\User; |
11
|
|
|
use KingsonDe\Marshal\Example\ObjectMapper\UserIdMapper; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class MarshalJsonTest extends TestCase { |
15
|
|
|
|
16
|
|
|
public function testJsonSerialize() { |
17
|
|
|
$json = MarshalJson::serializeItemCallable([$this, 'mapUser'], $this->createUser()); |
18
|
|
|
|
19
|
|
|
$this->assertJsonStringEqualsJsonFile(__DIR__ . '/Fixtures/User.json', $json); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testJsonSerializeWithOptions() { |
23
|
|
|
MarshalJson::setEncodingOptions(JSON_HEX_TAG | JSON_HEX_AMP); |
24
|
|
|
|
25
|
|
|
$json = MarshalJson::serializeItemCallable([$this, 'mapUser'], $this->createUser()); |
26
|
|
|
|
27
|
|
|
$this->assertJsonStringEqualsJsonFile(__DIR__ . '/Fixtures/UserEscaped.json', $json); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @expectedException \KingsonDe\Marshal\Exception\JsonSerializeException |
32
|
|
|
*/ |
33
|
|
|
public function testBuildDataStructureIsNull() { |
34
|
|
|
MarshalJson::serializeItemCallable(function () { |
35
|
|
|
return null; |
36
|
|
|
}); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @expectedException \KingsonDe\Marshal\Exception\JsonSerializeException |
41
|
|
|
*/ |
42
|
|
|
public function testSerializationFailed() { |
43
|
|
|
MarshalJson::serializeItemCallable(function () { |
44
|
|
|
return [ |
45
|
|
|
'malformedJson' => "\xB1\x31", |
46
|
|
|
]; |
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testDeserializeMapperGeneratedJson() { |
51
|
|
|
$json = MarshalJson::serializeItem(new UserMapper(), new User(123, '[email protected]')); |
52
|
|
|
|
53
|
|
|
$flexibleData = new FlexibleData(MarshalJson::deserializeJsonToData($json)); |
54
|
|
|
|
55
|
|
|
$newJson = MarshalJson::serialize($flexibleData); |
56
|
|
|
|
57
|
|
|
$this->assertJsonStringEqualsJsonString($json, $newJson); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testDeserializeJsonFile() { |
61
|
|
|
$json = file_get_contents(__DIR__ . '/Fixtures/User.json'); |
62
|
|
|
|
63
|
|
|
$flexibleData = new FlexibleData(MarshalJson::deserializeJsonToData($json)); |
64
|
|
|
|
65
|
|
|
$newJson = MarshalJson::serialize($flexibleData); |
66
|
|
|
|
67
|
|
|
$this->assertJsonStringEqualsJsonString($json, $newJson); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testDeserializeToInt() { |
71
|
|
|
$json = file_get_contents(__DIR__ . '/Fixtures/User.json'); |
72
|
|
|
|
73
|
|
|
$id = MarshalJson::deserializeJson($json, new UserIdMapper()); |
74
|
|
|
|
75
|
|
|
$this->assertSame(123, $id); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testDeserializeWithCallable() { |
79
|
|
|
$json = file_get_contents(__DIR__ . '/Fixtures/User.json'); |
80
|
|
|
|
81
|
|
|
$id = MarshalJson::deserializeJsonCallable($json, function (FlexibleData $flexibleData) { |
82
|
|
|
return $flexibleData['id']; |
83
|
|
|
}); |
84
|
|
|
|
85
|
|
|
$this->assertSame(123, $id); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @expectedException \KingsonDe\Marshal\Exception\JsonDeserializeException |
90
|
|
|
*/ |
91
|
|
|
public function testDeserializeInvalidXml() { |
92
|
|
|
MarshalJson::deserializeJsonToData('{not=valid}'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function mapUser(\stdClass $user) { |
96
|
|
|
return [ |
97
|
|
|
'id' => $user->id, |
98
|
|
|
'score' => $user->score, |
99
|
|
|
'email' => $user->email, |
100
|
|
|
'null' => null, |
101
|
|
|
'followers' => new CollectionCallable(function ($username) { |
102
|
|
|
return [ |
103
|
|
|
'username' => $username, |
104
|
|
|
]; |
105
|
|
|
}, $user->followers) |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function createUser() { |
110
|
|
|
$user = new \stdClass(); |
111
|
|
|
$user->id = 123; |
112
|
|
|
$user->score = 3.4; |
113
|
|
|
$user->email = '[email protected]'; |
114
|
|
|
$user->followers = ['pfefferkuchenmann & <co>', 'lululu']; |
115
|
|
|
|
116
|
|
|
return $user; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|