Skip to content

Commit 31c5648

Browse files
committed
add synchronous value validation
1 parent 3af4da8 commit 31c5648

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

system/Database/SQLite3/Connection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
1818
use CodeIgniter\Database\TableName;
1919
use Exception;
20+
use InvalidArgumentException;
2021
use SQLite3;
2122
use SQLite3Result;
2223
use stdClass;
@@ -81,6 +82,9 @@ public function initialize()
8182
}
8283

8384
if (is_int($this->synchronous)) {
85+
if (! in_array($this->synchronous, [0, 1, 2, 3], true)) {
86+
throw new InvalidArgumentException('Invalid synchronous value.');
87+
}
8488
$this->connID->exec('PRAGMA synchronous = ' . $this->synchronous);
8589
}
8690
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Live\SQLite3;
15+
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use Config\Database;
18+
use InvalidArgumentException;
19+
use PHPUnit\Framework\Attributes\Group;
20+
21+
/**
22+
* @internal
23+
*/
24+
#[Group('DatabaseLive')]
25+
final class ConnectTest extends CIUnitTestCase
26+
{
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$this->db = Database::connect($this->DBGroup);
32+
33+
if ($this->db->DBDriver !== 'SQLite3') {
34+
$this->markTestSkipped('This test is only for SQLite3.');
35+
}
36+
}
37+
38+
public function testShowErrorMessageWhenSettingInvalidSynchronous(): void
39+
{
40+
$this->expectException(InvalidArgumentException::class);
41+
$this->expectExceptionMessage('Invalid synchronous value.');
42+
43+
$config = config('Database');
44+
$group = $config->tests;
45+
// Sets invalid synchronous.
46+
$group['synchronous'] = 123;
47+
$db = Database::connect($group);
48+
49+
// Actually connect to DB.
50+
$db->initialize();
51+
}
52+
}

0 commit comments

Comments
 (0)