From c18398ed74f01d7d809e957bd905641deda88ba9 Mon Sep 17 00:00:00 2001 From: Jason Horvath Date: Tue, 25 Feb 2020 10:07:36 -0500 Subject: [PATCH 1/3] Update deprecated Entity::unsetProperty() to use 4.x Entity::unset() --- src/ORM/LazyLoadEntityTrait.php | 4 ++-- tests/TestCase/ORM/LazyLoadEntityTraitTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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..cbeebd0 100644 --- a/tests/TestCase/ORM/LazyLoadEntityTraitTest.php +++ b/tests/TestCase/ORM/LazyLoadEntityTraitTest.php @@ -116,7 +116,7 @@ public function testTablelessEntity() * * @return void */ - public function testUnsetProperty() + public function testUnset() { $this->Comments = $this->getTableLocator()->get('Comments'); $this->Comments->belongsTo('Authors', [ @@ -134,7 +134,7 @@ public function testUnsetProperty() ->will($this->returnValue($this->Comments)); $this->assertInstanceOf(EntityInterface::class, $comment->author); - $comment->unsetProperty('author'); + $comment->unset('author'); $this->assertNull($comment->author); // test re-setting a previously un-set prop From e11be0e91a5f07ff43da9c56fde7e080d0a8218d Mon Sep 17 00:00:00 2001 From: Jason Horvath Date: Tue, 25 Feb 2020 12:33:02 -0500 Subject: [PATCH 2/3] added back testUnsetProperty to ensure unset via proxy --- .../TestCase/ORM/LazyLoadEntityTraitTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/TestCase/ORM/LazyLoadEntityTraitTest.php b/tests/TestCase/ORM/LazyLoadEntityTraitTest.php index cbeebd0..d755dde 100644 --- a/tests/TestCase/ORM/LazyLoadEntityTraitTest.php +++ b/tests/TestCase/ORM/LazyLoadEntityTraitTest.php @@ -142,6 +142,38 @@ public function testUnset() $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'); + $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->unsetProperty('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 lazy loading a previously unset eager loaded property does not * reload the property From 32150195c5d70ffcc5860e6cbc2c4899ae0c1d6b Mon Sep 17 00:00:00 2001 From: Jason Horvath Date: Tue, 25 Feb 2020 15:21:30 -0500 Subject: [PATCH 3/3] added unit test for invoking EntityTrait::_unset() --- .../TestCase/ORM/LazyLoadEntityTraitTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/TestCase/ORM/LazyLoadEntityTraitTest.php b/tests/TestCase/ORM/LazyLoadEntityTraitTest.php index d755dde..642fada 100644 --- a/tests/TestCase/ORM/LazyLoadEntityTraitTest.php +++ b/tests/TestCase/ORM/LazyLoadEntityTraitTest.php @@ -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