-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathCreateDefinitionTest.php
169 lines (155 loc) · 6.56 KB
/
CreateDefinitionTest.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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Components;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Parsers\CreateDefinitions;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use PhpMyAdmin\SqlParser\Tests\TestCase;
class CreateDefinitionTest extends TestCase
{
public function testParse(): void
{
$component = CreateDefinitions::parse(
new Parser(),
$this->getTokensList('(str TEXT, FULLTEXT INDEX indx (str))'),
);
$this->assertEquals('str', $component[0]->name);
$this->assertNotNull($component[1]->key);
$this->assertEquals('FULLTEXT INDEX', $component[1]->key->type);
$this->assertEquals('indx', $component[1]->key->name);
$this->assertEquals('FULLTEXT INDEX `indx` (`str`)', (string) $component[1]);
}
public function testParse2(): void
{
$component = CreateDefinitions::parse(
new Parser(),
$this->getTokensList('(str TEXT NOT NULL INVISIBLE)'),
);
$this->assertEquals('str', $component[0]->name);
$this->assertNotNull($component[0]->type);
$this->assertEquals('TEXT', $component[0]->type->name);
$this->assertNotNull($component[0]->options);
$this->assertTrue($component[0]->options->has('INVISIBLE'));
$this->assertTrue($component[0]->options->has('NOT NULL'));
}
public function testParseErr1(): void
{
$parser = new Parser();
$component = CreateDefinitions::parse(
$parser,
$this->getTokensList('(str TEXT, FULLTEXT INDEX indx (str)'),
);
$this->assertCount(2, $component);
$this->assertEquals(
'A closing bracket was expected.',
$parser->errors[0]->getMessage(),
);
}
public function testParseErr2(): void
{
$parser = new Parser();
CreateDefinitions::parse(
$parser,
$this->getTokensList(')'),
);
$this->assertEquals(
'An opening bracket was expected.',
$parser->errors[0]->getMessage(),
);
}
public function testBuild(): void
{
$parser = new Parser(
'CREATE TABLE `payment` (' .
'-- snippet' . "\n" .
'`customer_id` smallint(5) unsigned NOT NULL,' .
'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) ' .
'REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' .
') ENGINE=InnoDB"',
);
$this->assertInstanceOf(CreateStatement::class, $parser->statements[0]);
$this->assertIsArray($parser->statements[0]->fields);
$this->assertEquals(
'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) ' .
'REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE',
$parser->statements[0]->fields[1]->build(),
);
}
public function testBuild2(): void
{
$parser = new Parser(
'CREATE TABLE `payment` (' .
'-- snippet' . "\n" .
'`customer_id` smallint(5) unsigned NOT NULL,' .
'`customer_data` longtext CHARACTER SET utf8mb4 CHARSET utf8mb4_bin NOT NULL ' .
'CHECK (json_valid(customer_data)),CONSTRAINT `fk_payment_customer` FOREIGN KEY ' .
'(`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' .
') ENGINE=InnoDB"',
);
$this->assertInstanceOf(CreateStatement::class, $parser->statements[0]);
$this->assertIsArray($parser->statements[0]->fields);
$this->assertEquals(
'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) ' .
'REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE',
$parser->statements[0]->fields[2]->build(),
);
}
public function testBuild3(): void
{
$parser = new Parser(
'DROP TABLE IF EXISTS `searches`;'
. 'CREATE TABLE `searches` ('
. ' `id` int(10) unsigned NOT NULL AUTO_INCREMENT,'
. ' `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,'
. ' `public_name` varchar(120) COLLATE utf8_unicode_ci NOT NULL,'
. ' `group_id` smallint(5) unsigned NOT NULL DEFAULT \'0\','
. ' `shortdesc` tinytext COLLATE utf8_unicode_ci,'
. ' `show_separators` tinyint(1) NOT NULL DEFAULT \'0\','
. ' `show_separators_two` tinyint(1) NOT NULL DEFAULT FALSE,'
. ' `deleted` tinyint(1) NOT NULL DEFAULT \'0\','
. ' PRIMARY KEY (`id`)'
. ') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;'
. ''
. 'ALTER TABLE `searches` ADD `admins_only` BOOLEAN NOT NULL DEFAULT FALSE AFTER `show_separators`;',
);
$this->assertInstanceOf(CreateStatement::class, $parser->statements[1]);
$this->assertIsArray($parser->statements[1]->fields);
$this->assertEquals(
'`public_name` varchar(120) COLLATE utf8_unicode_ci NOT NULL',
$parser->statements[1]->fields[2]->build(),
);
$this->assertEquals(
'`show_separators` tinyint(1) NOT NULL DEFAULT \'0\'',
$parser->statements[1]->fields[5]->build(),
);
$this->assertEquals(
'`show_separators_two` tinyint(1) NOT NULL DEFAULT FALSE',
$parser->statements[1]->fields[6]->build(),
);
}
public function testBuildWithInvisibleKeyword(): void
{
$parser = new Parser(
'CREATE TABLE `payment` (' .
'-- snippet' . "\n" .
'`customer_id` smallint(5) unsigned NOT NULL INVISIBLE,' .
'`customer_data` longtext CHARACTER SET utf8mb4 CHARSET utf8mb4_bin NOT NULL ' .
'CHECK (json_valid(customer_data)),CONSTRAINT `fk_payment_customer` FOREIGN KEY ' .
'(`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' .
') ENGINE=InnoDB"',
);
$this->assertInstanceOf(CreateStatement::class, $parser->statements[0]);
$this->assertIsArray($parser->statements[0]->fields);
$this->assertEquals(
'`customer_id` smallint(5) UNSIGNED NOT NULL INVISIBLE',
$parser->statements[0]->fields[0]->build(),
);
}
public function testBuildWithCompressed(): void
{
$query = 'CREATE TABLE `user` ( `message2` TEXT COMPRESSED )';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals("CREATE TABLE `user` (\n `message2` text COMPRESSED\n) ", $stmt->build());
}
}