Skip to content

Commit

Permalink
Merge pull request #7 from swaggest/issue-6
Browse files Browse the repository at this point in the history
Issue 6
  • Loading branch information
vearutop authored Feb 9, 2018
2 parents e583f89 + 688b9df commit 64972f5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/json-diff
/json-diff.tar.gz
/composer.lock
/composer.phar
/composer.phar
/clover.xml
13 changes: 11 additions & 2 deletions src/JsonDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ private function process($original, $new)
$newOrdered = array();

$originalKeys = $original instanceof \stdClass ? get_object_vars($original) : $original;
$isArray = is_array($original);
$removedOffset = 0;

foreach ($originalKeys as $key => $originalValue) {
if ($this->options & self::STOP_ON_DIFF) {
Expand All @@ -241,8 +243,12 @@ private function process($original, $new)

$path = $this->path;
$pathItems = $this->pathItems;
$this->path .= '/' . JsonPointer::escapeSegment($key, $this->options & self::JSON_URI_FRAGMENT_ID);
$this->pathItems[] = $key;
$actualKey = $key;
if ($isArray) {
$actualKey -= $removedOffset;
}
$this->path .= '/' . JsonPointer::escapeSegment($actualKey, $this->options & self::JSON_URI_FRAGMENT_ID);
$this->pathItems[] = $actualKey;

if (array_key_exists($key, $newArray)) {
$newOrdered[$key] = $this->process($originalValue, $newArray[$key]);
Expand All @@ -253,6 +259,9 @@ private function process($original, $new)
return null;
}
$this->removedPaths [] = $this->path;
if ($isArray) {
$removedOffset++;
}

$this->jsonPatch->op(new Remove($this->path));

Expand Down
4 changes: 3 additions & 1 deletion src/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ public static function remove(&$holder, $pathItems)
unset($parent->$refKey);
} else {
unset($parent[$refKey]);
$parent = array_values($parent);
if ($refKey !== count($parent)) {
$parent = array_values($parent);
}
}
}
return $ref;
Expand Down
90 changes: 90 additions & 0 deletions tests/src/Issues/Issue6Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Swaggest\JsonDiff\Tests\Issues;

use Swaggest\JsonDiff\JsonDiff;
use Swaggest\JsonDiff\JsonPatch;


/**
* @see https://github.com/swaggest/json-diff/issues/6
*/
class Issue6Test extends \PHPUnit_Framework_TestCase
{
public function testDefault()
{
$json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
$json2 = json_decode('[{"name":"b"}]');

$diff = new JsonDiff($json1, $json2);
$patch = $diff->getPatch();

$this->assertSame(<<<'JSON'
[
{
"value": "a",
"op": "test",
"path": "/0/name"
},
{
"value": "b",
"op": "replace",
"path": "/0/name"
},
{
"op": "remove",
"path": "/1"
},
{
"op": "remove",
"path": "/1"
}
]
JSON
, json_encode($patch, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES));

$json1a = $json1;
$patch->apply($json1a);

$this->assertEquals($json2, $json1a);
}

public function testOriginal()
{
$originalJson = '[{"name":"a"},{"name":"b"},{"name":"c"}]';
$newJson = '[{"name":"b"}]';
$diff = new JsonDiff(json_decode($originalJson), json_decode($newJson));

$patchJson = json_decode(json_encode($diff->getPatch()->jsonSerialize()), true);

$original = json_decode($originalJson);
$patch = JsonPatch::import($patchJson);
$patch->apply($original);
$this->assertEquals($original, json_decode($newJson));
}


public function testDoubleInverseRemove()
{
$json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
$json2 = json_decode('[{"name":"b"}]');

$patch = JsonPatch::import(json_decode('[{"op":"remove","path":"/2"},{"op":"remove","path":"/0"}]'));

$json1a = $json1;
$patch->apply($json1a);
$this->assertEquals(json_encode($json2), json_encode($json1a));
}

public function testDoubleRemove()
{
$json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
$json2 = json_decode('[{"name":"b"}]');

$patch = JsonPatch::import(json_decode('[{"op":"remove","path":"/0"},{"op":"remove","path":"/1"}]'));

$json1a = $json1;
$patch->apply($json1a);
$this->assertEquals(json_encode($json2), json_encode($json1a));
}
}

0 comments on commit 64972f5

Please sign in to comment.