From c52815ad87868c1ee49fbbbfed0accea7a3901e5 Mon Sep 17 00:00:00 2001 From: Sebastian Weckend Date: Sun, 29 Sep 2024 17:08:09 +0200 Subject: [PATCH] fix: issues --- .../PhpUnit/TestDatabaseProfilerTrait.php | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/Traits/TestTraits/PhpUnit/TestDatabaseProfilerTrait.php b/src/Traits/TestTraits/PhpUnit/TestDatabaseProfilerTrait.php index 34ed047ff..95b007af7 100644 --- a/src/Traits/TestTraits/PhpUnit/TestDatabaseProfilerTrait.php +++ b/src/Traits/TestTraits/PhpUnit/TestDatabaseProfilerTrait.php @@ -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."); } } @@ -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; } @@ -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; }); @@ -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; + }); + } }