1 | <?php |
||
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 | |||
119 |