1
0
mirror of synced 2025-01-19 15:01:40 +03:00

Merge pull request #1082 from scotam/invalid-parameter-count

added more informative error messages when invalid parameter count
This commit is contained in:
Guilherme Blanco 2014-07-15 11:06:53 -04:00
commit c019047d6c
3 changed files with 36 additions and 6 deletions

View File

@ -287,9 +287,13 @@ final class Query extends AbstractQuery
// Prepare parameters // Prepare parameters
$paramMappings = $this->_parserResult->getParameterMappings(); $paramMappings = $this->_parserResult->getParameterMappings();
$paramCount = count($this->parameters);
$mappingCount = count($paramMappings);
if (count($paramMappings) != count($this->parameters)) { if ($paramCount > $mappingCount) {
throw QueryException::invalidParameterNumber(); throw QueryException::tooManyParameters($mappingCount, $paramCount);
} elseif ($paramCount < $mappingCount) {
throw QueryException::tooFewParameters($mappingCount, $paramCount);
} }
// evict all cache for the entity region // evict all cache for the entity region

View File

@ -93,11 +93,25 @@ class QueryException extends \Doctrine\ORM\ORMException
} }
/** /**
* @param integer $expected
* @param integer $received
*
* @return QueryException * @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);
} }
/** /**

View File

@ -127,11 +127,11 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$user = $q->getSingleResult(); $user = $q->getSingleResult();
} }
public function testMismatchingParamExpectedParamCount() public function testTooManyParametersShouldThrowException()
{ {
$this->setExpectedException( $this->setExpectedException(
"Doctrine\ORM\Query\QueryException", "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'); $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(); $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() public function testInvalidInputParameterThrowsException()
{ {
$this->setExpectedException("Doctrine\ORM\Query\QueryException"); $this->setExpectedException("Doctrine\ORM\Query\QueryException");