Skip to content

Commit c311720

Browse files
Fix testExceptionContainsRawQuery() in ConnectionTest class in SQLite. (yiisoft#20653)
1 parent 8a87a5f commit c311720

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

tests/framework/db/ConnectionTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,21 @@ public function testExceptionContainsRawQuery(): void
397397
/**
398398
* @param Connection $connection
399399
*/
400-
private function runExceptionTest($connection): void
400+
private function runExceptionTest(Connection $connection): void
401401
{
402402
$thrown = false;
403+
$sqlAssertLog = 'INSERT INTO qlog1(a) VALUES(1);';
404+
405+
if ($connection->getDriverName() === 'sqlite') {
406+
// SQLite shows placeholders (`:a`), other drivers show values (`1`) in error messages.
407+
$sqlAssertLog = 'INSERT INTO qlog1(a) VALUES(:a);';
408+
}
409+
403410
try {
404411
$connection->createCommand('INSERT INTO qlog1(a) VALUES(:a);', [':a' => 1])->execute();
405412
} catch (\yii\db\Exception $e) {
406413
$this->assertStringContainsString(
407-
'INSERT INTO qlog1(a) VALUES(1);',
414+
$sqlAssertLog,
408415
$e->getMessage(),
409416
'Exception message should contain raw SQL query: ' . (string) $e
410417
);
@@ -413,11 +420,18 @@ private function runExceptionTest($connection): void
413420
$this->assertTrue($thrown, 'An exception should have been thrown by the command.');
414421

415422
$thrown = false;
423+
$sqlAssertLog = 'SELECT * FROM qlog1 WHERE id=1 ORDER BY nonexistingcolumn;';
424+
425+
if ($connection->getDriverName() === 'sqlite') {
426+
// SQLite shows placeholders (`:a`), other drivers show values (`1`) in error messages.
427+
$sqlAssertLog = 'SELECT * FROM qlog1 WHERE id=:a ORDER BY nonexistingcolumn;';
428+
}
429+
416430
try {
417431
$connection->createCommand('SELECT * FROM qlog1 WHERE id=:a ORDER BY nonexistingcolumn;', [':a' => 1])->queryAll();
418432
} catch (\yii\db\Exception $e) {
419433
$this->assertStringContainsString(
420-
'SELECT * FROM qlog1 WHERE id=1 ORDER BY nonexistingcolumn;',
434+
$sqlAssertLog,
421435
$e->getMessage(),
422436
'Exception message should contain raw SQL query: ' . (string) $e,
423437
);

tests/framework/db/sqlite/ConnectionTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace yiiunit\framework\db\sqlite;
1010

11-
use Exception;
1211
use Yii;
1312
use yii\db\Connection;
13+
use yii\db\Exception;
1414
use yii\db\Transaction;
1515
use yiiunit\data\ar\ActiveRecord;
1616
use yiiunit\data\ar\Customer;
@@ -218,9 +218,4 @@ public function testAliasDbPath(): void
218218

219219
$connection->close();
220220
}
221-
222-
public function testExceptionContainsRawQuery(): void
223-
{
224-
$this->markTestSkipped('This test does not work on sqlite because preparing the failing query fails');
225-
}
226221
}

0 commit comments

Comments
 (0)