Skip to content

Commit 17f1b81

Browse files
authored
Allow Debugbar to work with runtime connection (#1526)
* Allow Debugbar to work with runtime connection * rename test * different testing strategy for Laravel 9 and Laravel 10 * review
1 parent a629df4 commit 17f1b81

File tree

2 files changed

+96
-10
lines changed

2 files changed

+96
-10
lines changed

src/LaravelDebugbar.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,10 @@ function (\Illuminate\Log\Events\MessageLogged $log) use ($logger) {
288288
}
289289
}
290290

291-
if ($this->shouldCollect('db', true) && isset($this->app['db'])) {
292-
$db = $this->app['db'];
291+
if ($this->shouldCollect('db', true) && isset($this->app['db']) && isset($this->app['events'])) {
292+
/** @var \Illuminate\Events\Dispatcher $events */
293+
$events = $this->app['events'];
294+
293295
if (
294296
$debugbar->hasCollector('time') && $this->app['config']->get(
295297
'debugbar.options.db.timeline',
@@ -336,7 +338,7 @@ function (\Illuminate\Log\Events\MessageLogged $log) use ($logger) {
336338
$this->addCollector($queryCollector);
337339

338340
try {
339-
$db->listen(
341+
$events->listen(
340342
function (\Illuminate\Database\Events\QueryExecuted $query) {
341343
if (!app(static::class)->shouldCollect('db', true)) {
342344
return; // Issue 776 : We've turned off collecting after the listener was attached
@@ -354,49 +356,49 @@ function (\Illuminate\Database\Events\QueryExecuted $query) {
354356
}
355357

356358
try {
357-
$db->getEventDispatcher()->listen(
359+
$events->listen(
358360
\Illuminate\Database\Events\TransactionBeginning::class,
359361
function ($transaction) use ($queryCollector) {
360362
$queryCollector->collectTransactionEvent('Begin Transaction', $transaction->connection);
361363
}
362364
);
363365

364-
$db->getEventDispatcher()->listen(
366+
$events->listen(
365367
\Illuminate\Database\Events\TransactionCommitted::class,
366368
function ($transaction) use ($queryCollector) {
367369
$queryCollector->collectTransactionEvent('Commit Transaction', $transaction->connection);
368370
}
369371
);
370372

371-
$db->getEventDispatcher()->listen(
373+
$events->listen(
372374
\Illuminate\Database\Events\TransactionRolledBack::class,
373375
function ($transaction) use ($queryCollector) {
374376
$queryCollector->collectTransactionEvent('Rollback Transaction', $transaction->connection);
375377
}
376378
);
377379

378-
$db->getEventDispatcher()->listen(
380+
$events->listen(
379381
'connection.*.beganTransaction',
380382
function ($event, $params) use ($queryCollector) {
381383
$queryCollector->collectTransactionEvent('Begin Transaction', $params[0]);
382384
}
383385
);
384386

385-
$db->getEventDispatcher()->listen(
387+
$events->listen(
386388
'connection.*.committed',
387389
function ($event, $params) use ($queryCollector) {
388390
$queryCollector->collectTransactionEvent('Commit Transaction', $params[0]);
389391
}
390392
);
391393

392-
$db->getEventDispatcher()->listen(
394+
$events->listen(
393395
'connection.*.rollingBack',
394396
function ($event, $params) use ($queryCollector) {
395397
$queryCollector->collectTransactionEvent('Rollback Transaction', $params[0]);
396398
}
397399
);
398400

399-
$db->getEventDispatcher()->listen(
401+
$events->listen(
400402
function (\Illuminate\Database\Events\ConnectionEstablished $event) use ($queryCollector) {
401403
$queryCollector->collectTransactionEvent('Connection Established', $event->connection);
402404

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\Tests\DataCollector;
4+
5+
use Barryvdh\Debugbar\Tests\TestCase;
6+
use Illuminate\Database\Connection;
7+
use Illuminate\Database\Events\QueryExecuted;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use Illuminate\Support\Arr;
10+
11+
class QueryCollectorRuntimeDatabaseTest extends TestCase
12+
{
13+
protected function getEnvironmentSetUp($app)
14+
{
15+
$app['config']->set('database.default', null);
16+
17+
$app['config']->set('database.connections', []);
18+
}
19+
20+
public function testCollectsQueriesFromRuntimeConnections()
21+
{
22+
if (version_compare($this->app->version(), '10', '<')) {
23+
$this->markTestSkipped('This test is not compatible with Laravel 9.x and below');
24+
}
25+
26+
debugbar()->boot();
27+
28+
/** @var Connection $connection */
29+
$connection = $this->app['db']->connectUsing(
30+
'runtime-connection',
31+
[
32+
'driver' => 'sqlite',
33+
'database' => ':memory:',
34+
],
35+
);
36+
37+
$connection->statement('SELECT 1');
38+
39+
/** @var \Debugbar\DataCollector\ExceptionsCollector $collector */
40+
$exceptions = debugbar()->getCollector('exceptions');
41+
42+
self::assertEmpty($exceptions->getExceptions());
43+
44+
/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
45+
$collector = debugbar()->getCollector('queries');
46+
47+
tap($collector->collect(), function (array $collection) {
48+
$this->assertEquals(1, $collection['nb_statements']);
49+
50+
self::assertSame('SELECT 1', $collection['statements'][2]['sql']);
51+
});
52+
}
53+
54+
public function testCollectsQueriesFromRuntimeConnectionsWithoutConnectUsing()
55+
{
56+
debugbar()->boot();
57+
58+
$this->app['config']->set('database.connections.dynamic-connection', [
59+
'driver' => 'sqlite',
60+
'database' => ':memory:',
61+
]);
62+
63+
$this->app['config']->set('database.default', 'dynamic-connection');
64+
65+
/** @var Connection $connection */
66+
$connection = $this->app['db']->connection('dynamic-connection');
67+
68+
$connection->statement('SELECT 1');
69+
70+
/** @var \Debugbar\DataCollector\ExceptionsCollector $collector */
71+
$exceptions = debugbar()->getCollector('exceptions');
72+
73+
self::assertEmpty($exceptions->getExceptions());
74+
75+
/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
76+
$collector = debugbar()->getCollector('queries');
77+
78+
tap($collector->collect(), function (array $collection) {
79+
$this->assertEquals(1, $collection['nb_statements']);
80+
81+
self::assertSame('SELECT 1', $collection['statements'][2]['sql']);
82+
});
83+
}
84+
}

0 commit comments

Comments
 (0)