Skip to content

Commit 36d6150

Browse files
committed
test: add test for getFieldData() on SQLite3
1 parent 434ebbd commit 36d6150

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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\SQLite;
13+
14+
use CodeIgniter\Database\SQLite3\Connection;
15+
use CodeIgniter\Database\SQLite3\Forge;
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use Config\Database;
18+
19+
/**
20+
* @group DatabaseLive
21+
*
22+
* @internal
23+
*/
24+
final class GetFieldDataTest extends CIUnitTestCase
25+
{
26+
/**
27+
* @var Connection
28+
*/
29+
protected $db;
30+
31+
private Forge $forge;
32+
33+
protected function setUp(): void
34+
{
35+
parent::setUp();
36+
37+
$this->db = Database::connect($this->DBGroup);
38+
if ($this->db->DBDriver !== 'SQLite3') {
39+
$this->markTestSkipped('This test is only for SQLite3.');
40+
}
41+
42+
$config = [
43+
'DBDriver' => 'SQLite3',
44+
'database' => 'database.db',
45+
'DBDebug' => true,
46+
];
47+
$this->db = db_connect($config);
48+
$this->forge = Database::forge($config);
49+
}
50+
51+
public function testGetFieldData(): void
52+
{
53+
$this->forge->dropTable('test1', true);
54+
55+
$this->forge->addField([
56+
'id' => [
57+
'type' => 'INT',
58+
'auto_increment' => true,
59+
],
60+
'text_not_null' => [
61+
'type' => 'VARCHAR',
62+
],
63+
'text_null' => [
64+
'type' => 'VARCHAR',
65+
'null' => true,
66+
],
67+
'int_default_0' => [
68+
'type' => 'INT',
69+
'default' => 0,
70+
],
71+
'text_default_null' => [
72+
'type' => 'VARCHAR',
73+
'default' => null,
74+
],
75+
'text_default_text_null' => [
76+
'type' => 'VARCHAR',
77+
'default' => 'null',
78+
],
79+
'text_default_abc' => [
80+
'type' => 'VARCHAR',
81+
'default' => 'abc',
82+
],
83+
]);
84+
$this->forge->addKey('id', true);
85+
$this->forge->createTable('test1');
86+
87+
$fields = $this->db->getFieldData('test1');
88+
89+
$this->assertJsonStringEqualsJsonString(
90+
json_encode([
91+
(object) [
92+
'name' => 'id',
93+
'type' => 'INTEGER',
94+
'max_length' => null,
95+
'default' => null, // The default value is not defined.
96+
'primary_key' => true,
97+
'nullable' => true,
98+
],
99+
(object) [
100+
'name' => 'text_not_null',
101+
'type' => 'VARCHAR',
102+
'max_length' => null,
103+
'default' => null, // The default value is not defined.
104+
'primary_key' => false,
105+
'nullable' => false,
106+
],
107+
(object) [
108+
'name' => 'text_null',
109+
'type' => 'VARCHAR',
110+
'max_length' => null,
111+
'default' => null, // The default value is not defined.
112+
'primary_key' => false,
113+
'nullable' => true,
114+
],
115+
(object) [
116+
'name' => 'int_default_0',
117+
'type' => 'INT',
118+
'max_length' => null,
119+
'default' => '0', // int 0
120+
'primary_key' => false,
121+
'nullable' => false,
122+
],
123+
(object) [
124+
'name' => 'text_default_null',
125+
'type' => 'VARCHAR',
126+
'max_length' => null,
127+
'default' => 'NULL', // NULL value
128+
'primary_key' => false,
129+
'nullable' => true,
130+
],
131+
(object) [
132+
'name' => 'text_default_text_null',
133+
'type' => 'VARCHAR',
134+
'max_length' => null,
135+
'default' => "'null'", // string "null"
136+
'primary_key' => false,
137+
'nullable' => false,
138+
],
139+
(object) [
140+
'name' => 'text_default_abc',
141+
'type' => 'VARCHAR',
142+
'max_length' => null,
143+
'default' => "'abc'", // string "abc"
144+
'primary_key' => false,
145+
'nullable' => false,
146+
],
147+
]),
148+
json_encode($fields)
149+
);
150+
}
151+
}

0 commit comments

Comments
 (0)