-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathJsonTest.php
More file actions
221 lines (189 loc) · 6.58 KB
/
JsonTest.php
File metadata and controls
221 lines (189 loc) · 6.58 KB
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
<?php
declare(strict_types=1);
namespace tests;
use flight\util\Json;
use PHPUnit\Framework\TestCase;
use Exception;
class JsonTest extends TestCase
{
protected function setUp(): void
{
// Clear any previous JSON errors
json_encode(['clear' => 'error']);
}
// Test basic encoding
public function testEncode(): void
{
$data = ['name' => 'John', 'age' => 30];
$result = Json::encode($data);
$this->assertIsString($result);
$this->assertJson($result);
}
// Test encoding with custom options
public function testEncodeWithOptions(): void
{
$data = ['url' => 'https://example.com/path'];
$result = Json::encode($data, JSON_UNESCAPED_SLASHES);
$this->assertStringContainsString('https://example.com/path', $result);
}
// Test encoding with invalid data
public function testEncodeInvalidData(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('JSON encoding failed');
// Create a resource that cannot be encoded
$resource = fopen('php://memory', 'r');
Json::encode($resource);
fclose($resource);
}
// Test basic decoding
public function testDecode(): void
{
$json = '{"name":"John","age":30}';
$result = Json::decode($json);
$this->assertIsObject($result);
$this->assertEquals('John', $result->name);
$this->assertEquals(30, $result->age);
}
// Test decoding to associative array
public function testDecodeAssociative(): void
{
$json = '{"name":"John","age":30}';
$result = Json::decode($json, true);
$this->assertIsArray($result);
$this->assertEquals('John', $result['name']);
$this->assertEquals(30, $result['age']);
}
// Test decoding with custom depth
public function testDecodeWithDepth(): void
{
$json = '{"level1":{"level2":{"level3":"value"}}}';
$result = Json::decode($json, true, 512);
$this->assertEquals('value', $result['level1']['level2']['level3']);
}
// Test decoding invalid JSON
public function testDecodeInvalidJson(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('JSON decoding failed');
Json::decode('{"invalid": json}');
}
// Test JSON validation with valid JSON
public function testIsValidWithValidJson(): void
{
$validJson = '{"name":"John","age":30}';
$this->assertTrue(Json::isValid($validJson));
}
// Test JSON validation with invalid JSON
public function testIsValidWithInvalidJson(): void
{
$invalidJson = '{"invalid": json}';
$this->assertFalse(Json::isValid($invalidJson));
}
// Test JSON validation with empty string
public function testIsValidWithEmptyString(): void
{
$this->assertFalse(Json::isValid(''));
}
// Test pretty print functionality
public function testPrettyPrint(): void
{
$data = ['name' => 'John', 'age' => 30];
$result = Json::prettyPrint($data);
$this->assertStringContainsString("\n", $result);
$this->assertStringContainsString(' ', $result); // Should contain indentation
}
// Test pretty print with additional options
public function testPrettyPrintWithAdditionalOptions(): void
{
$data = ['html' => '<script>alert("test")</script>'];
$result = Json::prettyPrint($data, JSON_HEX_TAG);
$this->assertStringContainsString('\u003C', $result); // Should escape < character
}
// Test getLastError when no error
public function testGetLastErrorNoError(): void
{
// Perform a valid JSON operation first
Json::encode(['valid' => 'data']);
$this->assertEquals('', Json::getLastError());
}
// Test getLastError when there is an error
public function testGetLastErrorWithError(): void
{
// Trigger a JSON error by using json_decode directly with invalid JSON
// This bypasses our Json class exception handling to test getLastError()
json_decode('{"invalid": json}');
$errorMessage = Json::getLastError();
$this->assertNotEmpty($errorMessage);
$this->assertIsString($errorMessage);
}
// Test encoding arrays
public function testEncodeArray(): void
{
$data = [1, 2, 3, 'four'];
$result = Json::encode($data);
$this->assertEquals('[1,2,3,"four"]', $result);
}
// Test encoding null
public function testEncodeNull(): void
{
$result = Json::encode(null);
$this->assertEquals('null', $result);
}
// Test encoding boolean values
public function testEncodeBoolean(): void
{
$this->assertEquals('true', Json::encode(true));
$this->assertEquals('false', Json::encode(false));
}
// Test encoding strings
public function testEncodeString(): void
{
$result = Json::encode('Hello World');
$this->assertEquals('"Hello World"', $result);
}
// Test encoding numbers
public function testEncodeNumbers(): void
{
$this->assertEquals('42', Json::encode(42));
$this->assertEquals('3.14', Json::encode(3.14));
}
// Test decoding arrays
public function testDecodeArray(): void
{
$json = '[1,2,3,"four"]';
$result = Json::decode($json, true);
$this->assertEquals([1, 2, 3, 'four'], $result);
}
// Test decoding nested objects
public function testDecodeNestedObjects(): void
{
$json = '{"user":{"name":"John","profile":{"age":30}}}';
$result = Json::decode($json, true);
$this->assertEquals('John', $result['user']['name']);
$this->assertEquals(30, $result['user']['profile']['age']);
}
// Test default encoding options are applied
public function testDefaultEncodingOptions(): void
{
$data = ['url' => 'https://example.com/path'];
$result = Json::encode($data);
// Should not escape slashes due to JSON_UNESCAPED_SLASHES
$this->assertStringContainsString('https://example.com/path', $result);
}
// Test round trip encoding/decoding
public function testRoundTrip(): void
{
$original = [
'string' => 'test',
'number' => 42,
'boolean' => true,
'null' => null,
'array' => [1, 2, 3],
'object' => ['nested' => 'value']
];
$encoded = Json::encode($original);
$decoded = Json::decode($encoded, true);
$this->assertEquals($original, $decoded);
}
}