Skip to content

Commit 42f8d3a

Browse files
authored
Test limits (#1703)
1 parent 02bdb05 commit 42f8d3a

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

src/DataCollector/QueryCollector.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class QueryCollector extends PDOCollector
1717
protected $queries = [];
1818
protected $queryCount = 0;
1919
protected $transactionEventsCount = 0;
20+
protected $infoStatements = 0;
2021
protected $softLimit = null;
2122
protected $hardLimit = null;
2223
protected $lastMemoryUsage;
@@ -472,6 +473,7 @@ public function reset()
472473
{
473474
$this->queries = [];
474475
$this->queryCount = 0;
476+
$this->infoStatements = 0 ;
475477
}
476478

477479
/**
@@ -562,6 +564,7 @@ public function collect()
562564
'sql' => '... ' . ($this->queryCount - $this->hardLimit) . ' additional queries are executed but now shown because of Debugbar query limits. Limits can be raised in the config (debugbar.options.db.soft/hard_limit)',
563565
'type' => 'info',
564566
];
567+
$this->infoStatements+= 2;
565568
} elseif ($this->hardLimit && $this->queryCount > $this->hardLimit) {
566569
array_unshift($statements, [
567570
'sql' => '# Query hard limit for Debugbar is reached after ' . $this->hardLimit . ' queries, additional ' . ($this->queryCount - $this->hardLimit) . ' queries are not shown.. Limits can be raised in the config (debugbar.options.db.hard_limit)',
@@ -571,17 +574,20 @@ public function collect()
571574
'sql' => '... ' . ($this->queryCount - $this->hardLimit) . ' additional queries are executed but now shown because of Debugbar query limits. Limits can be raised in the config (debugbar.options.db.hard_limit)',
572575
'type' => 'info',
573576
];
577+
$this->infoStatements+= 2;
574578
} elseif ($this->softLimit && $this->queryCount > $this->softLimit) {
575579
array_unshift($statements, [
576580
'sql' => '# Query soft limit for Debugbar is reached after ' . $this->softLimit . ' queries, additional ' . ($this->queryCount - $this->softLimit) . ' queries only show the query. Limit can be raised in the config. Limits can be raised in the config (debugbar.options.db.soft_limit)',
577581
'type' => 'info',
578582
]);
583+
$this->infoStatements++;
579584
}
580585

586+
$visibleStatements = count($statements) - $this->infoStatements;
581587
$data = [
582588
'nb_statements' => $this->queryCount,
583-
'nb_visible_statements' => count($statements),
584-
'nb_excluded_statements' => $this->queryCount + $this->transactionEventsCount - count($statements),
589+
'nb_visible_statements' => $visibleStatements,
590+
'nb_excluded_statements' => $this->queryCount + $this->transactionEventsCount - $visibleStatements,
585591
'nb_failed_statements' => 0,
586592
'accumulated_duration' => $totalTime,
587593
'accumulated_duration_str' => $this->formatDuration($totalTime),
@@ -613,7 +619,7 @@ public function getWidgets()
613619
"default" => "[]"
614620
],
615621
"queries:badge" => [
616-
"map" => "queries.nb_visible_statements",
622+
"map" => "queries.nb_statements",
617623
"default" => 0
618624
]
619625
];
@@ -630,7 +636,7 @@ private function getSqlQueryToDisplay(array $query): string
630636
// Continue using the old substitute
631637
}
632638
}
633-
639+
634640
if ($query['type'] === 'query' && $this->renderSqlWithParams) {
635641
$bindings = $this->getDataFormatter()->checkBindings($query['bindings']);
636642
if (!empty($bindings)) {

tests/DebugbarBrowserTest.php

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ protected function addWebRoutes(Router $router)
6868
}
6969
]);
7070

71-
$router->get('web/query', [
72-
'uses' => function () {
71+
$router->get('web/query/{num?}', [
72+
'uses' => function ($num = 1) {
7373
debugbar()->boot();
7474

7575
/** @var Connection $connection */
@@ -81,8 +81,10 @@ protected function addWebRoutes(Router $router)
8181
],
8282
);
8383

84-
$executedQuery = new QueryExecuted('SELECT * FROM users WHERE username = ?', ['debuguser'], 0, $connection);
85-
event($executedQuery);
84+
foreach (range(1, $num) as $i) {
85+
$executedQuery = new QueryExecuted('SELECT * FROM users WHERE username = ?', ['debuguser' . $i], 0, $connection);
86+
event($executedQuery);
87+
}
8688

8789
return 'PONG';
8890
}
@@ -182,7 +184,7 @@ public function testDatabaseCollectsQueries()
182184
$browser->visit('web/query')
183185
->waitFor('.phpdebugbar')
184186
->click('.phpdebugbar-tab-history')
185-
->assertSeeIn('.phpdebugbar-tab[data-collector="queries"] .phpdebugbar-badge', 2)
187+
->waitForTextIn('.phpdebugbar-tab[data-collector="queries"] .phpdebugbar-badge', 1)
186188
->click('.phpdebugbar-tab[data-collector="queries"]')
187189
->screenshotElement('.phpdebugbar', 'queries-tab')
188190
->waitForText('executed')
@@ -198,4 +200,45 @@ public function testDatabaseCollectsQueries()
198200
->screenshotElement('.phpdebugbar', 'queries-expanded');
199201
});
200202
}
203+
204+
205+
public function testDatabaseCollectsQueriesWithSoftLimit()
206+
{
207+
if (version_compare($this->app->version(), '10', '<')) {
208+
$this->markTestSkipped('This test is not compatible with Laravel 9.x and below');
209+
}
210+
211+
$this->browse(function (Browser $browser) {
212+
$browser->visit('web/query/200')
213+
->waitFor('.phpdebugbar')
214+
->click('.phpdebugbar-tab-history')
215+
->waitForTextIn('.phpdebugbar-tab[data-collector="queries"] .phpdebugbar-badge', 200, 30)
216+
->click('.phpdebugbar-tab[data-collector="queries"]')
217+
->screenshotElement('.phpdebugbar', 'queries-tab')
218+
->waitForText('executed')
219+
->waitForText('200 statements were executed (100 duplicates)')
220+
->waitForText('Query soft limit for Debugbar is reached after 100 queries, additional 100 queries only show the query.')
221+
->screenshotElement('.phpdebugbar', 'queries-expanded');
222+
});
223+
}
224+
225+
public function testDatabaseCollectsQueriesWithHardLimit()
226+
{
227+
if (version_compare($this->app->version(), '10', '<')) {
228+
$this->markTestSkipped('This test is not compatible with Laravel 9.x and below');
229+
}
230+
231+
$this->browse(function (Browser $browser) {
232+
$browser->visit('web/query/600')
233+
->waitFor('.phpdebugbar')
234+
->click('.phpdebugbar-tab-history')
235+
->waitForTextIn('.phpdebugbar-tab[data-collector="queries"] .phpdebugbar-badge', 600)
236+
->click('.phpdebugbar-tab[data-collector="queries"]')
237+
->screenshotElement('.phpdebugbar', 'queries-tab')
238+
->waitForText('executed')
239+
->waitForText('600 statements were executed, 100 have been excluded (400 duplicates)')
240+
->waitForText('Query soft and hard limit for Debugbar are reached. Only the first 100 queries show details. Queries after the first 500 are ignored. ')
241+
->screenshotElement('.phpdebugbar', 'queries-expanded');
242+
});
243+
}
201244
}

0 commit comments

Comments
 (0)