|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Graze\Morphism\Parse; |
|
4
|
|
|
|
|
5
|
|
|
use Graze\Morphism\Test\Parse\TestCase; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
|
|
8
|
|
|
class TableOptionsTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @dataProvider providerSetDefaultEngine |
|
12
|
|
|
* @param string $engine |
|
13
|
|
|
* @param string $expected |
|
14
|
|
|
*/ |
|
15
|
|
|
public function testSetDefaultEngine($engine, $expected) |
|
16
|
|
|
{ |
|
17
|
|
|
$stream = $this->makeStream(''); |
|
18
|
|
|
$options = new TableOptions(new CollationInfo()); |
|
19
|
|
|
$options->setDefaultEngine($engine); |
|
20
|
|
|
$options->parse($stream); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertSame($expected, $options->toString()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return array |
|
27
|
|
|
*/ |
|
28
|
|
|
public function providerSetDefaultEngine() |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
['innodb', 'ENGINE=InnoDB'], |
|
32
|
|
|
['myisam', 'ENGINE=MyISAM'], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @dataProvider parseProvider |
|
38
|
|
|
* @param string $text |
|
39
|
|
|
* @param string $expected |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testParse($text, $expected) |
|
42
|
|
|
{ |
|
43
|
|
|
$stream = $this->makeStream($text); |
|
44
|
|
|
|
|
45
|
|
|
$options = new TableOptions(new CollationInfo()); |
|
46
|
|
|
$options->setDefaultEngine('InnoDB'); |
|
47
|
|
|
$options->parse($stream); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertSame($expected, $options->toString()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function parseProvider() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
["auto_increment=123", "ENGINE=InnoDB"], |
|
59
|
|
|
|
|
60
|
|
|
["CHARSET default", "ENGINE=InnoDB"], |
|
61
|
|
|
["CHARSET utf8", "ENGINE=InnoDB DEFAULT CHARSET=utf8"], |
|
62
|
|
|
["CHARSET=utf8", "ENGINE=InnoDB DEFAULT CHARSET=utf8"], |
|
63
|
|
|
["CHARACTER SET=utf8", "ENGINE=InnoDB DEFAULT CHARSET=utf8"], |
|
64
|
|
|
["DEFAULT CHARSET=utf8", "ENGINE=InnoDB DEFAULT CHARSET=utf8"], |
|
65
|
|
|
["DEFAULT CHARACTER SET=utf8", "ENGINE=InnoDB DEFAULT CHARSET=utf8"], |
|
66
|
|
|
|
|
67
|
|
|
["COLLATE default", "ENGINE=InnoDB"], |
|
68
|
|
|
["COLLATE utf8_bin", "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"], |
|
69
|
|
|
["DEFAULT COLLATE=utf8_bin", "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"], |
|
70
|
|
|
|
|
71
|
|
|
["Min_rows=0", "ENGINE=InnoDB"], |
|
72
|
|
|
["Min_rows=123", "ENGINE=InnoDB MIN_ROWS=123"], |
|
73
|
|
|
|
|
74
|
|
|
["Max_Rows=0", "ENGINE=InnoDB"], |
|
75
|
|
|
["Max_Rows=123", "ENGINE=InnoDB MAX_ROWS=123"], |
|
76
|
|
|
|
|
77
|
|
|
["avg_row_length=0", "ENGINE=InnoDB"], |
|
78
|
|
|
["avg_row_length=123", "ENGINE=InnoDB AVG_ROW_LENGTH=123"], |
|
79
|
|
|
|
|
80
|
|
|
["PACK_KEYS=DEFAULT", "ENGINE=InnoDB"], |
|
81
|
|
|
["PACK_KEYS=0", "ENGINE=InnoDB PACK_KEYS=0"], |
|
82
|
|
|
["PACK_KEYS=1", "ENGINE=InnoDB PACK_KEYS=1"], |
|
83
|
|
|
|
|
84
|
|
|
["CHECKSUM=0", "ENGINE=InnoDB"], |
|
85
|
|
|
["CHECKSUM=1", "ENGINE=InnoDB CHECKSUM=1"], |
|
86
|
|
|
|
|
87
|
|
|
["delay_key_write=0", "ENGINE=InnoDB"], |
|
88
|
|
|
["delay_key_write=1", "ENGINE=InnoDB DELAY_KEY_WRITE=1"], |
|
89
|
|
|
|
|
90
|
|
|
["row_format=default", "ENGINE=InnoDB"], |
|
91
|
|
|
["row_format=dynamic", "ENGINE=InnoDB ROW_FORMAT=DYNAMIC"], |
|
92
|
|
|
["row_format=fixed", "ENGINE=InnoDB ROW_FORMAT=FIXED"], |
|
93
|
|
|
["row_format=compressed", "ENGINE=InnoDB ROW_FORMAT=COMPRESSED"], |
|
94
|
|
|
["row_format=redundant", "ENGINE=InnoDB ROW_FORMAT=REDUNDANT"], |
|
95
|
|
|
["row_format=compact", "ENGINE=InnoDB ROW_FORMAT=COMPACT"], |
|
96
|
|
|
|
|
97
|
|
|
["key_block_size=0", "ENGINE=InnoDB"], |
|
98
|
|
|
["key_block_size=123", "ENGINE=InnoDB KEY_BLOCK_SIZE=123"], |
|
99
|
|
|
|
|
100
|
|
|
["comment=''", "ENGINE=InnoDB"], |
|
101
|
|
|
["comment='hello world'", "ENGINE=InnoDB COMMENT='hello world'"], |
|
102
|
|
|
["comment 'hello world'", "ENGINE=InnoDB COMMENT='hello world'"], |
|
103
|
|
|
|
|
104
|
|
|
["connection=''", "ENGINE=InnoDB"], |
|
105
|
|
|
["connection='hello world'", "ENGINE=InnoDB CONNECTION='hello world'"], |
|
106
|
|
|
["connection 'hello world'", "ENGINE=InnoDB CONNECTION='hello world'"], |
|
107
|
|
|
|
|
108
|
|
|
["DATA DIRECTORY='blah'", "ENGINE=InnoDB"], |
|
109
|
|
|
["INDEX DIRECTORY='blah'", "ENGINE=InnoDB"], |
|
110
|
|
|
["PASSWORD='blah'", "ENGINE=InnoDB"], |
|
111
|
|
|
|
|
112
|
|
|
["engine=innodb", "ENGINE=InnoDB"], |
|
113
|
|
|
["engine=myisam", "ENGINE=MyISAM"], |
|
114
|
|
|
["engine=foo", "ENGINE=FOO"], |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @dataProvider badParseProvider |
|
120
|
|
|
* @param string $text |
|
121
|
|
|
* @expectedException RuntimeException |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testBadParse($text) |
|
124
|
|
|
{ |
|
125
|
|
|
$stream = $this->makeStream($text); |
|
126
|
|
|
|
|
127
|
|
|
$options = new TableOptions(new CollationInfo()); |
|
128
|
|
|
$options->setDefaultEngine('InnoDB'); |
|
129
|
|
|
$options->parse($stream); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
public function badParseProvider() |
|
136
|
|
|
{ |
|
137
|
|
|
return [ |
|
138
|
|
|
// Invalid option for DEFAULT keyword |
|
139
|
|
|
["default foo"], |
|
140
|
|
|
// These three INSERT_METHOD variants are valid but not supported |
|
141
|
|
|
["insert_method=no"], |
|
142
|
|
|
["insert_method=first"], |
|
143
|
|
|
["insert_method=last"], |
|
144
|
|
|
// This INSERT_METHOD variant is invalid |
|
145
|
|
|
["insert_method=foo"], |
|
146
|
|
|
// These options are unsupported |
|
147
|
|
|
["stats_sample_pages"], |
|
148
|
|
|
["stats_auto_recalc"], |
|
149
|
|
|
["stats_persistent"], |
|
150
|
|
|
["tablespace"], |
|
151
|
|
|
["union"], |
|
152
|
|
|
["partition"], |
|
153
|
|
|
// Any other options are unsupported |
|
154
|
|
|
["foo"], |
|
155
|
|
|
// Missing value |
|
156
|
|
|
["engine="], |
|
157
|
|
|
// Invalid value for option that expects a single value |
|
158
|
|
|
["engine=("], |
|
159
|
|
|
// Invalid value for an option that expects a set of values |
|
160
|
|
|
["row_format=("], |
|
161
|
|
|
]; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $firstTableText |
|
166
|
|
|
* @param string $secondTableText |
|
167
|
|
|
* @param string $expected |
|
168
|
|
|
* @dataProvider diffProvider |
|
169
|
|
|
*/ |
|
170
|
|
|
public function testDiff($firstTableText, $secondTableText, $expected) |
|
171
|
|
|
{ |
|
172
|
|
|
$firstStream = $this->makeStream($firstTableText); |
|
173
|
|
|
|
|
174
|
|
|
$firstTableOptions = new TableOptions(new CollationInfo()); |
|
175
|
|
|
$firstTableOptions->setDefaultEngine('InnoDB'); |
|
176
|
|
|
$firstTableOptions->parse($firstStream); |
|
177
|
|
|
|
|
178
|
|
|
$secondStream = $this->makeStream($secondTableText); |
|
179
|
|
|
|
|
180
|
|
|
$secondTableOptions = new TableOptions(new CollationInfo()); |
|
181
|
|
|
$secondTableOptions->setDefaultEngine('InnoDB'); |
|
182
|
|
|
$secondTableOptions->parse($secondStream); |
|
183
|
|
|
|
|
184
|
|
|
$diff = $firstTableOptions->diff($secondTableOptions); |
|
185
|
|
|
|
|
186
|
|
|
$this->assertSame($expected, $diff); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @return array |
|
191
|
|
|
*/ |
|
192
|
|
|
public function diffProvider() |
|
193
|
|
|
{ |
|
194
|
|
|
return [ |
|
195
|
|
|
// [ 1st table option string, 2nd table option string, expected result ] |
|
196
|
|
|
|
|
197
|
|
|
// AVG_ROW_LENGTH |
|
198
|
|
|
["", "avg_row_length=100", "AVG_ROW_LENGTH=100"], |
|
199
|
|
|
["avg_row_length=200", "", "AVG_ROW_LENGTH=0"], |
|
200
|
|
|
["avg_row_length=100", "avg_row_length=200", "AVG_ROW_LENGTH=200"], |
|
201
|
|
|
|
|
202
|
|
|
// CHARSET |
|
203
|
|
|
["", "charset latin1", "DEFAULT CHARSET=latin1"], |
|
204
|
|
|
["charset latin1", "", ""], |
|
205
|
|
|
["charset default", "charset utf8", "DEFAULT CHARSET=utf8"], |
|
206
|
|
|
|
|
207
|
|
|
// COLLATE |
|
208
|
|
|
["", "collate=utf8_unicode_ci", "DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"], |
|
209
|
|
|
["collate=utf8_unicode_ci", "", ""], |
|
210
|
|
|
["collate=utf8_unicode_ci", "collate=latin1_german1_ci", "DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci"], |
|
211
|
|
|
|
|
212
|
|
|
// CHECKSUM |
|
213
|
|
|
["", "checksum=1", "CHECKSUM=1"], |
|
214
|
|
|
["checksum=1", "", "CHECKSUM=0"], |
|
215
|
|
|
["checksum=1", "checksum=0", "CHECKSUM=0"], |
|
216
|
|
|
|
|
217
|
|
|
// COMMENT |
|
218
|
|
|
["comment 'hello'", "", "COMMENT=''"], |
|
219
|
|
|
["", "comment 'hello'", "COMMENT='hello'"], |
|
220
|
|
|
["comment 'hello'", "comment 'goodbye'", "COMMENT='goodbye'"], |
|
221
|
|
|
|
|
222
|
|
|
// CONNECTION |
|
223
|
|
|
["connection 'foo'", "", "CONNECTION=''" ], |
|
224
|
|
|
["", "connection 'foo'", "CONNECTION='foo'"], |
|
225
|
|
|
["connection 'foo'", "connection 'bar'", "CONNECTION='bar'"], |
|
226
|
|
|
|
|
227
|
|
|
// DELAY_KEY_WRITE |
|
228
|
|
|
["", "delay_key_write=1", "DELAY_KEY_WRITE=1"], |
|
229
|
|
|
["delay_key_write=1", "", "DELAY_KEY_WRITE=0"], |
|
230
|
|
|
["delay_key_write=1", "delay_key_write=0", "DELAY_KEY_WRITE=0"], |
|
231
|
|
|
|
|
232
|
|
|
// ENGINE |
|
233
|
|
|
["", "engine=memory", "ENGINE=MEMORY"], |
|
234
|
|
|
["engine=InnoDB", "", ""], |
|
235
|
|
|
["engine=InnoDB", "engine=MyISAM", "ENGINE=MyISAM"], |
|
236
|
|
|
|
|
237
|
|
|
// KEY_BLOCK_SIZE |
|
238
|
|
|
["", "key_block_size=4", "KEY_BLOCK_SIZE=4"], |
|
239
|
|
|
["key_block_size=8", "", "KEY_BLOCK_SIZE=0"], |
|
240
|
|
|
["key_block_size=4", "key_block_size=16", "KEY_BLOCK_SIZE=16"], |
|
241
|
|
|
|
|
242
|
|
|
// MAX_ROWS |
|
243
|
|
|
["", "max_rows=100", "MAX_ROWS=100"], |
|
244
|
|
|
["max_rows=200", "", "MAX_ROWS=0"], |
|
245
|
|
|
["max_rows=100", "max_rows=200", "MAX_ROWS=200"], |
|
246
|
|
|
|
|
247
|
|
|
// MIN_ROWS |
|
248
|
|
|
["", "min_rows=100", "MIN_ROWS=100"], |
|
249
|
|
|
["min_rows=200", "", "MIN_ROWS=0"], |
|
250
|
|
|
["min_rows=100", "min_rows=200", "MIN_ROWS=200"], |
|
251
|
|
|
|
|
252
|
|
|
// PACK_KEYS |
|
253
|
|
|
["", "pack_keys=1", "PACK_KEYS=1"], |
|
254
|
|
|
["pack_keys=0", "", "PACK_KEYS=DEFAULT"], |
|
255
|
|
|
["pack_keys=1", "pack_keys=default", "PACK_KEYS=DEFAULT"], |
|
256
|
|
|
|
|
257
|
|
|
/* |
|
258
|
|
|
* Everything below here is currently unsupported by the |
|
259
|
|
|
* diff() function and so has an expected value of an empty string. |
|
260
|
|
|
* These should be moved up as and when support is added. |
|
261
|
|
|
*/ |
|
262
|
|
|
|
|
263
|
|
|
// AUTO_INCREMENT |
|
264
|
|
|
["", "auto_increment 1", ""], |
|
265
|
|
|
["", "auto_increment 5", ""], |
|
266
|
|
|
["auto_increment 1", "", ""], |
|
267
|
|
|
["auto_increment 3", "auto_increment 5", ""], |
|
268
|
|
|
|
|
269
|
|
|
// ROW_FORMAT |
|
270
|
|
|
["", "row_format=compressed", ""], |
|
271
|
|
|
["row_format=fixed", "", ""], |
|
272
|
|
|
["row_format=compact", "row_format=redundant", ""] |
|
273
|
|
|
]; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Test that disabling "alterEngine" works. |
|
278
|
|
|
*/ |
|
279
|
|
|
public function testNoAlterEngineDiff() |
|
280
|
|
|
{ |
|
281
|
|
|
$firstTableOptions = new TableOptions(new CollationInfo()); |
|
282
|
|
|
$firstTableOptions->parse($this->makeStream("engine=MyISAM")); |
|
283
|
|
|
|
|
284
|
|
|
$secondTableOptions = new TableOptions(new CollationInfo()); |
|
285
|
|
|
$secondTableOptions->parse($this->makeStream("engine=MEMORY")); |
|
286
|
|
|
|
|
287
|
|
|
$diff = $firstTableOptions->diff($secondTableOptions, ['alterEngine' => false]); |
|
288
|
|
|
|
|
289
|
|
|
$this->assertEmpty($diff); |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|