From f9a605f6ca1de37a8abf70e33fd363f7e3191c4c Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Thu, 17 Nov 2016 17:23:22 +0100 Subject: [PATCH 1/3] Add details about invalid Connection passed at creation. --- lib/Doctrine/ORM/EntityManager.php | 2 +- .../Doctrine/Tests/ORM/EntityManagerTest.php | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 4de4932bc..112502b5a 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -860,7 +860,7 @@ use Doctrine\Common\Util\ClassUtils; } if ( ! $connection instanceof Connection) { - throw new \InvalidArgumentException("Invalid argument: " . $connection); + throw new \InvalidArgumentException(sprintf('Invalid argument for connection "%s".', is_object($connection) ? get_class($connection) : gettype($connection) . '#' . $connection)); } if ($eventManager !== null && $connection->getEventManager() !== $eventManager) { diff --git a/tests/Doctrine/Tests/ORM/EntityManagerTest.php b/tests/Doctrine/Tests/ORM/EntityManagerTest.php index cf6fc99a4..1b08fb79e 100644 --- a/tests/Doctrine/Tests/ORM/EntityManagerTest.php +++ b/tests/Doctrine/Tests/ORM/EntityManagerTest.php @@ -2,6 +2,9 @@ namespace Doctrine\Tests\ORM; +use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; +use Doctrine\ORM\Configuration; +use Doctrine\ORM\EntityManager; use Doctrine\ORM\ORMException; use Doctrine\ORM\ORMInvalidArgumentException; use Doctrine\ORM\Query\ResultSetMapping; @@ -72,9 +75,9 @@ class EntityManagerTest extends OrmTestCase { $rsm = new ResultSetMapping(); $this->_em->getConfiguration()->addNamedNativeQuery('foo', 'SELECT foo', $rsm); - + $query = $this->_em->createNamedNativeQuery('foo'); - + $this->assertInstanceOf('Doctrine\ORM\NativeQuery', $query); } @@ -116,14 +119,14 @@ class EntityManagerTest extends OrmTestCase $this->assertInstanceOf('Doctrine\ORM\Query', $q); $this->assertEquals('SELECT 1', $q->getDql()); } - + /** * @covers Doctrine\ORM\EntityManager::createNamedQuery */ public function testCreateNamedQuery() { $this->_em->getConfiguration()->addNamedQuery('foo', 'SELECT 1'); - + $query = $this->_em->createNamedQuery('foo'); $this->assertInstanceOf('Doctrine\ORM\Query', $query); $this->assertEquals('SELECT 1', $query->getDql()); @@ -204,4 +207,15 @@ class EntityManagerTest extends OrmTestCase $this->assertSame($this->_em, $em); return 'callback'; } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessageRegExp /^Invalid argument for connection \"integer#1\".$/ + */ + public function testCreateInvalidConnection() + { + $config = new Configuration(); + $config->setMetadataDriverImpl($this->createMock(MappingDriver::class)); + EntityManager::create(1, $config); + } } From e37041aa942f52386586773d7304b50e069e706d Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Fri, 18 Nov 2016 08:06:11 +0100 Subject: [PATCH 2/3] Update message. --- lib/Doctrine/ORM/EntityManager.php | 8 +++++++- tests/Doctrine/Tests/ORM/EntityManagerTest.php | 7 +++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 112502b5a..51d6dbf05 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -860,7 +860,13 @@ use Doctrine\Common\Util\ClassUtils; } if ( ! $connection instanceof Connection) { - throw new \InvalidArgumentException(sprintf('Invalid argument for connection "%s".', is_object($connection) ? get_class($connection) : gettype($connection) . '#' . $connection)); + throw new \InvalidArgumentException( + sprintf( + 'Invalid $connection argument of type %s given%s.', + is_object($connection) ? get_class($connection) : gettype($connection), + is_object($connection) ? '' : ': "' . $connection . '"' + ) + ); } if ($eventManager !== null && $connection->getEventManager() !== $eventManager) { diff --git a/tests/Doctrine/Tests/ORM/EntityManagerTest.php b/tests/Doctrine/Tests/ORM/EntityManagerTest.php index 1b08fb79e..ec7f1301f 100644 --- a/tests/Doctrine/Tests/ORM/EntityManagerTest.php +++ b/tests/Doctrine/Tests/ORM/EntityManagerTest.php @@ -208,12 +208,11 @@ class EntityManagerTest extends OrmTestCase return 'callback'; } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessageRegExp /^Invalid argument for connection \"integer#1\".$/ - */ public function testCreateInvalidConnection() { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageRegExp('/^Invalid \$connection argument of type integer given: \"1\".$/'); + $config = new Configuration(); $config->setMetadataDriverImpl($this->createMock(MappingDriver::class)); EntityManager::create(1, $config); From d2643eeb8b445ad8a18982121833c29b2c3757d8 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 18 Nov 2016 09:19:27 +0100 Subject: [PATCH 3/3] #6136 Removed regex assertion - constant assertion is sufficient --- tests/Doctrine/Tests/ORM/EntityManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/EntityManagerTest.php b/tests/Doctrine/Tests/ORM/EntityManagerTest.php index ec7f1301f..d0cf2bcbb 100644 --- a/tests/Doctrine/Tests/ORM/EntityManagerTest.php +++ b/tests/Doctrine/Tests/ORM/EntityManagerTest.php @@ -211,7 +211,7 @@ class EntityManagerTest extends OrmTestCase public function testCreateInvalidConnection() { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/^Invalid \$connection argument of type integer given: \"1\".$/'); + $this->expectExceptionMessage('Invalid $connection argument of type integer given: "1".'); $config = new Configuration(); $config->setMetadataDriverImpl($this->createMock(MappingDriver::class));