1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Json\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Yiisoft\Json\Json; |
7
|
|
|
|
8
|
|
|
class JsonTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
public function testEncodeBasic(): void |
11
|
|
|
{ |
12
|
|
|
$data = '1'; |
13
|
|
|
$this->assertSame('"1"', Json::encode($data)); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function testEncodeSimpleArray(): void |
17
|
|
|
{ |
18
|
|
|
$data = [1, 2]; |
19
|
|
|
$this->assertSame('[1,2]', Json::encode($data)); |
20
|
|
|
$data = ['a' => 1, 'b' => 2]; |
21
|
|
|
$this->assertSame('{"a":1,"b":2}', Json::encode($data)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testEncodeSimpleObject(): void |
25
|
|
|
{ |
26
|
|
|
$data = new \stdClass(); |
27
|
|
|
$data->a = 1; |
28
|
|
|
$data->b = 2; |
29
|
|
|
$this->assertSame('{"a":1,"b":2}', Json::encode($data)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testEncodeEmpty(): void |
33
|
|
|
{ |
34
|
|
|
$data = []; |
35
|
|
|
$this->assertSame('[]', Json::encode($data)); |
36
|
|
|
$data = new \stdClass(); |
37
|
|
|
$this->assertSame('{}', Json::encode($data)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @see https://github.com/yiisoft/yii2/issues/957 |
42
|
|
|
*/ |
43
|
|
|
public function testEncodeNullObject(): void |
44
|
|
|
{ |
45
|
|
|
$data = (object)null; |
46
|
|
|
$this->assertSame('{}', Json::encode($data)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function testEncodeJsonSerializable(): void |
51
|
|
|
{ |
52
|
|
|
$data = new Post(42, 'json serializable'); |
53
|
|
|
$this->assertSame('{"id":42,"title":"json serializable"}', Json::encode($data)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @see https://github.com/yiisoft/yii2/issues/12043 |
58
|
|
|
*/ |
59
|
|
|
public function testEncodeWithSerializableReturningEmptyArray(): void |
60
|
|
|
{ |
61
|
|
|
$data = new Data([]); |
62
|
|
|
$this->assertSame('[]', Json::encode($data)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @see https://github.com/yiisoft/yii2/issues/12043 |
67
|
|
|
*/ |
68
|
|
|
public function testEncodeWithSerializableReturningEmptyObject(): void |
69
|
|
|
{ |
70
|
|
|
$data = new Data((object)null); |
71
|
|
|
$this->assertSame('{}', Json::encode($data)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testsHtmlEncodeEscapesCharacters(): void |
75
|
|
|
{ |
76
|
|
|
$data = '&<>"\'/'; |
77
|
|
|
$this->assertSame('"\u0026\u003C\u003E\u0022\u0027\/"', Json::htmlEncode($data)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testHtmlEncodeBasic(): void |
81
|
|
|
{ |
82
|
|
|
$data = '1'; |
83
|
|
|
$this->assertSame('"1"', Json::htmlEncode($data)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testHtmlEncodeSimpleArray(): void |
87
|
|
|
{ |
88
|
|
|
$data = [1, 2]; |
89
|
|
|
$this->assertSame('[1,2]', Json::htmlEncode($data)); |
90
|
|
|
$data = ['a' => 1, 'b' => 2]; |
91
|
|
|
$this->assertSame('{"a":1,"b":2}', Json::htmlEncode($data)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testHtmlEncodeSimpleObject(): void |
95
|
|
|
{ |
96
|
|
|
$data = new \stdClass(); |
97
|
|
|
$data->a = 1; |
98
|
|
|
$data->b = 2; |
99
|
|
|
$this->assertSame('{"a":1,"b":2}', Json::htmlEncode($data)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @see https://github.com/yiisoft/yii2/issues/957 |
104
|
|
|
*/ |
105
|
|
|
public function testHtmlEncodeNullObject(): void |
106
|
|
|
{ |
107
|
|
|
$data = (object) null; |
108
|
|
|
$this->assertSame('{}', Json::htmlEncode($data)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testHtmlEncodeJsonSerializable(): void |
112
|
|
|
{ |
113
|
|
|
$data = new Post(42, 'json serializable'); |
114
|
|
|
$this->assertSame('{"id":42,"title":"json serializable"}', Json::htmlEncode($data)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @see https://github.com/yiisoft/yii2/issues/10278 |
119
|
|
|
*/ |
120
|
|
|
public function testsHtmlEncodeXmlDocument(): void |
121
|
|
|
{ |
122
|
|
|
$xml = '<?xml version="1.0" encoding="UTF-8"?> |
123
|
|
|
<file> |
124
|
|
|
<apiKey>ieu2iqw4o</apiKey> |
125
|
|
|
<methodProperties> |
126
|
|
|
<FindByString>Kiev</FindByString> |
127
|
|
|
</methodProperties> |
128
|
|
|
</file>'; |
129
|
|
|
|
130
|
|
|
$document = simplexml_load_string($xml); |
131
|
|
|
$this->assertSame('{"apiKey":"ieu2iqw4o","methodProperties":{"FindByString":"Kiev"}}', Json::encode($document)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testsHtmlEncodeSplStack(): void |
135
|
|
|
{ |
136
|
|
|
$postsStack = new \SplStack(); |
137
|
|
|
$postsStack->push(new Post(915, 'record1')); |
138
|
|
|
$postsStack->push(new Post(456, 'record2')); |
139
|
|
|
|
140
|
|
|
$this->assertSame('{"1":{"id":456,"title":"record2"},"0":{"id":915,"title":"record1"}}', Json::encode($postsStack)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testDecodeEmptyValue(): void |
144
|
|
|
{ |
145
|
|
|
$json = ''; |
146
|
|
|
$actual = Json::decode($json); |
147
|
|
|
$this->assertNull($actual); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testDecodeBasic(): void |
151
|
|
|
{ |
152
|
|
|
$json = '"1"'; |
153
|
|
|
$this->assertSame('1', Json::decode($json)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testsDecodeArray(): void |
157
|
|
|
{ |
158
|
|
|
$json = '{"a":1,"b":2}'; |
159
|
|
|
$this->assertSame(['a' => 1, 'b' => 2], Json::decode($json)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testsDecodeInvalidJsonThrowsException(): void |
163
|
|
|
{ |
164
|
|
|
$json = '{"a":1,"b":2'; |
165
|
|
|
$this->expectException(\JsonException::class); |
166
|
|
|
Json::decode($json); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testHandleJsonError(): void |
170
|
|
|
{ |
171
|
|
|
// Basic syntax error |
172
|
|
|
try { |
173
|
|
|
$json = "{'a': '1'}"; |
174
|
|
|
Json::decode($json); |
175
|
|
|
} catch (\JsonException $e) { |
176
|
|
|
$this->assertSame('Syntax error', $e->getMessage()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Unsupported type since PHP 5.5 |
180
|
|
|
try { |
181
|
|
|
$fp = fopen('php://stdin', 'r'); |
182
|
|
|
$data = ['a' => $fp]; |
183
|
|
|
Json::encode($data); |
184
|
|
|
fclose($fp); |
185
|
|
|
} catch (\JsonException $e) { |
186
|
|
|
$this->assertSame('Type is not supported', $e->getMessage()); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @link https://github.com/yiisoft/yii2/issues/17760 |
192
|
|
|
*/ |
193
|
|
|
public function testEncodeDateTime() |
194
|
|
|
{ |
195
|
|
|
$input = new \DateTime('October 12, 2014', new \DateTimeZone('UTC')); |
196
|
|
|
$output = Json::encode($input); |
197
|
|
|
$this->assertEquals('{"date":"2014-10-12 00:00:00.000000","timezone_type":3,"timezone":"UTC"}', $output); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|