From 74f2b812a2a667371e956bdf50a3a6c883986805 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 19 Jan 2024 12:35:47 +0900 Subject: [PATCH 1/3] test: add test for getFieldData() for MySQLi --- .../Live/AbstractGetFieldDataTest.php | 95 +++++++++++ .../Database/Live/MySQLi/GetFieldDataTest.php | 156 ++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 tests/system/Database/Live/AbstractGetFieldDataTest.php create mode 100644 tests/system/Database/Live/MySQLi/GetFieldDataTest.php diff --git a/tests/system/Database/Live/AbstractGetFieldDataTest.php b/tests/system/Database/Live/AbstractGetFieldDataTest.php new file mode 100644 index 000000000000..570061cac3e3 --- /dev/null +++ b/tests/system/Database/Live/AbstractGetFieldDataTest.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Database\Live; + +use CodeIgniter\Database\BaseConnection; +use CodeIgniter\Database\Forge; +use CodeIgniter\Test\CIUnitTestCase; +use Config\Database; + +abstract class AbstractGetFieldDataTest extends CIUnitTestCase +{ + /** + * @var BaseConnection + */ + protected $db; + + protected Forge $forge; + + protected function setUp(): void + { + parent::setUp(); + + $this->db = Database::connect($this->DBGroup); + + $this->createForge(); + $this->createTable(); + } + + /** + * Make sure that $db and $forge are instantiated. + */ + abstract protected function createForge(): void; + + protected function tearDown(): void + { + parent::tearDown(); + + $this->forge->dropTable('test1', true); + } + + protected function createTable() + { + $this->forge->dropTable('test1', true); + + $this->forge->addField([ + 'id' => [ + 'type' => 'INT', + 'auto_increment' => true, + ], + 'text_not_null' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + ], + 'text_null' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'null' => true, + ], + 'int_default_0' => [ + 'type' => 'INT', + 'default' => 0, + ], + 'text_default_null' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'default' => null, + ], + 'text_default_text_null' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'default' => 'null', + ], + 'text_default_abc' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'default' => 'abc', + ], + ]); + $this->forge->addKey('id', true); + $this->forge->createTable('test1'); + } + + abstract public function testGetFieldData(): void; +} diff --git a/tests/system/Database/Live/MySQLi/GetFieldDataTest.php b/tests/system/Database/Live/MySQLi/GetFieldDataTest.php new file mode 100644 index 000000000000..f7a14f950f8f --- /dev/null +++ b/tests/system/Database/Live/MySQLi/GetFieldDataTest.php @@ -0,0 +1,156 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Database\Live\MySQLi; + +use CodeIgniter\Database\MySQLi\Connection; +use CodeIgniter\Database\MySQLi\Forge; +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\DatabaseTestTrait; +use Config\Database; + +/** + * @group DatabaseLive + * + * @internal + */ +final class GetFieldDataTest extends CIUnitTestCase +{ + use DatabaseTestTrait; + + protected $migrate = false; + + /** + * @var Connection + */ + protected $db; + + private Forge $forge; + + protected function setUp(): void + { + parent::setUp(); + + if ($this->db->DBDriver !== 'MySQLi') { + $this->markTestSkipped('This test is only for MySQLi.'); + } + + $this->forge = Database::forge($this->db); + } + + public function testGetFieldData(): void + { + $this->forge->dropTable('test1', true); + + $this->forge->addField([ + 'id' => [ + 'type' => 'INT', + 'auto_increment' => true, + ], + 'text_not_null' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + ], + 'text_null' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'null' => true, + ], + 'int_default_0' => [ + 'type' => 'INT', + 'default' => 0, + ], + 'text_default_null' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'default' => null, + ], + 'text_default_text_null' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'default' => 'null', + ], + 'text_default_abc' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'default' => 'abc', + ], + ]); + $this->forge->addKey('id', true); + $this->forge->createTable('test1'); + + $fields = $this->db->getFieldData('test1'); + + $this->assertJsonStringEqualsJsonString( + json_encode([ + (object) [ + 'name' => 'id', + 'type' => 'int', + 'max_length' => null, + 'default' => null, // The default value is not defined. + 'primary_key' => 1, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_not_null', + 'type' => 'varchar', + 'max_length' => 64, + 'default' => null, // The default value is not defined. + 'primary_key' => 0, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_null', + 'type' => 'varchar', + 'max_length' => 64, + 'default' => null, // The default value is not defined. + 'primary_key' => 0, + 'nullable' => true, + ], + (object) [ + 'name' => 'int_default_0', + 'type' => 'int', + 'max_length' => null, + 'default' => '0', // int 0 + 'primary_key' => 0, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_default_null', + 'type' => 'varchar', + 'max_length' => 64, + 'default' => null, // NULL value + 'primary_key' => 0, + 'nullable' => true, + ], + (object) [ + 'name' => 'text_default_text_null', + 'type' => 'varchar', + 'max_length' => 64, + 'default' => 'null', // string "null" + 'primary_key' => 0, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_default_abc', + 'type' => 'varchar', + 'max_length' => 64, + 'default' => 'abc', // string "abc" + 'primary_key' => 0, + 'nullable' => false, + ], + ]), + json_encode($fields) + ); + + $this->forge->dropTable('test1', true); + } +} From ebf8fab81d8b75efec9fa6c147947395f6274bfb Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 19 Jan 2024 13:07:28 +0900 Subject: [PATCH 2/3] test: extract AbstractGetFieldDataTest class --- .../Database/Live/MySQLi/GetFieldDataTest.php | 65 ++----------------- .../Live/SQLite3/GetFieldDataTest.php | 54 +-------------- 2 files changed, 8 insertions(+), 111 deletions(-) diff --git a/tests/system/Database/Live/MySQLi/GetFieldDataTest.php b/tests/system/Database/Live/MySQLi/GetFieldDataTest.php index f7a14f950f8f..7ba2fc43a0cb 100644 --- a/tests/system/Database/Live/MySQLi/GetFieldDataTest.php +++ b/tests/system/Database/Live/MySQLi/GetFieldDataTest.php @@ -1,5 +1,7 @@ db->DBDriver !== 'MySQLi') { $this->markTestSkipped('This test is only for MySQLi.'); } @@ -48,45 +34,6 @@ protected function setUp(): void public function testGetFieldData(): void { - $this->forge->dropTable('test1', true); - - $this->forge->addField([ - 'id' => [ - 'type' => 'INT', - 'auto_increment' => true, - ], - 'text_not_null' => [ - 'type' => 'VARCHAR', - 'constraint' => 64, - ], - 'text_null' => [ - 'type' => 'VARCHAR', - 'constraint' => 64, - 'null' => true, - ], - 'int_default_0' => [ - 'type' => 'INT', - 'default' => 0, - ], - 'text_default_null' => [ - 'type' => 'VARCHAR', - 'constraint' => 64, - 'default' => null, - ], - 'text_default_text_null' => [ - 'type' => 'VARCHAR', - 'constraint' => 64, - 'default' => 'null', - ], - 'text_default_abc' => [ - 'type' => 'VARCHAR', - 'constraint' => 64, - 'default' => 'abc', - ], - ]); - $this->forge->addKey('id', true); - $this->forge->createTable('test1'); - $fields = $this->db->getFieldData('test1'); $this->assertJsonStringEqualsJsonString( @@ -150,7 +97,5 @@ public function testGetFieldData(): void ]), json_encode($fields) ); - - $this->forge->dropTable('test1', true); } } diff --git a/tests/system/Database/Live/SQLite3/GetFieldDataTest.php b/tests/system/Database/Live/SQLite3/GetFieldDataTest.php index 252c6485f7f8..70586f10fbdd 100644 --- a/tests/system/Database/Live/SQLite3/GetFieldDataTest.php +++ b/tests/system/Database/Live/SQLite3/GetFieldDataTest.php @@ -11,9 +11,7 @@ namespace CodeIgniter\Database\Live\SQLite3; -use CodeIgniter\Database\SQLite3\Connection; -use CodeIgniter\Database\SQLite3\Forge; -use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Database\Live\AbstractGetFieldDataTest; use Config\Database; /** @@ -21,20 +19,10 @@ * * @internal */ -final class GetFieldDataTest extends CIUnitTestCase +final class GetFieldDataTest extends AbstractGetFieldDataTest { - /** - * @var Connection - */ - protected $db; - - private Forge $forge; - - protected function setUp(): void + protected function createForge(): void { - parent::setUp(); - - $this->db = Database::connect($this->DBGroup); if ($this->db->DBDriver !== 'SQLite3') { $this->markTestSkipped('This test is only for SQLite3.'); } @@ -50,40 +38,6 @@ protected function setUp(): void 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( @@ -147,7 +101,5 @@ public function testGetFieldData(): void ]), json_encode($fields) ); - - $this->forge->dropTable('test1', true); } } From 4411e44c1dfb8f2d86f3e8b9a0bc308d3e602bf6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 20 Jan 2024 10:43:29 +0900 Subject: [PATCH 3/3] test: fix assertions on MySQL 5.7 --- .../Database/Live/MySQLi/GetFieldDataTest.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/system/Database/Live/MySQLi/GetFieldDataTest.php b/tests/system/Database/Live/MySQLi/GetFieldDataTest.php index 7ba2fc43a0cb..aacc7fa84b99 100644 --- a/tests/system/Database/Live/MySQLi/GetFieldDataTest.php +++ b/tests/system/Database/Live/MySQLi/GetFieldDataTest.php @@ -32,6 +32,20 @@ protected function createForge(): void $this->forge = Database::forge($this->db); } + /** + * As of MySQL 8.0.17, the display width attribute for integer data types + * is deprecated and is not reported back anymore. + * + * @see https://dev.mysql.com/doc/refman/8.0/en/numeric-type-attributes.html + */ + private function isOldMySQL(): bool + { + return ! ( + version_compare($this->db->getVersion(), '8.0.17', '>=') + && strpos($this->db->getVersion(), 'MariaDB') === false + ); + } + public function testGetFieldData(): void { $fields = $this->db->getFieldData('test1'); @@ -41,7 +55,7 @@ public function testGetFieldData(): void (object) [ 'name' => 'id', 'type' => 'int', - 'max_length' => null, + 'max_length' => $this->isOldMySQL() ? 11 : null, 'default' => null, // The default value is not defined. 'primary_key' => 1, 'nullable' => false, @@ -65,7 +79,7 @@ public function testGetFieldData(): void (object) [ 'name' => 'int_default_0', 'type' => 'int', - 'max_length' => null, + 'max_length' => $this->isOldMySQL() ? 11 : null, 'default' => '0', // int 0 'primary_key' => 0, 'nullable' => false,