Skip to content

Commit 74f2b81

Browse files
committed
test: add test for getFieldData() for MySQLi
1 parent 83c582c commit 74f2b81

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Live;
15+
16+
use CodeIgniter\Database\BaseConnection;
17+
use CodeIgniter\Database\Forge;
18+
use CodeIgniter\Test\CIUnitTestCase;
19+
use Config\Database;
20+
21+
abstract class AbstractGetFieldDataTest extends CIUnitTestCase
22+
{
23+
/**
24+
* @var BaseConnection
25+
*/
26+
protected $db;
27+
28+
protected Forge $forge;
29+
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
34+
$this->db = Database::connect($this->DBGroup);
35+
36+
$this->createForge();
37+
$this->createTable();
38+
}
39+
40+
/**
41+
* Make sure that $db and $forge are instantiated.
42+
*/
43+
abstract protected function createForge(): void;
44+
45+
protected function tearDown(): void
46+
{
47+
parent::tearDown();
48+
49+
$this->forge->dropTable('test1', true);
50+
}
51+
52+
protected function createTable()
53+
{
54+
$this->forge->dropTable('test1', true);
55+
56+
$this->forge->addField([
57+
'id' => [
58+
'type' => 'INT',
59+
'auto_increment' => true,
60+
],
61+
'text_not_null' => [
62+
'type' => 'VARCHAR',
63+
'constraint' => 64,
64+
],
65+
'text_null' => [
66+
'type' => 'VARCHAR',
67+
'constraint' => 64,
68+
'null' => true,
69+
],
70+
'int_default_0' => [
71+
'type' => 'INT',
72+
'default' => 0,
73+
],
74+
'text_default_null' => [
75+
'type' => 'VARCHAR',
76+
'constraint' => 64,
77+
'default' => null,
78+
],
79+
'text_default_text_null' => [
80+
'type' => 'VARCHAR',
81+
'constraint' => 64,
82+
'default' => 'null',
83+
],
84+
'text_default_abc' => [
85+
'type' => 'VARCHAR',
86+
'constraint' => 64,
87+
'default' => 'abc',
88+
],
89+
]);
90+
$this->forge->addKey('id', true);
91+
$this->forge->createTable('test1');
92+
}
93+
94+
abstract public function testGetFieldData(): void;
95+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
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\MySQLi;
13+
14+
use CodeIgniter\Database\MySQLi\Connection;
15+
use CodeIgniter\Database\MySQLi\Forge;
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\DatabaseTestTrait;
18+
use Config\Database;
19+
20+
/**
21+
* @group DatabaseLive
22+
*
23+
* @internal
24+
*/
25+
final class GetFieldDataTest extends CIUnitTestCase
26+
{
27+
use DatabaseTestTrait;
28+
29+
protected $migrate = false;
30+
31+
/**
32+
* @var Connection
33+
*/
34+
protected $db;
35+
36+
private Forge $forge;
37+
38+
protected function setUp(): void
39+
{
40+
parent::setUp();
41+
42+
if ($this->db->DBDriver !== 'MySQLi') {
43+
$this->markTestSkipped('This test is only for MySQLi.');
44+
}
45+
46+
$this->forge = Database::forge($this->db);
47+
}
48+
49+
public function testGetFieldData(): void
50+
{
51+
$this->forge->dropTable('test1', true);
52+
53+
$this->forge->addField([
54+
'id' => [
55+
'type' => 'INT',
56+
'auto_increment' => true,
57+
],
58+
'text_not_null' => [
59+
'type' => 'VARCHAR',
60+
'constraint' => 64,
61+
],
62+
'text_null' => [
63+
'type' => 'VARCHAR',
64+
'constraint' => 64,
65+
'null' => true,
66+
],
67+
'int_default_0' => [
68+
'type' => 'INT',
69+
'default' => 0,
70+
],
71+
'text_default_null' => [
72+
'type' => 'VARCHAR',
73+
'constraint' => 64,
74+
'default' => null,
75+
],
76+
'text_default_text_null' => [
77+
'type' => 'VARCHAR',
78+
'constraint' => 64,
79+
'default' => 'null',
80+
],
81+
'text_default_abc' => [
82+
'type' => 'VARCHAR',
83+
'constraint' => 64,
84+
'default' => 'abc',
85+
],
86+
]);
87+
$this->forge->addKey('id', true);
88+
$this->forge->createTable('test1');
89+
90+
$fields = $this->db->getFieldData('test1');
91+
92+
$this->assertJsonStringEqualsJsonString(
93+
json_encode([
94+
(object) [
95+
'name' => 'id',
96+
'type' => 'int',
97+
'max_length' => null,
98+
'default' => null, // The default value is not defined.
99+
'primary_key' => 1,
100+
'nullable' => false,
101+
],
102+
(object) [
103+
'name' => 'text_not_null',
104+
'type' => 'varchar',
105+
'max_length' => 64,
106+
'default' => null, // The default value is not defined.
107+
'primary_key' => 0,
108+
'nullable' => false,
109+
],
110+
(object) [
111+
'name' => 'text_null',
112+
'type' => 'varchar',
113+
'max_length' => 64,
114+
'default' => null, // The default value is not defined.
115+
'primary_key' => 0,
116+
'nullable' => true,
117+
],
118+
(object) [
119+
'name' => 'int_default_0',
120+
'type' => 'int',
121+
'max_length' => null,
122+
'default' => '0', // int 0
123+
'primary_key' => 0,
124+
'nullable' => false,
125+
],
126+
(object) [
127+
'name' => 'text_default_null',
128+
'type' => 'varchar',
129+
'max_length' => 64,
130+
'default' => null, // NULL value
131+
'primary_key' => 0,
132+
'nullable' => true,
133+
],
134+
(object) [
135+
'name' => 'text_default_text_null',
136+
'type' => 'varchar',
137+
'max_length' => 64,
138+
'default' => 'null', // string "null"
139+
'primary_key' => 0,
140+
'nullable' => false,
141+
],
142+
(object) [
143+
'name' => 'text_default_abc',
144+
'type' => 'varchar',
145+
'max_length' => 64,
146+
'default' => 'abc', // string "abc"
147+
'primary_key' => 0,
148+
'nullable' => false,
149+
],
150+
]),
151+
json_encode($fields)
152+
);
153+
154+
$this->forge->dropTable('test1', true);
155+
}
156+
}

0 commit comments

Comments
 (0)