From db897a0812c888365b0e89b2a148168f23c2c199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Ostroluck=C3=BD?= Date: Sun, 30 Apr 2017 22:20:49 +0200 Subject: [PATCH] Fix __call exception when no metadata has been set This makes sure $cm is set before trying to call it in catch block closes #139 --- lib/Doctrine/Common/Persistence/PersistentObject.php | 12 ++++-------- .../Common/Persistence/PersistentObjectTest.php | 7 +++++++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/Common/Persistence/PersistentObject.php b/lib/Doctrine/Common/Persistence/PersistentObject.php index 990642e78..f90b0ccca 100644 --- a/lib/Doctrine/Common/Persistence/PersistentObject.php +++ b/lib/Doctrine/Common/Persistence/PersistentObject.php @@ -119,8 +119,6 @@ public function injectObjectManager(ObjectManager $objectManager, ClassMetadata */ private function set($field, $args) { - $this->initializeDoctrine(); - if ($this->cm->hasField($field) && !$this->cm->isIdentifier($field)) { $this->$field = $args[0]; } else if ($this->cm->hasAssociation($field) && $this->cm->isSingleValuedAssociation($field)) { @@ -146,13 +144,11 @@ private function set($field, $args) */ private function get($field) { - $this->initializeDoctrine(); - if ( $this->cm->hasField($field) || $this->cm->hasAssociation($field) ) { return $this->$field; - } else { - throw new \BadMethodCallException("no field with name '".$field."' exists on '".$this->cm->getName()."'"); } + + throw new \BadMethodCallException("no field with name '".$field."' exists on '".$this->cm->getName()."'"); } /** @@ -190,8 +186,6 @@ private function completeOwningSide($field, $targetClass, $targetObject) */ private function add($field, $args) { - $this->initializeDoctrine(); - if ($this->cm->hasAssociation($field) && $this->cm->isCollectionValuedAssociation($field)) { $targetClass = $this->cm->getAssociationTargetClass($field); if (!($args[0] instanceof $targetClass)) { @@ -239,6 +233,8 @@ private function initializeDoctrine() */ public function __call($method, $args) { + $this->initializeDoctrine(); + $command = substr($method, 0, 3); $field = lcfirst(substr($method, 3)); if ($command == "set") { diff --git a/tests/Doctrine/Tests/Common/Persistence/PersistentObjectTest.php b/tests/Doctrine/Tests/Common/Persistence/PersistentObjectTest.php index 01ed95baa..f424638d4 100644 --- a/tests/Doctrine/Tests/Common/Persistence/PersistentObjectTest.php +++ b/tests/Doctrine/Tests/Common/Persistence/PersistentObjectTest.php @@ -76,6 +76,13 @@ public function testGetUnknownField() $this->object->getUnknown(); } + public function testUndefinedMethod() + { + $this->expectException(BadMethodCallException::class); + $this->expectExceptionMessage("There is no method"); + (new TestObject)->undefinedMethod(); + } + public function testGetToOneAssociation() { $this->assertNull($this->object->getParent());