Skip to content

Commit

Permalink
added unit test for invoking EntityTrait::_unset()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Horvath committed Feb 25, 2020
1 parent e11be0e commit 3215019
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/TestCase/ORM/LazyLoadEntityTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,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 3215019

Please sign in to comment.