Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert sql #98

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Testing/Laravel/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
14 changes: 14 additions & 0 deletions src/Tests/Traits/SqlTestEnable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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: (?<sql>.*)\)$#', $queryException->getMessage(), $matches);

Assert::assertSame($expected, $matches['sql']);
}
}
}
Loading