From 2a7d21ad181f1ccbb18bf4fc1d7851d913b56dd7 Mon Sep 17 00:00:00 2001
From: Jeremy Benoist <jeremy.benoist@gmail.com>
Date: Mon, 12 Sep 2016 12:08:42 +0200
Subject: [PATCH] Throw an exception instead of a workaround

---
 lib/Doctrine/ORM/UnitOfWork.php             |  8 ++++----
 tests/Doctrine/Tests/ORM/UnitOfWorkTest.php | 16 ++++++++++++++--
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php
index bc8d09fb8..1340dc891 100644
--- a/lib/Doctrine/ORM/UnitOfWork.php
+++ b/lib/Doctrine/ORM/UnitOfWork.php
@@ -2363,6 +2363,10 @@ class UnitOfWork implements PropertyChangedListener
      */
     public function clear($entityName = null)
     {
+        if ($entityName !== null && !is_string($entityName)) {
+            throw new \InvalidArgumentException(sprintf('Argument 1 passed to %s() must be a string, %s given', __METHOD__, gettype($entityName)));
+        }
+
         if ($entityName === null) {
             $this->identityMap =
             $this->entityIdentifiers =
@@ -3442,10 +3446,6 @@ class UnitOfWork implements PropertyChangedListener
      */
     private function clearIdentityMapForEntityName($entityName)
     {
-        if (is_object($entityName)) {
-            return;
-        }
-
         if (! isset($this->identityMap[$entityName])) {
             return;
         }
diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php
index 84df1073d..def2ae91a 100644
--- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php
+++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php
@@ -351,16 +351,28 @@ class UnitOfWorkTest extends OrmTestCase
 
     public function testClearManagerWithObject()
     {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionMessage('must be a string');
+
         $entity = new Country(456, 'United Kingdom');
 
         $this->_unitOfWork->persist($entity);
         $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity));
 
         $this->_unitOfWork->clear($entity);
+    }
 
-        // true because entity wasn't a string so it wasn't cleared
+    public function testClearManagerWithNullValue()
+    {
+        $entity = new Country(456, 'United Kingdom');
+
+        $this->_unitOfWork->persist($entity);
         $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity));
-        $this->assertTrue($this->_unitOfWork->isScheduledForInsert($entity));
+
+        $this->_unitOfWork->clear();
+
+        $this->assertFalse($this->_unitOfWork->isInIdentityMap($entity));
+        $this->assertFalse($this->_unitOfWork->isScheduledForInsert($entity));
     }
 
     /**