diff --git a/system/Database/SQLite3/Connection.php b/system/Database/SQLite3/Connection.php index 83e1803ed826..06a8f1911819 100644 --- a/system/Database/SQLite3/Connection.php +++ b/system/Database/SQLite3/Connection.php @@ -17,6 +17,7 @@ use CodeIgniter\Database\Exceptions\DatabaseException; use CodeIgniter\Database\TableName; use Exception; +use InvalidArgumentException; use SQLite3; use SQLite3Result; use stdClass; @@ -81,6 +82,9 @@ public function initialize() } if (is_int($this->synchronous)) { + if (! in_array($this->synchronous, [0, 1, 2, 3], true)) { + throw new InvalidArgumentException('Invalid synchronous value.'); + } $this->connID->exec('PRAGMA synchronous = ' . $this->synchronous); } } diff --git a/tests/system/Database/Live/SQLite3/ConnectTest.php b/tests/system/Database/Live/SQLite3/ConnectTest.php new file mode 100644 index 000000000000..e14e3acb1add --- /dev/null +++ b/tests/system/Database/Live/SQLite3/ConnectTest.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Database\Live\SQLite3; + +use CodeIgniter\Test\CIUnitTestCase; +use Config\Database; +use InvalidArgumentException; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('DatabaseLive')] +final class ConnectTest extends CIUnitTestCase +{ + protected function setUp(): void + { + parent::setUp(); + + $this->db = Database::connect($this->DBGroup); + + if ($this->db->DBDriver !== 'SQLite3') { + $this->markTestSkipped('This test is only for SQLite3.'); + } + } + + public function testShowErrorMessageWhenSettingInvalidSynchronous(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid synchronous value.'); + + $config = config('Database'); + $group = $config->tests; + // Sets invalid synchronous. + $group['synchronous'] = 123; + $db = Database::connect($group); + + // Actually connect to DB. + $db->initialize(); + } +}