From 8ea1c1c21b7d82ae4806fe37099496119d6783b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Fri, 6 Sep 2024 07:46:42 +0200 Subject: [PATCH 1/2] feat(SqlTestEnable): add method assertSql() --- src/Tests/Traits/SqlTestEnable.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Tests/Traits/SqlTestEnable.php b/src/Tests/Traits/SqlTestEnable.php index 8ffecd81..3eeeac3e 100644 --- a/src/Tests/Traits/SqlTestEnable.php +++ b/src/Tests/Traits/SqlTestEnable.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\ConnectionResolver; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\QueryException; use Illuminate\Database\SQLiteConnection; use PDO; use PHPUnit\Framework\Assert; @@ -35,4 +36,17 @@ final protected static function assertQuerySql( Assert::assertSame(trim($expectedSql), $query->toSql()); Assert::assertSame($expectedBindings, $query->getBindings()); } + + final protected function assertSql(callable $query, string $sql): void + { + try { + ($query)(); + Assert::fail('Failed asserting that query was executed.'); + } catch (QueryException $queryException) { + $expected = preg_replace('#\s+#', ' ', $sql); + preg_match('#\(SQL: (?.*)\)$#', $queryException->getMessage(), $matches); + + Assert::assertSame($expected, $matches['sql']); + } + } } From 5edb3e6d5f0e7c0fe34adc9be3f1d0c8f5b61361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Fri, 6 Sep 2024 07:51:23 +0200 Subject: [PATCH 2/2] fix(phpstan) --- src/Testing/Laravel/Logger.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Testing/Laravel/Logger.php b/src/Testing/Laravel/Logger.php index 2af065ea..37ea4393 100644 --- a/src/Testing/Laravel/Logger.php +++ b/src/Testing/Laravel/Logger.php @@ -67,6 +67,9 @@ public function debug(Stringable|string $message, array $context = []): void $this->debug[] = [$message, $context]; } + /** + * @param string $level + */ public function log($level, Stringable|string $message, array $context = []): void { $this->log[] = [$message, $context];