Skip to content

Commit 9855225

Browse files
committed
Narrow return types for AbstractQuery::getSingleScalarResult()
1 parent 8fba9d6 commit 9855225

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/Doctrine/ORM/AbstractQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ public function getSingleResult($hydrationMode = null)
10101010
*
10111011
* Alias for getSingleResult(HYDRATE_SINGLE_SCALAR).
10121012
*
1013-
* @return mixed The scalar result.
1013+
* @return bool|float|int|string The scalar result.
10141014
*
10151015
* @throws NoResultException If the query returned no result.
10161016
* @throws NonUniqueResultException If the query result is not unique.

tests/Doctrine/Tests/ORM/Functional/QueryTest.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,34 @@ public function testGetSingleResultThrowsExceptionOnNoResult(): void
486486
public function testGetSingleScalarResultThrowsExceptionOnNoResult(): void
487487
{
488488
$this->expectException('Doctrine\ORM\NoResultException');
489-
$this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a')
489+
$this->_em->createQuery('select a.id from Doctrine\Tests\Models\CMS\CmsArticle a')
490+
->getSingleScalarResult();
491+
}
492+
493+
public function testGetSingleScalarResultThrowsExceptionOnSingleRowWithMultipleColumns(): void
494+
{
495+
$user = new CmsUser();
496+
$user->name = 'Javier';
497+
$user->username = 'phansys';
498+
$user->status = 'developer';
499+
500+
$this->_em->persist($user);
501+
502+
$this->_em->flush();
503+
$this->_em->clear();
504+
505+
$this->expectException(NonUniqueResultException::class);
506+
$this->expectExceptionMessage(
507+
'The query returned a row containing multiple columns. Change the query or use a different result function'
508+
. ' like getScalarResult().'
509+
);
510+
511+
$this->_em->createQuery('select u from Doctrine\Tests\Models\CMS\CmsUser u')
490512
->getSingleScalarResult();
491513
}
492514

493515
public function testGetSingleScalarResultThrowsExceptionOnNonUniqueResult(): void
494516
{
495-
$this->expectException('Doctrine\ORM\NonUniqueResultException');
496517
$user = new CmsUser();
497518
$user->name = 'Guilherme';
498519
$user->username = 'gblanco';
@@ -515,7 +536,12 @@ public function testGetSingleScalarResultThrowsExceptionOnNonUniqueResult(): voi
515536
$this->_em->flush();
516537
$this->_em->clear();
517538

518-
$this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a')
539+
$this->expectException(NonUniqueResultException::class);
540+
$this->expectExceptionMessage(
541+
'The query returned multiple rows. Change the query or use a different result function like getScalarResult().'
542+
);
543+
544+
$this->_em->createQuery('select a.id from Doctrine\Tests\Models\CMS\CmsArticle a')
519545
->getSingleScalarResult();
520546
}
521547

0 commit comments

Comments
 (0)