Skip to content

Commit

Permalink
Fix, ensure correct object properties after an array path - closes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
narcoticfresh authored and raphaelstolt committed Aug 29, 2016
1 parent fa790b5 commit edc2aa5
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/integration/Rs/Json/PatchAddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,42 @@ public function shouldAddToSingleElementArray()
$patchedDocument
);
}

/**
* @test
* @ticket 37 (https://github.com/raphaelstolt/php-jsonpatch/issues/37)
*/
public function shouldCorrectlyUseNumericIndexInObjectHandling()
{
$targetDocument = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"qux"} ] }}}';
$patchDocument = '[{"op":"add", "path":"/foo/bar/baz/1", "value":{"bar":"otherValue"} }]';
$expectedDocument = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"otherValue"}, {"bar":"qux"} ] }}}';

$patch = new Patch($targetDocument, $patchDocument);
$patchedDocument = $patch->apply();

$this->assertJsonStringEqualsJsonString(
$expectedDocument,
$patchedDocument
);
}

/**
* @test
* @ticket 37 (https://github.com/raphaelstolt/php-jsonpatch/issues/37)
*/
public function shouldCorrectlyUseNumericIndexInObjectHandlingWithAddedSubProp()
{
$targetDocument = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"qux"} ] }}}';
$patchDocument = '[{"op":"add", "path":"/foo/bar/baz/1/bar", "value":"otherValue"}]';
$expectedDocument = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"otherValue"} ] }}}';

$patch = new Patch($targetDocument, $patchDocument);
$patchedDocument = $patch->apply();

$this->assertJsonStringEqualsJsonString(
$expectedDocument,
$patchedDocument
);
}
}
38 changes: 38 additions & 0 deletions tests/integration/Rs/Json/PatchReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,42 @@ public function shouldPreserveObjectsSameLevel()
$patchedDocument
);
}

/**
* @test
* @ticket 37 (https://github.com/raphaelstolt/php-jsonpatch/issues/37)
*/
public function shouldCorrectlyUseNumericIndexInObjectHandling()
{
$targetDocument = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"qux"} ] }}}';
$patchDocument = '[{"op":"replace", "path":"/foo/bar/baz/1", "value":{"bar":"otherValue"} }]';
$expectedDocument = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"otherValue"} ] }}}';

$patch = new Patch($targetDocument, $patchDocument);
$patchedDocument = $patch->apply();

$this->assertJsonStringEqualsJsonString(
$expectedDocument,
$patchedDocument
);
}

/**
* @test
* @ticket 37 (https://github.com/raphaelstolt/php-jsonpatch/issues/37)
*/
public function shouldCorrectlyUseNumericIndexInObjectHandlingWithAddedSubProp()
{
$targetDocument = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"qux"} ] }}}';
$patchDocument = '[{"op":"replace", "path":"/foo/bar/baz/1/bar", "value":"otherValue"}]';
$expectedDocument = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"otherValue"} ] }}}';

$patch = new Patch($targetDocument, $patchDocument);
$patchedDocument = $patch->apply();

$this->assertJsonStringEqualsJsonString(
$expectedDocument,
$patchedDocument
);
}
}
43 changes: 43 additions & 0 deletions tests/unit/Rs/Json/Patch/Operations/AddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,49 @@ public function shouldPreserveEmptyObjectSameLevel()
);
}

/**
* @test
* @ticket 37 (https://github.com/raphaelstolt/php-jsonpatch/issues/37)
*/
public function shouldCorrectlyUseNumericIndexInObjectHandling()
{
$targetJson = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"qux"} ] }}}';
$expectedJson = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"otherValue"}, {"bar":"qux"} ] }}}';

$operation = new \stdClass;
$operation->path = '/foo/bar/baz/1';
$operation->value = new \stdClass();
$operation->value->bar = 'otherValue';

$addOperation = new Add($operation);

$this->assertJsonStringEqualsJsonString(
$expectedJson,
$addOperation->perform($targetJson)
);
}

/**
* @test
* @ticket 37 (https://github.com/raphaelstolt/php-jsonpatch/issues/37)
*/
public function shouldCorrectlyUseNumericIndexInObjectHandlingWithAddedSubProp()
{
$targetJson = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"qux"} ] }}}';
$expectedJson = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"otherValue"} ] }}}';

$operation = new \stdClass;
$operation->path = '/foo/bar/baz/1/bar';
$operation->value = 'otherValue';

$addOperation = new Add($operation);

$this->assertJsonStringEqualsJsonString(
$expectedJson,
$addOperation->perform($targetJson)
);
}

/**
* @test
*/
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/Rs/Json/Patch/Operations/ReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,48 @@ public function shouldPreserveEmptyObjectSameLevel()
);
}

/**
* @test
* @ticket 37 (https://github.com/raphaelstolt/php-jsonpatch/issues/37)
*/
public function shouldCorrectlyUseNumericIndexInObjectHandling()
{
$targetJson = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"qux"} ] }}}';
$expectedJson = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"otherValue"} ] }}}';

$operation = new \stdClass;
$operation->path = '/foo/bar/baz/1';
$operation->value = '{"bar":"otherValue"}';

$replaceOperation = new Replace($operation);

$this->assertJsonStringEqualsJsonString(
$expectedJson,
$replaceOperation->perform($targetJson)
);
}

/**
* @test
* @ticket 37 (https://github.com/raphaelstolt/php-jsonpatch/issues/37)
*/
public function shouldCorrectlyUseNumericIndexInObjectHandlingWithAddedSubProp()
{
$targetJson = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"qux"} ] }}}';
$expectedJson = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"otherValue"} ] }}}';

$operation = new \stdClass;
$operation->path = '/foo/bar/baz/1/bar';
$operation->value = 'otherValue';

$replaceOperation = new Replace($operation);

$this->assertJsonStringEqualsJsonString(
$expectedJson,
$replaceOperation->perform($targetJson)
);
}

/**
* @test
* @ticket 5 (https://github.com/raphaelstolt/php-jsonpatch/issues/5)
Expand Down

0 comments on commit edc2aa5

Please sign in to comment.