From ad10a18071f9f6b01a2ae56590695ddd54876786 Mon Sep 17 00:00:00 2001 From: Rhodri Pugh Date: Tue, 15 Jul 2014 13:17:22 +0100 Subject: [PATCH] added more informative error messages when invalid parameter count --- lib/Doctrine/ORM/Query.php | 8 ++++++-- lib/Doctrine/ORM/Query/QueryException.php | 18 ++++++++++++++++-- .../Tests/ORM/Functional/QueryTest.php | 16 ++++++++++++++-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ORM/Query.php b/lib/Doctrine/ORM/Query.php index 02a1dc9eb..6839b480d 100644 --- a/lib/Doctrine/ORM/Query.php +++ b/lib/Doctrine/ORM/Query.php @@ -287,9 +287,13 @@ final class Query extends AbstractQuery // Prepare parameters $paramMappings = $this->_parserResult->getParameterMappings(); + $paramCount = count($this->parameters); + $mappingCount = count($paramMappings); - if (count($paramMappings) != count($this->parameters)) { - throw QueryException::invalidParameterNumber(); + if ($paramCount > $mappingCount) { + throw QueryException::tooManyParameters($mappingCount, $paramCount); + } elseif ($paramCount < $mappingCount) { + throw QueryException::tooFewParameters($mappingCount, $paramCount); } // evict all cache for the entity region diff --git a/lib/Doctrine/ORM/Query/QueryException.php b/lib/Doctrine/ORM/Query/QueryException.php index da0d2d50c..f2fb61c71 100644 --- a/lib/Doctrine/ORM/Query/QueryException.php +++ b/lib/Doctrine/ORM/Query/QueryException.php @@ -93,11 +93,25 @@ class QueryException extends \Doctrine\ORM\ORMException } /** + * @param integer $expected + * @param integer $received + * * @return QueryException */ - public static function invalidParameterNumber() + public static function tooManyParameters($expected, $received) { - return new self("Invalid parameter number: number of bound variables does not match number of tokens"); + return new self('Too many parameters: the query defines ' . $expected . ' parameters and you bound ' . $received); + } + + /** + * @param integer $expected + * @param integer $received + * + * @return QueryException + */ + public static function tooFewParameters($expected, $received) + { + return new self('Too few parameters: the query defines ' . $expected . ' parameters but you only bound ' . $received); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php index 795deba19..2e6dbc3c6 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php @@ -127,11 +127,11 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $user = $q->getSingleResult(); } - public function testMismatchingParamExpectedParamCount() + public function testTooManyParametersShouldThrowException() { $this->setExpectedException( "Doctrine\ORM\Query\QueryException", - "Invalid parameter number: number of bound variables does not match number of tokens" + "Too many parameters: the query defines 1 parameters and you bound 2" ); $q = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?1'); @@ -141,6 +141,18 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $user = $q->getSingleResult(); } + public function testTooFewParametersShouldThrowException() + { + $this->setExpectedException( + "Doctrine\ORM\Query\QueryException", + "Too few parameters: the query defines 1 parameters but you only bound 0" + ); + + $q = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?1'); + + $user = $q->getSingleResult(); + } + public function testInvalidInputParameterThrowsException() { $this->setExpectedException("Doctrine\ORM\Query\QueryException");