From be4aafd4f6aee5558842726cc7d53bc120def342 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 26 Nov 2016 23:12:09 +0100 Subject: [PATCH] Use ORMException instead of a default exception --- lib/Doctrine/ORM/ORMException.php | 14 +++++++++++++- lib/Doctrine/ORM/UnitOfWork.php | 2 +- tests/Doctrine/Tests/ORM/UnitOfWorkTest.php | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 919789d92..edfc47df6 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -101,7 +101,7 @@ class ORMException extends Exception return new self("Unrecognized field: $field"); } - /** + /** * * @param string $class * @param string $association @@ -340,4 +340,16 @@ class ORMException extends Exception { return new self("Can't use IN operator on entities that have composite keys."); } + + /** + * Used when a given entityName hasn't the good type + * + * @param mixed $entityName The given entity (which shouldn't be a string) + * + * @return ORMException + */ + public static function invalidEntityName($entityName) + { + return new self(sprintf('Entity name must be a string, %s given', gettype($entityName))); + } } diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 1340dc891..8fbac956d 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -2364,7 +2364,7 @@ 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))); + throw ORMException::invalidEntityName($entityName); } if ($entityName === null) { diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index def2ae91a..c60de753e 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -8,6 +8,7 @@ use Doctrine\Common\PropertyChangedListener; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\ORMInvalidArgumentException; use Doctrine\ORM\UnitOfWork; +use Doctrine\ORM\ORMException; use Doctrine\Tests\Mocks\ConnectionMock; use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\EntityManagerMock; @@ -351,7 +352,7 @@ class UnitOfWorkTest extends OrmTestCase public function testClearManagerWithObject() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(ORMException::class); $this->expectExceptionMessage('must be a string'); $entity = new Country(456, 'United Kingdom');