Skip to content

Commit c1280aa

Browse files
authored
Merge pull request #19 from TavoNiievez/seeNumRecords
Added seeNumRecords function
2 parents 4d15193 + 5bb5a7b commit c1280aa

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

documentation.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,22 @@ $I->seeLink('Logout','/logout'); // matches <a href="/logout">Logout</a>
11351135
* `param string` $url optional
11361136

11371137

1138+
### seeNumRecords
1139+
1140+
Checks that number of given records were found in database.
1141+
'id' is the default search parameter.
1142+
1143+
```php
1144+
<?php
1145+
$I->seeNumRecords(1, User::class, ['name' => 'davert']);
1146+
$I->seeNumRecords(80, User::class);
1147+
```
1148+
1149+
* `param int` $expectedNum Expected number of records
1150+
* `param string` $className A doctrine entity
1151+
* `param array` $criteria Optional query criteria
1152+
1153+
11381154
### seeNumberOfElements
11391155

11401156
Checks that there are a certain number of elements matched by the given locator on the page.

src/Codeception/Module/Symfony.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,4 +770,43 @@ private function getPossibleKernelClasses()
770770

771771
return [$this->config['kernel_class']];
772772
}
773+
774+
/**
775+
* Checks that number of given records were found in database.
776+
* 'id' is the default search parameter.
777+
*
778+
* ```php
779+
* <?php
780+
* $I->seeNumRecords(1, User::class, ['name' => 'davert']);
781+
* $I->seeNumRecords(80, User::class);
782+
* ```
783+
*
784+
* @param int $expectedNum Expected number of records
785+
* @param string $className A doctrine entity
786+
* @param array $criteria Optional query criteria
787+
*/
788+
public function seeNumRecords($expectedNum, $className, $criteria = [])
789+
{
790+
$em = $this->_getEntityManager();
791+
$repository = $em->getRepository($className);
792+
793+
if (empty($criteria)) {
794+
$currentNum = (int) $repository->createQueryBuilder('a')
795+
->select('count(a.id)')
796+
->getQuery()
797+
->getSingleScalarResult()
798+
;
799+
} else {
800+
$currentNum = $repository->count($criteria);
801+
}
802+
803+
$this->assertEquals(
804+
$expectedNum,
805+
$currentNum,
806+
sprintf(
807+
'The number of found %s (%d) does not match expected number %d with %s',
808+
$className, $currentNum, $expectedNum, json_encode($criteria)
809+
)
810+
);
811+
}
773812
}

0 commit comments

Comments
 (0)