Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions tests/system/Database/Live/AbstractGetFieldDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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;

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;
}
115 changes: 115 additions & 0 deletions tests/system/Database/Live/MySQLi/GetFieldDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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\MySQLi;

use CodeIgniter\Database\Live\AbstractGetFieldDataTest;
use Config\Database;

/**
* @group DatabaseLive
*
* @internal
*/
final class GetFieldDataTest extends AbstractGetFieldDataTest
{
protected function createForge(): void
{
if ($this->db->DBDriver !== 'MySQLi') {
$this->markTestSkipped('This test is only for MySQLi.');
}

$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');

$this->assertJsonStringEqualsJsonString(
json_encode([
(object) [
'name' => 'id',
'type' => 'int',
'max_length' => $this->isOldMySQL() ? 11 : 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' => $this->isOldMySQL() ? 11 : 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)
);
}
}
54 changes: 3 additions & 51 deletions tests/system/Database/Live/SQLite3/GetFieldDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,18 @@

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;

/**
* @group DatabaseLive
*
* @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.');
}
Expand All @@ -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(
Expand Down Expand Up @@ -147,7 +101,5 @@ public function testGetFieldData(): void
]),
json_encode($fields)
);

$this->forge->dropTable('test1', true);
}
}