diff --git a/src/ORM/LazyLoadEntityTrait.php b/src/ORM/LazyLoadEntityTrait.php index 0068b54..87265f8 100644 --- a/src/ORM/LazyLoadEntityTrait.php +++ b/src/ORM/LazyLoadEntityTrait.php @@ -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); } /** diff --git a/tests/TestCase/ORM/LazyLoadEntityTraitTest.php b/tests/TestCase/ORM/LazyLoadEntityTraitTest.php index 1ecf704..642fada 100644 --- a/tests/TestCase/ORM/LazyLoadEntityTraitTest.php +++ b/tests/TestCase/ORM/LazyLoadEntityTraitTest.php @@ -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'); @@ -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