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