PHP code example of mattiasgeniar / phpunit-query-count-assertions
1. Go to this page and download the library: Download mattiasgeniar/phpunit-query-count-assertions library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mattiasgeniar / phpunit-query-count-assertions example snippets
use Mattiasgeniar\PhpunitQueryCountAssertions\AssertsQueryCounts;
class YourTest extends TestCase
{
use AssertsQueryCounts;
public function setUp(): void
{
parent::setUp();
AssertsQueryCounts::trackQueries();
}
/** @test */
public function your_tests_go_here()
{
$this->assertNoQueriesExecuted();
}
}
$this->assertNoQueriesExecuted(); // No queries should have been run
$this->assertQueryCountMatches(5); // Query count should be exactly 5
$this->assertQueryCountLessThan(6); // Should be less than 6 queries
$this->assertQueryCountGreaterThan(4); // Should be more than 4 queries
$this->assertQueryCountMatches(2, function() {
// assertion will pass if exactly 2 queries happen here.
});