Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Fix expectsDatabaseQueryCount() $connection parameter #46228

Merged
merged 2 commits into from
Feb 23, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Testing\Constraints\CountInDatabase;
Expand Down Expand Up @@ -177,15 +178,21 @@ protected function assertModelMissing($model)
*/
public function expectsDatabaseQueryCount($expected, $connection = null)
{
with($this->getConnection($connection), function ($connection) use ($expected) {
with($this->getConnection($connection), function ($connectionInstance) use ($expected, $connection) {
$actual = 0;

$connection->listen(function () use (&$actual) {
$actual++;
$connectionInstance->listen(function (QueryExecuted $event) use (&$actual, $connectionInstance, $connection) {
if (is_null($connection) || $connectionInstance === $event->connection) {
$actual++;
}
});

$this->beforeApplicationDestroyed(function () use (&$actual, $expected, $connection) {
$this->assertSame($actual, $expected, "Expected {$expected} database queries on the [{$connection->getName()}] connection. {$actual} occurred.");
$this->beforeApplicationDestroyed(function () use (&$actual, $expected, $connectionInstance) {
$this->assertSame(
$actual,
$expected,
"Expected {$expected} database queries on the [{$connectionInstance->getName()}] connection. {$actual} occurred."
);
});
});

Expand Down
25 changes: 25 additions & 0 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,31 @@ public function testExpectsDatabaseQueryCount()
} catch (ExpectationFailedException $e) {
$this->assertSame("Expected 3 database queries on the [testing] connection. 4 occurred.\nFailed asserting that 3 is identical to 4.", $e->getMessage());
}

$case = new class extends TestingTestCase
{
use CreatesApplication;

public function testExpectsDatabaseQueryCount()
{
$this->expectsDatabaseQueryCount(4);
$this->expectsDatabaseQueryCount(1, 'mysql');

DB::pretend(function ($db) {
$db->table('foo')->count();
$db->table('foo')->count();
$db->table('foo')->count();
});

DB::connection('mysql')->pretend(function ($db) {
$db->table('foo')->count();
});
}
};

$case->setUp();
$case->testExpectsDatabaseQueryCount();
$case->tearDown();
}

protected function mockCountBuilder($countResult, $deletedAtColumn = 'deleted_at')
Expand Down