From c311720922c22330fd40dfb442a73fe7f32a9f79 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Fri, 31 Oct 2025 05:57:06 -0300 Subject: [PATCH] Fix `testExceptionContainsRawQuery()` in `ConnectionTest` class in SQLite. (#20653) --- tests/framework/db/ConnectionTest.php | 20 +++++++++++++++++--- tests/framework/db/sqlite/ConnectionTest.php | 7 +------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/framework/db/ConnectionTest.php b/tests/framework/db/ConnectionTest.php index 7e937310fb..a83d888acb 100644 --- a/tests/framework/db/ConnectionTest.php +++ b/tests/framework/db/ConnectionTest.php @@ -397,14 +397,21 @@ abstract class ConnectionTest extends DatabaseTestCase /** * @param Connection $connection */ - private function runExceptionTest($connection): void + private function runExceptionTest(Connection $connection): void { $thrown = false; + $sqlAssertLog = 'INSERT INTO qlog1(a) VALUES(1);'; + + if ($connection->getDriverName() === 'sqlite') { + // SQLite shows placeholders (`:a`), other drivers show values (`1`) in error messages. + $sqlAssertLog = 'INSERT INTO qlog1(a) VALUES(:a);'; + } + try { $connection->createCommand('INSERT INTO qlog1(a) VALUES(:a);', [':a' => 1])->execute(); } catch (\yii\db\Exception $e) { $this->assertStringContainsString( - 'INSERT INTO qlog1(a) VALUES(1);', + $sqlAssertLog, $e->getMessage(), 'Exception message should contain raw SQL query: ' . (string) $e ); @@ -413,11 +420,18 @@ abstract class ConnectionTest extends DatabaseTestCase $this->assertTrue($thrown, 'An exception should have been thrown by the command.'); $thrown = false; + $sqlAssertLog = 'SELECT * FROM qlog1 WHERE id=1 ORDER BY nonexistingcolumn;'; + + if ($connection->getDriverName() === 'sqlite') { + // SQLite shows placeholders (`:a`), other drivers show values (`1`) in error messages. + $sqlAssertLog = 'SELECT * FROM qlog1 WHERE id=:a ORDER BY nonexistingcolumn;'; + } + try { $connection->createCommand('SELECT * FROM qlog1 WHERE id=:a ORDER BY nonexistingcolumn;', [':a' => 1])->queryAll(); } catch (\yii\db\Exception $e) { $this->assertStringContainsString( - 'SELECT * FROM qlog1 WHERE id=1 ORDER BY nonexistingcolumn;', + $sqlAssertLog, $e->getMessage(), 'Exception message should contain raw SQL query: ' . (string) $e, ); diff --git a/tests/framework/db/sqlite/ConnectionTest.php b/tests/framework/db/sqlite/ConnectionTest.php index 616978db85..7ca6421f66 100644 --- a/tests/framework/db/sqlite/ConnectionTest.php +++ b/tests/framework/db/sqlite/ConnectionTest.php @@ -8,9 +8,9 @@ namespace yiiunit\framework\db\sqlite; -use Exception; use Yii; use yii\db\Connection; +use yii\db\Exception; use yii\db\Transaction; use yiiunit\data\ar\ActiveRecord; use yiiunit\data\ar\Customer; @@ -218,9 +218,4 @@ class ConnectionTest extends \yiiunit\framework\db\ConnectionTest $connection->close(); } - - public function testExceptionContainsRawQuery(): void - { - $this->markTestSkipped('This test does not work on sqlite because preparing the failing query fails'); - } }