Skip to content

Commit

Permalink
fix #1 properly handle null values
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Nov 29, 2017
1 parent 34b4d41 commit b0bc1ba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/JsonDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private function process($original, $new)
$path = $this->path;
$this->path .= '/' . urlencode($key);

if (isset($newArray[$key])) {
if (array_key_exists($key, $newArray)) {
$newOrdered[$key] = $this->process($originalValue, $newArray[$key]);
unset($newArray[$key]);
} else {
Expand Down
44 changes: 44 additions & 0 deletions tests/src/RearrangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,48 @@ public function testRemoved()

}


public function testNull()
{
$originalJson = <<<'JSON'
{
"key2": 2,
"key3": null,
"key4": [
{"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
]
}
JSON;

$newJson = <<<'JSON'
{
"key3": null
}
JSON;

$expected = <<<'JSON'
{
"key2": 2,
"key4": [
{"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
]
}
JSON;

$r = new JsonDiff(json_decode($originalJson), json_decode($newJson));
$this->assertSame(array(
'#/key2',
'#/key4',
), $r->getRemovedPaths());

$this->assertSame(2, $r->getRemovedCnt());

$this->assertSame(
json_encode(json_decode($expected), JSON_PRETTY_PRINT),
json_encode($r->getRemoved(), JSON_PRETTY_PRINT)
);

}


}

0 comments on commit b0bc1ba

Please sign in to comment.