From 5bb5a7bf47df5bd98f9cf0f166643022ee43dd51 Mon Sep 17 00:00:00 2001 From: Tavo Nieves J Date: Sat, 12 Sep 2020 20:41:17 -0500 Subject: [PATCH] Added seeNumRecords function --- documentation.md | 16 ++++++++++++ src/Codeception/Module/Symfony.php | 39 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/documentation.md b/documentation.md index a7cbf282..a9d3f016 100644 --- a/documentation.md +++ b/documentation.md @@ -1135,6 +1135,22 @@ $I->seeLink('Logout','/logout'); // matches Logout * `param string` $url optional +### seeNumRecords + +Checks that number of given records were found in database. +'id' is the default search parameter. + +```php +seeNumRecords(1, User::class, ['name' => 'davert']); +$I->seeNumRecords(80, User::class); +``` + + * `param int` $expectedNum Expected number of records + * `param string` $className A doctrine entity + * `param array` $criteria Optional query criteria + + ### seeNumberOfElements Checks that there are a certain number of elements matched by the given locator on the page. diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 39756533..1a0c6a9f 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -770,4 +770,43 @@ private function getPossibleKernelClasses() return [$this->config['kernel_class']]; } + + /** + * Checks that number of given records were found in database. + * 'id' is the default search parameter. + * + * ```php + * seeNumRecords(1, User::class, ['name' => 'davert']); + * $I->seeNumRecords(80, User::class); + * ``` + * + * @param int $expectedNum Expected number of records + * @param string $className A doctrine entity + * @param array $criteria Optional query criteria + */ + public function seeNumRecords($expectedNum, $className, $criteria = []) + { + $em = $this->_getEntityManager(); + $repository = $em->getRepository($className); + + if (empty($criteria)) { + $currentNum = (int) $repository->createQueryBuilder('a') + ->select('count(a.id)') + ->getQuery() + ->getSingleScalarResult() + ; + } else { + $currentNum = $repository->count($criteria); + } + + $this->assertEquals( + $expectedNum, + $currentNum, + sprintf( + 'The number of found %s (%d) does not match expected number %d with %s', + $className, $currentNum, $expectedNum, json_encode($criteria) + ) + ); + } }