Skip to content

Commit

Permalink
[9.x] Fix expectsDatabaseQueryCount() $connection parameter (#46228)
Browse files Browse the repository at this point in the history
* Fix expectsDatabaseQueryCount() $connection parameter

The listen() function was assuming that the event was being dispatched for just that connection, rather than globally.

Instead, we now check the connection name matches if it was provided.

Fixes #45932

* fixes

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
giggsey and taylorotwell authored Feb 23, 2023
1 parent 9239128 commit 344c0d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
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

0 comments on commit 344c0d8

Please sign in to comment.