Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,22 @@ $I->seeLink('Logout','/logout'); // matches <a href="/logout">Logout</a>
* `param string` $url optional


### seeNumRecords

Checks that number of given records were found in database.
'id' is the default search parameter.

```php
<?php
$I->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.
Expand Down
39 changes: 39 additions & 0 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <?php
* $I->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)
)
);
}
}