Skip to content

Commit

Permalink
fix: issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yalsicor committed Sep 29, 2024
1 parent 508a950 commit c52815a
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/Traits/TestTraits/PhpUnit/TestDatabaseProfilerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,18 @@ protected function assertDatabaseExecutedQuery(string $expectedQuery): void
*/
protected function assertDatabaseExecutedQueries(array $expectedQueries): void
{
$queries = $this->getDatabaseQueries();

foreach ($expectedQueries as $expectedQuery) {
$this->assertDatabaseExecutedQuery($expectedQuery);
$found = false;
foreach ($queries as $query) {
if (str_contains($query['query'], $expectedQuery)) {
$found = true;
break;
}
}

$this->assertTrue($found, "Expected query '$expectedQuery' not found in database queries.");
}
}

Expand All @@ -88,9 +98,12 @@ protected function assertDatabaseExecutedQueries(array $expectedQueries): void
*/
protected function profileDatabaseQueries(callable $callback): mixed
{
$this->startDatabaseProfiler();
$result = $callback();
$this->stopDatabaseProfiler();
try {
$this->startDatabaseQueryLog();
$result = $callback();
} finally {
$this->stopDatabaseQueryLog();
}

return $result;
}
Expand All @@ -102,7 +115,7 @@ protected function profileDatabaseQueryCount(int $expectedCount, callable $callb
{
return $this->profileDatabaseQueries(function () use ($expectedCount, $callback) {
$result = $callback();
$this->assertDatabaseQueriesCount($expectedCount);
$this->assertDatabaseQueryCount($expectedCount);

return $result;
});
Expand All @@ -120,4 +133,17 @@ protected function profileDatabaseExecutedQuery(string $expectedQuery, callable
return $result;
});
}

/**
* Wrapper to profile database queries and assert the executed queries.
*/
protected function profileDatabaseExecutedQueries(array $expectedQueries, callable $callback): mixed
{
return $this->profileDatabaseQueries(function () use ($expectedQueries, $callback) {
$result = $callback();
$this->assertDatabaseExecutedQueries($expectedQueries);

return $result;
});
}
}

0 comments on commit c52815a

Please sign in to comment.