Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af3071a

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

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

lib/Doctrine/ORM/AbstractQuery.php

Lines changed: 1 addition & 1 deletion
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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,35 @@ 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')
490490
->getSingleScalarResult();
491491
}
492492

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')
512+
->setMaxResults(1)
513+
->getSingleScalarResult();
514+
}
515+
493516
public function testGetSingleScalarResultThrowsExceptionOnNonUniqueResult(): void
494517
{
495-
$this->expectException('Doctrine\ORM\NonUniqueResultException');
496518
$user = new CmsUser();
497519
$user->name = 'Guilherme';
498520
$user->username = 'gblanco';
@@ -515,7 +537,12 @@ public function testGetSingleScalarResultThrowsExceptionOnNonUniqueResult(): voi
515537
$this->_em->flush();
516538
$this->_em->clear();
517539

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

0 commit comments

Comments
 (0)