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