Skip to content

Commit 344c0d8

Browse files
[9.x] Fix expectsDatabaseQueryCount() $connection parameter (#46228)
* 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]>
1 parent 9239128 commit 344c0d8

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Support\Jsonable;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\SoftDeletes;
8+
use Illuminate\Database\Events\QueryExecuted;
89
use Illuminate\Support\Arr;
910
use Illuminate\Support\Facades\DB;
1011
use Illuminate\Testing\Constraints\CountInDatabase;
@@ -177,15 +178,21 @@ protected function assertModelMissing($model)
177178
*/
178179
public function expectsDatabaseQueryCount($expected, $connection = null)
179180
{
180-
with($this->getConnection($connection), function ($connection) use ($expected) {
181+
with($this->getConnection($connection), function ($connectionInstance) use ($expected, $connection) {
181182
$actual = 0;
182183

183-
$connection->listen(function () use (&$actual) {
184-
$actual++;
184+
$connectionInstance->listen(function (QueryExecuted $event) use (&$actual, $connectionInstance, $connection) {
185+
if (is_null($connection) || $connectionInstance === $event->connection) {
186+
$actual++;
187+
}
185188
});
186189

187-
$this->beforeApplicationDestroyed(function () use (&$actual, $expected, $connection) {
188-
$this->assertSame($actual, $expected, "Expected {$expected} database queries on the [{$connection->getName()}] connection. {$actual} occurred.");
190+
$this->beforeApplicationDestroyed(function () use (&$actual, $expected, $connectionInstance) {
191+
$this->assertSame(
192+
$actual,
193+
$expected,
194+
"Expected {$expected} database queries on the [{$connectionInstance->getName()}] connection. {$actual} occurred."
195+
);
189196
});
190197
});
191198

tests/Foundation/FoundationInteractsWithDatabaseTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,31 @@ public function testExpectsDatabaseQueryCount()
385385
} catch (ExpectationFailedException $e) {
386386
$this->assertSame("Expected 3 database queries on the [testing] connection. 4 occurred.\nFailed asserting that 3 is identical to 4.", $e->getMessage());
387387
}
388+
389+
$case = new class extends TestingTestCase
390+
{
391+
use CreatesApplication;
392+
393+
public function testExpectsDatabaseQueryCount()
394+
{
395+
$this->expectsDatabaseQueryCount(4);
396+
$this->expectsDatabaseQueryCount(1, 'mysql');
397+
398+
DB::pretend(function ($db) {
399+
$db->table('foo')->count();
400+
$db->table('foo')->count();
401+
$db->table('foo')->count();
402+
});
403+
404+
DB::connection('mysql')->pretend(function ($db) {
405+
$db->table('foo')->count();
406+
});
407+
}
408+
};
409+
410+
$case->setUp();
411+
$case->testExpectsDatabaseQueryCount();
412+
$case->tearDown();
388413
}
389414

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

0 commit comments

Comments
 (0)