Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/Parse/Internal/RemoveOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
68 changes: 68 additions & 0 deletions tests/Parse/RemoveOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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());
}
}