Skip to content

Commit

Permalink
add synchronous value validation
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Oct 7, 2024
1 parent 3af4da8 commit 31c5648
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\TableName;
use Exception;
use InvalidArgumentException;
use SQLite3;
use SQLite3Result;
use stdClass;
Expand Down Expand Up @@ -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);
}
}
Expand Down
52 changes: 52 additions & 0 deletions tests/system/Database/Live/SQLite3/ConnectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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();
}
}

0 comments on commit 31c5648

Please sign in to comment.