Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathTableTest.php
More file actions
239 lines (231 loc) · 9.57 KB
/
Copy pathTableTest.php
File metadata and controls
239 lines (231 loc) · 9.57 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Utils;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use PhpMyAdmin\SqlParser\Tests\TestCase;
use PhpMyAdmin\SqlParser\Utils\ForeignKey;
use PhpMyAdmin\SqlParser\Utils\Table;
use PHPUnit\Framework\Attributes\DataProvider;
class TableTest extends TestCase
{
/** @param list<ForeignKey> $expected */
#[DataProvider('getForeignKeysProvider')]
public function testGetForeignKeys(string $query, array $expected): void
{
$parser = new Parser($query);
$this->assertInstanceOf(CreateStatement::class, $parser->statements[0]);
$result = $parser->statements[0]->getForeignKeys();
$this->assertEquals($expected, $result);
}
/** @return list<array{string, list<ForeignKey>}> */
public static function getForeignKeysProvider(): array
{
return [
[
'CREATE USER test',
[],
],
[
'CREATE TABLE `payment` (
`payment_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`customer_id` smallint(5) unsigned NOT NULL,
`staff_id` tinyint(3) unsigned NOT NULL,
`rental_id` int(11) DEFAULT NULL,
`amount` decimal(5,2) NOT NULL,
`payment_date` datetime NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`payment_id`),
KEY `idx_fk_staff_id` (`staff_id`),
KEY `idx_fk_customer_id` (`customer_id`),
KEY `fk_payment_rental` (`rental_id`),
CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`)
REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE,
CONSTRAINT `fk_payment_rental` FOREIGN KEY (`rental_id`)
REFERENCES `rental` (`rental_id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `fk_payment_staff` FOREIGN KEY (`staff_id`)
REFERENCES `staff` (`staff_id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=16050 DEFAULT CHARSET=utf8',
[
new ForeignKey(
constraint: 'fk_payment_customer',
indexList: ['customer_id'],
refTableName: 'customer',
refIndexList: ['customer_id'],
onUpdate: 'CASCADE',
),
new ForeignKey(
constraint: 'fk_payment_rental',
indexList: ['rental_id'],
refTableName: 'rental',
refIndexList: ['rental_id'],
onDelete: 'SET_NULL',
onUpdate: 'CASCADE',
),
new ForeignKey(
constraint: 'fk_payment_staff',
indexList: ['staff_id'],
refTableName: 'staff',
refIndexList: ['staff_id'],
onUpdate: 'CASCADE',
),
],
],
[
'CREATE TABLE `actor` (
`actor_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`actor_id`),
KEY `idx_actor_last_name` (`last_name`)
) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8',
[],
],
[
'CREATE TABLE `address` (
`address_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`address` varchar(50) NOT NULL,
`address2` varchar(50) DEFAULT NULL,
`district` varchar(20) NOT NULL,
`city_id` smallint(5) unsigned NOT NULL,
`postal_code` varchar(10) DEFAULT NULL,
`phone` varchar(20) NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`address_id`),
KEY `idx_fk_city_id` (`city_id`),
CONSTRAINT `fk_address_city` FOREIGN KEY (`city_id`) REFERENCES `city` (`city_id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=606 DEFAULT CHARSET=utf8',
[
new ForeignKey(
constraint: 'fk_address_city',
indexList: ['city_id'],
refTableName: 'city',
refIndexList: ['city_id'],
onUpdate: 'CASCADE',
),
],
],
];
}
/**
* @param array<string, array<string, bool|string>> $expected
* @psalm-param array<string, array{
* type: string,
* timestamp_not_null: bool,
* default_value?: string,
* default_current_timestamp?: bool,
* on_update_current_timestamp?: bool,
* expr?: string
* }> $expected
*/
#[DataProvider('getFieldsProvider')]
public function testGetFields(string $query, array $expected): void
{
$parser = new Parser($query);
$this->assertInstanceOf(CreateStatement::class, $parser->statements[0]);
$this->assertEquals($expected, Table::getFields($parser->statements[0]));
}
/**
* @return array<int, array<int, string|array<string, array<string, bool|string>>>>
* @psalm-return list<array{string, array<string, array{
* type: string,
* timestamp_not_null: bool,
* default_value?: string,
* default_current_timestamp?: bool,
* on_update_current_timestamp?: bool,
* expr?: string
* }>}>
*/
public static function getFieldsProvider(): array
{
return [
[
'CREATE USER test',
[],
],
[
'CREATE TABLE `address` (
`address_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`address` varchar(50) NOT NULL,
`address2` varchar(50) DEFAULT NULL,
`district` varchar(20) NOT NULL,
`city_id` smallint(5) unsigned NOT NULL,
`postal_code` varchar(10) DEFAULT NULL,
`phone` varchar(20) NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`address_id`),
KEY `idx_fk_city_id` (`city_id`),
CONSTRAINT `fk_address_city` FOREIGN KEY (`city_id`) REFERENCES `city` (`city_id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=606 DEFAULT CHARSET=utf8',
[
'address_id' => [
'type' => 'SMALLINT',
'timestamp_not_null' => false,
],
'address' => [
'type' => 'VARCHAR',
'timestamp_not_null' => false,
],
'address2' => [
'type' => 'VARCHAR',
'timestamp_not_null' => false,
'default_value' => 'NULL',
],
'district' => [
'type' => 'VARCHAR',
'timestamp_not_null' => false,
],
'city_id' => [
'type' => 'SMALLINT',
'timestamp_not_null' => false,
],
'postal_code' => [
'type' => 'VARCHAR',
'timestamp_not_null' => false,
'default_value' => 'NULL',
],
'phone' => [
'type' => 'VARCHAR',
'timestamp_not_null' => false,
],
'last_update' => [
'type' => 'TIMESTAMP',
'timestamp_not_null' => true,
'default_value' => 'CURRENT_TIMESTAMP',
'default_current_timestamp' => true,
'on_update_current_timestamp' => true,
],
],
],
[
'CREATE TABLE table1 (
a INT NOT NULL,
b VARCHAR(32),
c INT AS (a mod 10) VIRTUAL,
d VARCHAR(5) AS (left(b,5)) PERSISTENT
)',
[
'a' => [
'type' => 'INT',
'timestamp_not_null' => false,
],
'b' => [
'type' => 'VARCHAR',
'timestamp_not_null' => false,
],
'c' => [
'type' => 'INT',
'timestamp_not_null' => false,
'expr' => '(a mod 10)',
],
'd' => [
'type' => 'VARCHAR',
'timestamp_not_null' => false,
'expr' => '(left(b,5))',
],
],
],
];
}
}
You can’t perform that action at this time.
