Skip to content

Commit 8633a82

Browse files
committed
test: add test for Forge::addColumn() and null
1 parent 82079cb commit 8633a82

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tests/system/Database/Live/ForgeTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\DatabaseTestTrait;
1818
use Config\Database;
1919
use RuntimeException;
20+
use stdClass;
2021
use Tests\Support\Database\Seeds\CITestSeeder;
2122

2223
/**
@@ -825,6 +826,41 @@ public function testAddColumn()
825826
$this->assertSame('username', $fieldNames[1]);
826827
}
827828

829+
public function testAddColumnNull()
830+
{
831+
$this->forge->dropTable('forge_test_table', true);
832+
833+
$this->forge->addField([
834+
'col1' => ['type' => 'VARCHAR', 'constraint' => 255],
835+
'col2' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
836+
'col3' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => false],
837+
]);
838+
$this->forge->createTable('forge_test_table');
839+
840+
$this->forge->addColumn('forge_test_table', [
841+
'col4' => ['type' => 'VARCHAR', 'constraint' => 255],
842+
'col5' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
843+
'col6' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => false],
844+
]);
845+
846+
$this->db->resetDataCache();
847+
848+
$col1 = $this->getMetaData('col1', 'forge_test_table');
849+
$this->assertFalse($col1->nullable);
850+
$col2 = $this->getMetaData('col2', 'forge_test_table');
851+
$this->assertTrue($col2->nullable);
852+
$col3 = $this->getMetaData('col3', 'forge_test_table');
853+
$this->assertFalse($col3->nullable);
854+
$col4 = $this->getMetaData('col4', 'forge_test_table');
855+
$this->assertTrue($col4->nullable);
856+
$col5 = $this->getMetaData('col5', 'forge_test_table');
857+
$this->assertTrue($col5->nullable);
858+
$col6 = $this->getMetaData('col6', 'forge_test_table');
859+
$this->assertFalse($col6->nullable);
860+
861+
$this->forge->dropTable('forge_test_table', true);
862+
}
863+
828864
public function testAddFields()
829865
{
830866
$tableName = 'forge_test_fields';
@@ -1281,6 +1317,17 @@ public function testModifyColumnRename()
12811317
$this->forge->dropTable('forge_test_three', true);
12821318
}
12831319

1320+
private function getMetaData(string $column, string $table): stdClass
1321+
{
1322+
$fields = $this->db->getFieldData($table);
1323+
1324+
return $fields[array_search(
1325+
$column,
1326+
array_column($fields, 'name'),
1327+
true
1328+
)];
1329+
}
1330+
12841331
public function testConnectWithArrayGroup()
12851332
{
12861333
$group = config('Database');

0 commit comments

Comments
 (0)