diff --git a/src/Parse/Internal/RemoveOperation.php b/src/Parse/Internal/RemoveOperation.php index bee46f58..c4b57b7b 100644 --- a/src/Parse/Internal/RemoveOperation.php +++ b/src/Parse/Internal/RemoveOperation.php @@ -116,24 +116,36 @@ public function _apply($oldValue, $obj, $key) $newValue = []; foreach ($oldValue as $oldObject) { - foreach ($this->objects as $newObject) { - if ($oldObject instanceof ParseObject) { - if ($newObject instanceof ParseObject - && !$oldObject->isDirty() - && $oldObject->getObjectId() == $newObject->getObjectId() - ) { - // found the object, won't add it. - } else { - $newValue[] = $oldObject; - } - } else { - if ($oldObject !== $newObject) { - $newValue[] = $oldObject; - } - } + if (!$this->isRemoved($oldObject)) { + $newValue[] = $oldObject; } } return $newValue; } + + /** + * Checks if a value matches any of the objects to remove. + * + * @param mixed $oldObject Value to check. + * + * @return bool + */ + private function isRemoved($oldObject) + { + foreach ($this->objects as $newObject) { + if ($oldObject instanceof ParseObject) { + if ($newObject instanceof ParseObject + && !$oldObject->isDirty() + && $oldObject->getObjectId() == $newObject->getObjectId() + ) { + return true; + } + } elseif ($oldObject === $newObject) { + return true; + } + } + + return false; + } } diff --git a/tests/Parse/RemoveOperationTest.php b/tests/Parse/RemoveOperationTest.php index 794d9989..f137b848 100644 --- a/tests/Parse/RemoveOperationTest.php +++ b/tests/Parse/RemoveOperationTest.php @@ -9,11 +9,17 @@ use Parse\Internal\DeleteOperation; use Parse\Internal\RemoveOperation; use Parse\Internal\SetOperation; +use Parse\ParseObject; use PHPUnit\Framework\TestCase; class RemoveOperationTest extends TestCase { + public static function setUpBeforeClass() : void + { + Helper::setUp(); + } + /** * @group remove-op */ @@ -82,4 +88,66 @@ public function testEmptyApply() ]); $this->assertEmpty($removeOp->_apply([], null, null)); } + + /** + * @group remove-op + */ + public function testApplyRemovesMultipleValues() + { + $removeOp = new RemoveOperation([1, 2]); + $this->assertSame([3], $removeOp->_apply([1, 2, 3], null, null)); + } + + /** + * @group remove-op + */ + public function testApplyRemovesAllDuplicates() + { + $removeOp = new RemoveOperation(['a', 'b']); + $this->assertSame( + ['c'], + $removeOp->_apply(['a', 'b', 'a', 'c', 'b'], null, null) + ); + } + + /** + * @group remove-op + */ + public function testApplyWithNothingToRemove() + { + $removeOp = new RemoveOperation([]); + $this->assertSame([1, 2], $removeOp->_apply([1, 2], null, null)); + } + + /** + * @group remove-op + */ + public function testApplyRemovesParseObjectsByObjectId() + { + $removeOp = new RemoveOperation([ + ParseObject::create('TestObject', 'id1'), + ParseObject::create('TestObject', 'id2'), + ]); + $kept = ParseObject::create('TestObject', 'id3'); + + $result = $removeOp->_apply([ + ParseObject::create('TestObject', 'id1'), + ParseObject::create('TestObject', 'id2'), + $kept, + ], null, null); + + $this->assertSame([$kept], $result); + } + + /** + * @group remove-op-merge + */ + public function testMergeWithSetRemovesMultipleValues() + { + $removeOp = new RemoveOperation([1, 2]); + $merged = $removeOp->_mergeWithPrevious(new SetOperation([1, 2, 3])); + + $this->assertTrue($merged instanceof SetOperation); + $this->assertSame([3], $merged->getValue()); + } }