Skip to content

Commit

Permalink
Merge pull request #25 from KainGNX/unset-fix
Browse files Browse the repository at this point in the history
Update deprecated Entity::unsetProperty() to use 4.x Entity::unset()
  • Loading branch information
jeremyharris authored Feb 25, 2020
2 parents 39e09f8 + 3215019 commit 1319c1f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ORM/LazyLoadEntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ protected function _parentHas($property)
* @param array|string $property Property
* @return $this
*/
public function unsetProperty($property)
public function unset($property)
{
$property = (array)$property;
foreach ($property as $prop) {
$this->_unsetProperties[] = $prop;
}

return Entity::unsetProperty($property);
return Entity::unset($property);
}

/**
Expand Down
64 changes: 64 additions & 0 deletions tests/TestCase/ORM/LazyLoadEntityTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,38 @@ public function testTablelessEntity()
*
* @return void
*/
public function testUnset()
{
$this->Comments = $this->getTableLocator()->get('Comments');
$this->Comments->belongsTo('Authors', [
'foreignKey' => 'user_id'
]);

$comment = $this->getMockBuilder(Comment::class)
->setConstructorArgs([['id' => 1, 'user_id' => 2]])
->setMethods(['_repository'])
->getMock();

$comment
->expects($this->once())
->method('_repository')
->will($this->returnValue($this->Comments));

$this->assertInstanceOf(EntityInterface::class, $comment->author);
$comment->unset('author');
$this->assertNull($comment->author);

// test re-setting a previously un-set prop
$comment->author = 'manual set';
$this->assertSame('manual set', $comment->author);
}

/**
* tests that unsetting a property through proxy of
* \Cake\Datasource\EntityTrait::unsetProperty() until its removal
*
* @return void
*/
public function testUnsetProperty()
{
$this->Comments = $this->getTableLocator()->get('Comments');
Expand All @@ -142,6 +174,38 @@ public function testUnsetProperty()
$this->assertSame('manual set', $comment->author);
}

/**
* tests that unsetting a property by calling unset($obj->prop) which invokes
* \Cake\Datasource\EntityTrait::__unset()
*
* @return void
*/
public function testUnsetMagicMethod()
{
$this->Comments = $this->getTableLocator()->get('Comments');
$this->Comments->belongsTo('Authors', [
'foreignKey' => 'user_id'
]);

$comment = $this->getMockBuilder(Comment::class)
->setConstructorArgs([['id' => 1, 'user_id' => 2]])
->setMethods(['_repository'])
->getMock();

$comment
->expects($this->once())
->method('_repository')
->will($this->returnValue($this->Comments));

$this->assertInstanceOf(EntityInterface::class, $comment->author);
unset($comment->author); // invoke magic __unset()
$this->assertNull($comment->author);

// test re-setting a previously un-set prop
$comment->author = 'manual set';
$this->assertSame('manual set', $comment->author);
}

/**
* tests that lazy loading a previously unset eager loaded property does not
* reload the property
Expand Down

0 comments on commit 1319c1f

Please sign in to comment.