forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAbstractGetFieldDataTestCase.php
174 lines (149 loc) · 5.74 KB
/
AbstractGetFieldDataTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?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;
use stdClass;
abstract class AbstractGetFieldDataTestCase extends CIUnitTestCase
{
/**
* @var BaseConnection
*/
protected $db;
protected Forge $forge;
protected string $table = 'test1';
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
helper('array');
}
protected function setUp(): void
{
parent::setUp();
$this->db = Database::connect($this->DBGroup);
$this->createForge();
}
/**
* Make sure that $db and $forge are instantiated.
*/
abstract protected function createForge(): void;
protected function tearDown(): void
{
parent::tearDown();
$this->forge->dropTable($this->table, true);
}
protected function createTableForDefault(): void
{
$this->forge->dropTable($this->table, 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($this->table);
}
protected function createTableForType(): void
{
$this->forge->dropTable($this->table, true);
// missing types:
// TINYINT,MEDIUMINT,BIT,YEAR,BINARY,VARBINARY (BLOB more or less handles these two),
// TINYTEXT,LONGTEXT,JSON,Spatial data types
// `id` must be INTEGER else SQLite3 error on not null for autoincrement field.
$fields = [
'id' => ['type' => 'INTEGER', 'constraint' => 20, 'auto_increment' => true],
'type_varchar' => ['type' => 'VARCHAR', 'constraint' => 40, 'null' => true],
'type_char' => ['type' => 'CHAR', 'constraint' => 10, 'null' => true],
// TEXT should not be used on SQLSRV. It is deprecated.
'type_text' => ['type' => 'TEXT', 'null' => true],
'type_smallint' => ['type' => 'SMALLINT', 'null' => true],
'type_integer' => ['type' => 'INTEGER', 'null' => true],
'type_float' => ['type' => 'FLOAT', 'null' => true],
'type_numeric' => ['type' => 'NUMERIC', 'constraint' => '18,2', 'null' => true],
'type_date' => ['type' => 'DATE', 'null' => true],
'type_time' => ['type' => 'TIME', 'null' => true],
// On SQLSRV `datetime2` is recommended.
'type_datetime' => ['type' => 'DATETIME', 'null' => true],
'type_timestamp' => ['type' => 'TIMESTAMP', 'null' => true],
'type_bigint' => ['type' => 'BIGINT', 'null' => true],
'type_real' => ['type' => 'REAL', 'null' => true],
'type_enum' => ['type' => 'ENUM', 'constraint' => ['appel', 'pears'], 'null' => true],
'type_set' => ['type' => 'SET', 'constraint' => ['one', 'two'], 'null' => true],
'type_mediumtext' => ['type' => 'MEDIUMTEXT', 'null' => true],
'type_double' => ['type' => 'DOUBLE', 'null' => true],
'type_decimal' => ['type' => 'DECIMAL', 'constraint' => '18,4', 'null' => true],
'type_blob' => ['type' => 'BLOB', 'null' => true],
'type_boolean' => ['type' => 'BOOLEAN', 'null' => true],
];
if ($this->db->DBDriver === 'Postgre') {
unset(
$fields['type_enum'],
$fields['type_set'],
$fields['type_mediumtext'],
$fields['type_double']
);
}
if ($this->db->DBDriver === 'SQLSRV') {
unset(
$fields['type_set'],
$fields['type_mediumtext'],
$fields['type_double']
);
}
$this->forge->addField($fields);
$this->forge->addKey('id', true);
$this->forge->createTable($this->table);
}
abstract public function testGetFieldDataDefault(): void;
/**
* @param list<stdClass> $expected
* @param list<stdClass> $actual
*/
protected function assertSameFieldData(array $expected, array $actual): void
{
$expectedArray = json_decode(json_encode($expected), true);
array_sort_by_multiple_keys($expectedArray, ['name' => SORT_ASC]);
$fieldsArray = json_decode(json_encode($actual), true);
array_sort_by_multiple_keys($fieldsArray, ['name' => SORT_ASC]);
$this->assertSame($expectedArray, $fieldsArray);
}
}