Skip to content

Commit b0bc1ba

Browse files
committed
fix #1 properly handle null values
1 parent 34b4d41 commit b0bc1ba

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/JsonDiff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private function process($original, $new)
177177
$path = $this->path;
178178
$this->path .= '/' . urlencode($key);
179179

180-
if (isset($newArray[$key])) {
180+
if (array_key_exists($key, $newArray)) {
181181
$newOrdered[$key] = $this->process($originalValue, $newArray[$key]);
182182
unset($newArray[$key]);
183183
} else {

tests/src/RearrangeTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,48 @@ public function testRemoved()
140140

141141
}
142142

143+
144+
public function testNull()
145+
{
146+
$originalJson = <<<'JSON'
147+
{
148+
"key2": 2,
149+
"key3": null,
150+
"key4": [
151+
{"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
152+
]
153+
}
154+
JSON;
155+
156+
$newJson = <<<'JSON'
157+
{
158+
"key3": null
159+
}
160+
JSON;
161+
162+
$expected = <<<'JSON'
163+
{
164+
"key2": 2,
165+
"key4": [
166+
{"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
167+
]
168+
}
169+
JSON;
170+
171+
$r = new JsonDiff(json_decode($originalJson), json_decode($newJson));
172+
$this->assertSame(array(
173+
'#/key2',
174+
'#/key4',
175+
), $r->getRemovedPaths());
176+
177+
$this->assertSame(2, $r->getRemovedCnt());
178+
179+
$this->assertSame(
180+
json_encode(json_decode($expected), JSON_PRETTY_PRINT),
181+
json_encode($r->getRemoved(), JSON_PRETTY_PRINT)
182+
);
183+
184+
}
185+
186+
143187
}

0 commit comments

Comments
 (0)