Skip to content

Commit eafb2d2

Browse files
committed
test: add test for SQLite3 Forge::modifyColumn()
1 parent c28c0c3 commit eafb2d2

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\Live\SQLite3;
13+
14+
use CodeIgniter\Database\Forge;
15+
use CodeIgniter\Test\CIUnitTestCase;
16+
use Config\Database;
17+
18+
/**
19+
* @group DatabaseLive
20+
*
21+
* @internal
22+
*/
23+
final class ForgeModifyColumnTest extends CIUnitTestCase
24+
{
25+
private Forge $forge;
26+
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$this->db = Database::connect($this->DBGroup);
32+
33+
if ($this->db->DBDriver !== 'SQLite3') {
34+
$this->markTestSkipped('This test is only for SQLite3.');
35+
}
36+
37+
$this->forge = Database::forge($this->DBGroup);
38+
}
39+
40+
public function testModifyColumnRename(): void
41+
{
42+
$this->forge->dropTable('forge_test_three', true);
43+
44+
$this->forge->addField([
45+
'id' => [
46+
'type' => 'INTEGER',
47+
'constraint' => 11,
48+
'auto_increment' => true,
49+
],
50+
'int' => [
51+
'type' => 'INT',
52+
'constraint' => 10,
53+
'null' => false,
54+
'default' => 0,
55+
],
56+
'varchar' => [
57+
'type' => 'VARCHAR',
58+
'constraint' => 7,
59+
'null' => false,
60+
],
61+
'decimal' => [
62+
'type' => 'DECIMAL',
63+
'constraint' => '10,5',
64+
'default' => 0.1,
65+
],
66+
'name' => [
67+
'type' => 'VARCHAR',
68+
'constraint' => 255,
69+
'null' => true,
70+
],
71+
]);
72+
73+
$this->forge->addKey('id', true);
74+
$this->forge->createTable('forge_test_three');
75+
76+
$this->assertTrue($this->db->fieldExists('name', 'forge_test_three'));
77+
78+
$this->forge->modifyColumn('forge_test_three', [
79+
'name' => [
80+
'name' => 'altered',
81+
'type' => 'VARCHAR',
82+
'constraint' => 255,
83+
'null' => true,
84+
],
85+
]);
86+
87+
$this->db->resetDataCache();
88+
89+
$fieldData = $this->db->getFieldData('forge_test_three');
90+
$fields = [];
91+
92+
foreach ($fieldData as $obj) {
93+
$fields[$obj->name] = $obj;
94+
}
95+
96+
$this->assertFalse($this->db->fieldExists('name', 'forge_test_three'));
97+
$this->assertTrue($this->db->fieldExists('altered', 'forge_test_three'));
98+
99+
$this->assertFalse($fields['int']->nullable);
100+
$this->assertSame('0', $fields['int']->default);
101+
102+
$this->assertFalse($fields['varchar']->nullable);
103+
$this->assertNull($fields['varchar']->default);
104+
105+
$this->assertFalse($fields['decimal']->nullable);
106+
$this->assertSame('0.1', $fields['decimal']->default);
107+
108+
$this->assertTrue($fields['altered']->nullable);
109+
$this->assertNull($fields['altered']->default);
110+
111+
$this->forge->dropTable('forge_test_three', true);
112+
}
113+
}

0 commit comments

Comments
 (0)