1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Tests\Components; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\CreateDefinition; |
8
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
9
|
|
|
use PhpMyAdmin\SqlParser\Statements\CreateStatement; |
10
|
|
|
use PhpMyAdmin\SqlParser\Tests\TestCase; |
11
|
|
|
|
12
|
|
|
class CreateDefinitionTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testParse(): void |
15
|
|
|
{ |
16
|
|
|
$component = CreateDefinition::parse( |
17
|
|
|
new Parser(), |
18
|
|
|
$this->getTokensList('(str TEXT, FULLTEXT INDEX indx (str))') |
19
|
|
|
); |
20
|
|
|
$this->assertEquals('str', $component[0]->name); |
21
|
|
|
$this->assertEquals('FULLTEXT INDEX', $component[1]->key->type); |
22
|
|
|
$this->assertEquals('indx', $component[1]->key->name); |
23
|
|
|
$this->assertEquals('FULLTEXT INDEX `indx` (`str`)', (string) $component[1]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testParse2(): void |
27
|
|
|
{ |
28
|
|
|
$component = CreateDefinition::parse( |
29
|
|
|
new Parser(), |
30
|
|
|
$this->getTokensList('(str TEXT NOT NULL INVISIBLE)') |
31
|
|
|
); |
32
|
|
|
$this->assertEquals('str', $component[0]->name); |
33
|
|
|
$this->assertEquals('TEXT', $component[0]->type->name); |
34
|
|
|
$this->assertTrue($component[0]->options->has('INVISIBLE')); |
|
|
|
|
35
|
|
|
$this->assertTrue($component[0]->options->has('NOT NULL')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testParseErr1(): void |
39
|
|
|
{ |
40
|
|
|
$parser = new Parser(); |
41
|
|
|
$component = CreateDefinition::parse( |
42
|
|
|
$parser, |
43
|
|
|
$this->getTokensList('(str TEXT, FULLTEXT INDEX indx (str)') |
44
|
|
|
); |
45
|
|
|
$this->assertCount(2, $component); |
46
|
|
|
|
47
|
|
|
$this->assertEquals( |
48
|
|
|
'A closing bracket was expected.', |
49
|
|
|
$parser->errors[0]->getMessage() |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testParseErr2(): void |
54
|
|
|
{ |
55
|
|
|
$parser = new Parser(); |
56
|
|
|
CreateDefinition::parse( |
57
|
|
|
$parser, |
58
|
|
|
$this->getTokensList(')') |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$this->assertEquals( |
62
|
|
|
'An opening bracket was expected.', |
63
|
|
|
$parser->errors[0]->getMessage() |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testBuild(): void |
68
|
|
|
{ |
69
|
|
|
$parser = new Parser( |
70
|
|
|
'CREATE TABLE `payment` (' . |
71
|
|
|
'-- snippet' . "\n" . |
72
|
|
|
'`customer_id` smallint(5) unsigned NOT NULL,' . |
73
|
|
|
'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) ' . |
74
|
|
|
'REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' . |
75
|
|
|
') ENGINE=InnoDB"' |
76
|
|
|
); |
77
|
|
|
$this->assertInstanceOf(CreateStatement::class, $parser->statements[0]); |
78
|
|
|
$this->assertEquals( |
79
|
|
|
'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) ' . |
80
|
|
|
'REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE', |
81
|
|
|
CreateDefinition::build($parser->statements[0]->fields[1]) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testBuild2(): void |
86
|
|
|
{ |
87
|
|
|
$parser = new Parser( |
88
|
|
|
'CREATE TABLE `payment` (' . |
89
|
|
|
'-- snippet' . "\n" . |
90
|
|
|
'`customer_id` smallint(5) unsigned NOT NULL,' . |
91
|
|
|
'`customer_data` longtext CHARACTER SET utf8mb4 CHARSET utf8mb4_bin NOT NULL ' . |
92
|
|
|
'CHECK (json_valid(customer_data)),CONSTRAINT `fk_payment_customer` FOREIGN KEY ' . |
93
|
|
|
'(`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' . |
94
|
|
|
') ENGINE=InnoDB"' |
95
|
|
|
); |
96
|
|
|
$this->assertInstanceOf(CreateStatement::class, $parser->statements[0]); |
97
|
|
|
$this->assertEquals( |
98
|
|
|
'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) ' . |
99
|
|
|
'REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE', |
100
|
|
|
CreateDefinition::build($parser->statements[0]->fields[2]) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testBuild3(): void |
105
|
|
|
{ |
106
|
|
|
$parser = new Parser( |
107
|
|
|
'DROP TABLE IF EXISTS `searches`;' |
108
|
|
|
. 'CREATE TABLE `searches` (' |
109
|
|
|
. ' `id` int(10) unsigned NOT NULL AUTO_INCREMENT,' |
110
|
|
|
. ' `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,' |
111
|
|
|
. ' `public_name` varchar(120) COLLATE utf8_unicode_ci NOT NULL,' |
112
|
|
|
. ' `group_id` smallint(5) unsigned NOT NULL DEFAULT \'0\',' |
113
|
|
|
. ' `shortdesc` tinytext COLLATE utf8_unicode_ci,' |
114
|
|
|
. ' `show_separators` tinyint(1) NOT NULL DEFAULT \'0\',' |
115
|
|
|
. ' `show_separators_two` tinyint(1) NOT NULL DEFAULT FALSE,' |
116
|
|
|
. ' `deleted` tinyint(1) NOT NULL DEFAULT \'0\',' |
117
|
|
|
. ' PRIMARY KEY (`id`)' |
118
|
|
|
. ') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;' |
119
|
|
|
. '' |
120
|
|
|
. 'ALTER TABLE `searches` ADD `admins_only` BOOLEAN NOT NULL DEFAULT FALSE AFTER `show_separators`;' |
121
|
|
|
); |
122
|
|
|
$this->assertInstanceOf(CreateStatement::class, $parser->statements[1]); |
123
|
|
|
$this->assertEquals( |
124
|
|
|
'`public_name` varchar(120) COLLATE utf8_unicode_ci NOT NULL', |
125
|
|
|
CreateDefinition::build($parser->statements[1]->fields[2]) |
126
|
|
|
); |
127
|
|
|
$this->assertEquals( |
128
|
|
|
'`show_separators` tinyint(1) NOT NULL DEFAULT \'0\'', |
129
|
|
|
CreateDefinition::build($parser->statements[1]->fields[5]) |
130
|
|
|
); |
131
|
|
|
$this->assertEquals( |
132
|
|
|
'`show_separators_two` tinyint(1) NOT NULL DEFAULT FALSE', |
133
|
|
|
CreateDefinition::build($parser->statements[1]->fields[6]) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testBuildWithInvisibleKeyword(): void |
138
|
|
|
{ |
139
|
|
|
$parser = new Parser( |
140
|
|
|
'CREATE TABLE `payment` (' . |
141
|
|
|
'-- snippet' . "\n" . |
142
|
|
|
'`customer_id` smallint(5) unsigned NOT NULL INVISIBLE,' . |
143
|
|
|
'`customer_data` longtext CHARACTER SET utf8mb4 CHARSET utf8mb4_bin NOT NULL ' . |
144
|
|
|
'CHECK (json_valid(customer_data)),CONSTRAINT `fk_payment_customer` FOREIGN KEY ' . |
145
|
|
|
'(`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' . |
146
|
|
|
') ENGINE=InnoDB"' |
147
|
|
|
); |
148
|
|
|
$this->assertInstanceOf(CreateStatement::class, $parser->statements[0]); |
149
|
|
|
$this->assertEquals( |
150
|
|
|
'`customer_id` smallint(5) UNSIGNED NOT NULL INVISIBLE', |
151
|
|
|
CreateDefinition::build($parser->statements[0]->fields[0]) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testBuildWithCompressed() |
156
|
|
|
{ |
157
|
|
|
$query = 'CREATE TABLE `user` ( `message2` TEXT COMPRESSED )'; |
158
|
|
|
$parser = new Parser($query); |
159
|
|
|
$stmt = $parser->statements[0]; |
160
|
|
|
$this->assertEquals("CREATE TABLE `user` (\n `message2` text COMPRESSED\n) ", $stmt->build()); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.