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