/
JSONTest.php
266 lines (246 loc) · 6.73 KB
/
JSONTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
require_once dirname(dirname(dirname(__DIR__))) . '/lib/autoloader.php';
/**
* Tests for JSON.
*
* @author Conor McDermottroe <conor@mcdermottroe.com>
*/
class JSONTest
extends TestCase
{
/**
* Test a few values can be encoded into an expected format.
*
* @return void
*/
public function testEncode() {
$samples = array(
'{}' => array(),
'[1,2,3]' => array(1, 2, 3),
'{"foo":"bar"}' => array('foo' => 'bar'),
'{"foo":{}}' => array('foo' => array()),
'{"foo":[1,2,3]}' => array('foo' => array(1, 2, 3)),
);
foreach ($samples as $expected => $object) {
$this->assertEquals($expected, JSON::encode($object));
}
}
/**
* Test a few values can be decoded into an expected format.
*
* @return void
*/
public function testDecode() {
$samples = array(
'{}' => array(),
'[1,2,3]' => array(1, 2, 3),
'{"foo":"bar"}' => array('foo' => 'bar'),
'{"foo":{}}' => array('foo' => array()),
'{"foo":[1,2,3]}' => array('foo' => array(1, 2, 3)),
);
foreach ($samples as $json => $expected) {
$this->assertEquals($expected, JSON::decode($json));
}
}
/**
* Check that a selection of values can be round-tripped in both directions
* through the JSON class.
*
* @return void
*/
public function testRoundTrip() {
$encode_decode = array(
array(),
array(1,2,3),
array('foo' => 'bar', 'baz' => 'quux'),
);
foreach ($encode_decode as $sample) {
$encoded = JSON::encode($sample);
$decoded = JSON::decode($encoded);
$this->assertEquals($sample, $decoded);
}
$decode_encode = array(
'{}',
'[1,2,3]',
'{"foo":"bar"}',
'{"foo":{}}',
);
foreach ($decode_encode as $sample) {
$decoded = JSON::decode($sample);
$encoded = JSON::encode($decoded);
$this->assertEquals($sample, $encoded);
}
}
/**
* Make sure that encode refuses to encode some values but let decode
* decode the same ones. I don't want to spread bad JSON around, but I
* don't want to choke on it unnecessarily.
*
* @return void
*/
public function testEncodeLessForgivingThanDecode() {
// null
$this->assertEquals(null, JSON::decode('null'));
$this->assertException(
function () {
JSON::encode(null);
}
);
// true
$this->assertEquals(true, JSON::decode('true'));
$this->assertException(
function () {
JSON::encode(true);
}
);
// false
$this->assertEquals(false, JSON::decode('false'));
$this->assertException(
function () {
JSON::encode(false);
}
);
// a naked int
$this->assertEquals(1, JSON::decode('1'));
$this->assertException(
function () {
JSON::encode(1);
}
);
// a naked double
$this->assertEquals(3.14, JSON::decode('3.14'));
$this->assertException(
function () {
JSON::encode(3.14);
}
);
// a raw string
$this->assertEquals("foo", JSON::decode('"foo"'));
$this->assertException(
function () {
JSON::encode("foo");
}
);
}
/**
* Ensure that it's possible to encode an object.
*
* @return void
*/
public function testObjectsGetEncoded() {
$object = new stdclass();
$object->member = 'value';
$this->assertEquals(
'{"member":"value"}',
JSON::encode($object)
);
}
/**
* Trigger JSON_ERROR_DEPTH.
*
* @return void
*/
public function testOverlyNestedJSONCausesException() {
$json = '{}';
for ($i = 0; $i < 1024; $i++) {
$json = "{\"inner\": $json}";
}
$this->assertException(
function () use ($json) {
JSON::decode($json);
}
);
}
/**
* Trigger JSON_ERROR_CTRL_CHAR.
*
* @return void
*/
public function testControlCharactersInInputCausesException() {
$this->assertException(
function () {
JSON::decode("{\"foo\":\"\x01\"}");
}
);
}
/**
* Trigger JSON_ERROR_SYNTAX.
*
* @return void
*/
public function testMalformedJSONCausesSyntaxError() {
$this->assertException(
function () {
JSON::decode('{"foo":}');
}
);
}
/**
* Trigger JSON_ERROR_UTF8.
*
* @return void
*/
public function testMalformedUTF8CausesException() {
$this->assertException(
function () {
JSON::decode("{\"Bad UTF-8\": \"\x80\"}");
}
);
}
/**
* Trigger JSON_ERROR_RECURSION or the old PHP error for recursion.
*
* @return void
*/
public function testRecursiveStructureCausesException() {
$array = array();
$array = array('foo' => &$array);
$this->assertException(
function () {
JSON::encode($array);
}
);
}
/**
* Trigger JSON_ERROR_INF_OR_NAN or the old PHP errors for Inf/NaN.
*
* @return void
*/
public function testInfOrNaNCausesException() {
if ($this->isHHVMAndOlderThan('3.3.0')) {
$this->markTestSkipped('This test fails on old version of HHVM');
}
$this->assertException(
function () {
JSON::encode(array('Infinity' => INF));
},
null,
"Failed to throw an exception on encoding INF"
);
$this->assertException(
function () {
JSON::encode(array('NaN' => NAN));
},
null,
"Failed to throw an exception on encoding NAN"
);
}
/**
* Trigger JSON_ERROR_UNSUPPORTED_TYPE or the old PHP error for an
* unsupported type.
*
* @return void
*/
public function testUnsupportedTypeCausesException() {
if ($this->isHHVMAndOlderThan('3.5.1')) {
$this->markTestSkipped('This test fails on old version of HHVM');
}
$this->assertException(
function () {
$v = JSON::encode(array('Resource' => fopen('/dev/null', 'r')));
var_export($v);
}
);
}
}
?>