diff --git a/tests/system/Database/Live/SQLite/AlterTableTest.php b/tests/system/Database/Live/SQLite3/AlterTableTest.php similarity index 98% rename from tests/system/Database/Live/SQLite/AlterTableTest.php rename to tests/system/Database/Live/SQLite3/AlterTableTest.php index 74d53cb0783d..4a06a8ada091 100644 --- a/tests/system/Database/Live/SQLite/AlterTableTest.php +++ b/tests/system/Database/Live/SQLite3/AlterTableTest.php @@ -9,7 +9,7 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Database\Live\SQLite; +namespace CodeIgniter\Database\Live\SQLite3; use CodeIgniter\Database\Exceptions\DataException; use CodeIgniter\Database\SQLite3\Forge; @@ -43,12 +43,15 @@ protected function setUp(): void { parent::setUp(); + if ($this->db->DBDriver !== 'SQLite3') { + $this->markTestSkipped('This test is only for SQLite3.'); + } + $config = [ 'DBDriver' => 'SQLite3', 'database' => ':memory:', 'DBDebug' => true, ]; - $this->db = db_connect($config); $this->forge = Database::forge($config); $this->table = new Table($this->db, $this->forge); diff --git a/tests/system/Database/Live/SQLite3/GetFieldDataTest.php b/tests/system/Database/Live/SQLite3/GetFieldDataTest.php new file mode 100644 index 000000000000..252c6485f7f8 --- /dev/null +++ b/tests/system/Database/Live/SQLite3/GetFieldDataTest.php @@ -0,0 +1,153 @@ + + * + * 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\Database\SQLite3\Connection; +use CodeIgniter\Database\SQLite3\Forge; +use CodeIgniter\Test\CIUnitTestCase; +use Config\Database; + +/** + * @group DatabaseLive + * + * @internal + */ +final class GetFieldDataTest extends CIUnitTestCase +{ + /** + * @var Connection + */ + protected $db; + + private Forge $forge; + + 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.'); + } + + $config = [ + 'DBDriver' => 'SQLite3', + 'database' => 'database.db', + 'DBDebug' => true, + ]; + $this->db = db_connect($config); + $this->forge = Database::forge($config); + } + + public function testGetFieldData(): void + { + $this->forge->dropTable('test1', true); + + $this->forge->addField([ + 'id' => [ + 'type' => 'INT', + 'auto_increment' => true, + ], + 'text_not_null' => [ + 'type' => 'VARCHAR', + ], + 'text_null' => [ + 'type' => 'VARCHAR', + 'null' => true, + ], + 'int_default_0' => [ + 'type' => 'INT', + 'default' => 0, + ], + 'text_default_null' => [ + 'type' => 'VARCHAR', + 'default' => null, + ], + 'text_default_text_null' => [ + 'type' => 'VARCHAR', + 'default' => 'null', + ], + 'text_default_abc' => [ + 'type' => 'VARCHAR', + 'default' => 'abc', + ], + ]); + $this->forge->addKey('id', true); + $this->forge->createTable('test1'); + + $fields = $this->db->getFieldData('test1'); + + $this->assertJsonStringEqualsJsonString( + json_encode([ + (object) [ + 'name' => 'id', + 'type' => 'INTEGER', + 'max_length' => null, + 'default' => null, // The default value is not defined. + 'primary_key' => true, + 'nullable' => true, + ], + (object) [ + 'name' => 'text_not_null', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => null, // The default value is not defined. + 'primary_key' => false, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_null', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => null, // The default value is not defined. + 'primary_key' => false, + 'nullable' => true, + ], + (object) [ + 'name' => 'int_default_0', + 'type' => 'INT', + 'max_length' => null, + 'default' => '0', // int 0 + 'primary_key' => false, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_default_null', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => 'NULL', // NULL value + 'primary_key' => false, + 'nullable' => true, + ], + (object) [ + 'name' => 'text_default_text_null', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => "'null'", // string "null" + 'primary_key' => false, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_default_abc', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => "'abc'", // string "abc" + 'primary_key' => false, + 'nullable' => false, + ], + ]), + json_encode($fields) + ); + + $this->forge->dropTable('test1', true); + } +} diff --git a/tests/system/Database/Live/SQLite/GetIndexDataTest.php b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php similarity index 93% rename from tests/system/Database/Live/SQLite/GetIndexDataTest.php rename to tests/system/Database/Live/SQLite3/GetIndexDataTest.php index c8b34d7ee651..cf05b7a7f07b 100644 --- a/tests/system/Database/Live/SQLite/GetIndexDataTest.php +++ b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php @@ -9,12 +9,11 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Database\Live\SQLite; +namespace CodeIgniter\Database\Live\SQLite3; use CodeIgniter\Database\SQLite3\Connection; use CodeIgniter\Database\SQLite3\Forge; use CodeIgniter\Test\CIUnitTestCase; -use CodeIgniter\Test\DatabaseTestTrait; use Config\Database; use stdClass; @@ -25,15 +24,6 @@ */ final class GetIndexDataTest extends CIUnitTestCase { - use DatabaseTestTrait; - - /** - * In setUp() db connection is changed. So migration doesn't work - * - * @var bool - */ - protected $migrate = false; - /** * @var Connection */ @@ -45,12 +35,16 @@ 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.'); + } + $config = [ 'DBDriver' => 'SQLite3', 'database' => 'database.db', 'DBDebug' => true, ]; - $this->db = db_connect($config); $this->forge = Database::forge($config); }