1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\Morphism\Parse; |
4
|
|
|
|
5
|
|
|
use Graze\Morphism\Test\Parse\TestCase; |
6
|
|
|
use Mockery; |
7
|
|
|
|
8
|
|
|
class CreateDatabaseTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param string $text |
12
|
|
|
* @param string $expectedName |
13
|
|
|
* @dataProvider parseNameProvider |
14
|
|
|
*/ |
15
|
|
|
public function testParseName($text, $expectedName) |
16
|
|
|
{ |
17
|
|
|
$stream = $this->makeStream($text); |
18
|
|
|
$database = new CreateDatabase(new CollationInfo()); |
19
|
|
|
$database->parse($stream); |
20
|
|
|
|
21
|
|
|
$this->assertEquals($expectedName, $database->name); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function parseNameProvider() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
[ 'create database foo', 'foo' ], |
31
|
|
|
[ 'create database if not exists bar', 'bar' ], |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $text |
37
|
|
|
* @param string $expectedCharset |
38
|
|
|
* @dataProvider parseCharsetProvider |
39
|
|
|
*/ |
40
|
|
|
public function testParseCharset($text, $expectedCharset) |
41
|
|
|
{ |
42
|
|
|
$stream = $this->makeStream('create database foo ' . $text); |
43
|
|
|
$database = new CreateDatabase(new CollationInfo()); |
44
|
|
|
$database->parse($stream); |
45
|
|
|
|
46
|
|
|
if ($database->getCollation()->isSpecified()) { |
47
|
|
|
$charset = $database->getCollation()->getCharset(); |
48
|
|
|
} else { |
49
|
|
|
$charset = null; |
50
|
|
|
} |
51
|
|
|
$this->assertEquals($expectedCharset, $charset); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function parseCharsetProvider() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
[ 'charset = default', null ], |
61
|
|
|
[ 'character set = default', null ], |
62
|
|
|
|
63
|
|
|
[ 'charset = utf8', 'utf8' ], |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $text |
69
|
|
|
* @param string $expectedCollation |
70
|
|
|
* @dataProvider parseCollationProvider |
71
|
|
|
*/ |
72
|
|
|
public function testParseCollation($text, $expectedCollation) |
73
|
|
|
{ |
74
|
|
|
$stream = $this->makeStream('create database foo ' . $text); |
75
|
|
|
$database = new CreateDatabase(new CollationInfo()); |
76
|
|
|
$database->parse($stream); |
77
|
|
|
|
78
|
|
|
if ($database->getCollation()->isSpecified()) { |
79
|
|
|
$collation = $database->getCollation()->getCollation(); |
80
|
|
|
} else { |
81
|
|
|
$collation = null; |
82
|
|
|
} |
83
|
|
|
$this->assertEquals($expectedCollation, $collation); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function parseCollationProvider() |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
[ 'collate = default', null ], |
93
|
|
|
[ 'collate = utf8_general_ci', 'utf8_general_ci' ], |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @expectedException \RuntimeException |
99
|
|
|
*/ |
100
|
|
|
public function testBadParse() |
101
|
|
|
{ |
102
|
|
|
$stream = $this->makeStream('foo'); |
103
|
|
|
$database = new CreateDatabase(new CollationInfo()); |
104
|
|
|
$database->parse($stream); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function badParseProvider() |
111
|
|
|
{ |
112
|
|
|
return [ |
113
|
|
|
// Not a 'create database' statement |
114
|
|
|
[ 'foo' ], |
115
|
|
|
|
116
|
|
|
// No database name |
117
|
|
|
[ 'create database' ], |
118
|
|
|
|
119
|
|
|
// invalid character set syntax |
120
|
|
|
[ 'create database foo default charset utf8' ], |
121
|
|
|
|
122
|
|
|
// invalid character set |
123
|
|
|
[ 'create database foo default charset = bar' ], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testAddTable() |
128
|
|
|
{ |
129
|
|
|
$tableName = 'foo'; |
130
|
|
|
|
131
|
|
|
$createTable = Mockery::mock(CreateTable::class); |
132
|
|
|
$createTable->shouldReceive('getName')->andReturn($tableName); |
133
|
|
|
|
134
|
|
|
$database = new CreateDatabase(Mockery::mock(CollationInfo::class)); |
|
|
|
|
135
|
|
|
$database->addTable($createTable); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$this->assertArrayHasKey($tableName, $database->tables); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @expectedException \RuntimeException |
142
|
|
|
*/ |
143
|
|
|
public function testBadAddTable() |
144
|
|
|
{ |
145
|
|
|
$createTable = new CreateTable(new CollationInfo()); |
146
|
|
|
$database = new CreateDatabase(new CollationInfo()); |
147
|
|
|
$database->addTable($createTable); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param CreateDatabase $database |
152
|
|
|
* @param string $sql |
153
|
|
|
* @dataProvider testGetDDLProvider |
154
|
|
|
*/ |
155
|
|
|
public function testGetDDL(CreateDatabase $database, $sql) |
156
|
|
|
{ |
157
|
|
|
$ddl = $database->getDDL(); |
158
|
|
|
|
159
|
|
|
$this->assertInternalType('array', $ddl); |
|
|
|
|
160
|
|
|
$this->assertEquals(1, count($ddl)); |
161
|
|
|
$this->assertEquals($sql, $ddl[0]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
|
|
public function testGetDDLProvider() |
168
|
|
|
{ |
169
|
|
|
$testCases = []; |
170
|
|
|
|
171
|
|
|
// Basic database creation with out any specific collation |
172
|
|
|
$database = new CreateDatabase(new CollationInfo()); |
173
|
|
|
$database->name = 'alpha'; |
174
|
|
|
$testCases[] = [ |
175
|
|
|
$database, |
176
|
|
|
'CREATE DATABASE IF NOT EXISTS `alpha`' |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
// Explicit charset specified |
180
|
|
|
$database = new CreateDatabase(new CollationInfo('utf8')); |
181
|
|
|
$database->name = 'beta'; |
182
|
|
|
$testCases[] = [ |
183
|
|
|
$database, |
184
|
|
|
'CREATE DATABASE IF NOT EXISTS `beta` DEFAULT CHARACTER SET utf8' |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
|
// Explicit collation and charset specified |
188
|
|
|
$database = new CreateDatabase(new CollationInfo('utf8', 'utf8_unicode_ci')); |
189
|
|
|
$database->name = 'gamma'; |
190
|
|
|
$testCases[] = [ |
191
|
|
|
$database, |
192
|
|
|
'CREATE DATABASE IF NOT EXISTS `gamma` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci' |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
return $testCases; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @expectedException \RuntimeException |
200
|
|
|
*/ |
201
|
|
|
public function testBadGetDDL() |
202
|
|
|
{ |
203
|
|
|
$database = new CreateDatabase(new CollationInfo()); |
204
|
|
|
$database->getDDL(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param CreateDatabase $db1 |
209
|
|
|
* @param CreateDatabase $db2 |
210
|
|
|
* @param array $flags |
211
|
|
|
* @param array $expected |
212
|
|
|
* @dataProvider diffProvider |
213
|
|
|
*/ |
214
|
|
|
public function testDiff(CreateDatabase $db1, CreateDatabase $db2, array $flags, array $expected) |
215
|
|
|
{ |
216
|
|
|
$diff = $db1->diff($db2, $flags); |
217
|
|
|
|
218
|
|
|
$this->assertEquals($expected, $diff); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
public function diffProvider() |
225
|
|
|
{ |
226
|
|
|
// [ |
227
|
|
|
// [ create database 1, create database 2, diff flags, array of statements to execute ], |
228
|
|
|
// ... |
229
|
|
|
// ] |
230
|
|
|
$testCases = []; |
231
|
|
|
|
232
|
|
|
/** @var CollationInfo|Mockery\MockInterface $collationInfo */ |
233
|
|
|
$collationInfo = Mockery::mock(CollationInfo::class); |
234
|
|
|
$collationInfo->shouldReceive('isSpecified'); |
235
|
|
|
|
236
|
|
|
// Completely empty objects |
237
|
|
|
$testCases[] = [ |
238
|
|
|
new CreateDatabase($collationInfo), |
|
|
|
|
239
|
|
|
new CreateDatabase($collationInfo), |
240
|
|
|
[], |
241
|
|
|
[] |
242
|
|
|
]; |
243
|
|
|
|
244
|
|
|
// Named databases |
245
|
|
|
// Morphism does not support renaming databases |
246
|
|
|
$db1 = new CreateDatabase($collationInfo); |
247
|
|
|
$db1->name = 'foo'; |
248
|
|
|
|
249
|
|
|
$db2 = new CreateDatabase($collationInfo); |
250
|
|
|
$db2->name = 'bar'; |
251
|
|
|
|
252
|
|
|
$testCases[] = [ |
253
|
|
|
$db1, |
254
|
|
|
$db2, |
255
|
|
|
[], |
256
|
|
|
[] |
257
|
|
|
]; |
258
|
|
|
|
259
|
|
|
// Table added |
260
|
|
|
$db1 = new CreateDatabase($collationInfo); |
261
|
|
|
|
262
|
|
|
/** @var CreateTable|Mockery\MockInterface $tableA */ |
263
|
|
|
$tableA = Mockery::mock(CreateTable::class); |
264
|
|
|
$tableA->shouldReceive('getName')->andReturn('t'); |
265
|
|
|
$tableA->shouldReceive('getDDL')->andReturn(["CREATE TABLE `t` (\n `a` int(11) DEFAULT NULL\n) ENGINE=E"]); |
266
|
|
|
|
267
|
|
|
$db2 = new CreateDatabase($collationInfo); |
268
|
|
|
$db2->addTable($tableA); |
|
|
|
|
269
|
|
|
|
270
|
|
|
$testCases[] = [ |
271
|
|
|
$db1, |
272
|
|
|
$db2, |
273
|
|
|
[], |
274
|
|
|
["CREATE TABLE `t` (\n `a` int(11) DEFAULT NULL\n) ENGINE=E"] |
275
|
|
|
]; |
276
|
|
|
|
277
|
|
|
// Table added |
278
|
|
|
$db1 = new CreateDatabase($collationInfo); |
279
|
|
|
|
280
|
|
|
$db2 = new CreateDatabase($collationInfo); |
281
|
|
|
$db2->addTable($tableA); |
282
|
|
|
|
283
|
|
|
$testCases[] = [ |
284
|
|
|
$db1, |
285
|
|
|
$db2, |
286
|
|
|
[], |
287
|
|
|
["CREATE TABLE `t` (\n `a` int(11) DEFAULT NULL\n) ENGINE=E"] |
288
|
|
|
]; |
289
|
|
|
|
290
|
|
|
// Table added (but ignored) |
291
|
|
|
$db1 = new CreateDatabase($collationInfo); |
292
|
|
|
|
293
|
|
|
$db2 = new CreateDatabase($collationInfo); |
294
|
|
|
$db2->addTable($tableA); |
295
|
|
|
|
296
|
|
|
$testCases[] = [ |
297
|
|
|
$db1, |
298
|
|
|
$db2, |
299
|
|
|
['createTable' => false], |
300
|
|
|
[] |
301
|
|
|
]; |
302
|
|
|
|
303
|
|
|
// Table removed |
304
|
|
|
$db1 = new CreateDatabase($collationInfo); |
305
|
|
|
$db1->addTable($tableA); |
306
|
|
|
|
307
|
|
|
$db2 = new CreateDatabase($collationInfo); |
308
|
|
|
|
309
|
|
|
$testCases[] = [ |
310
|
|
|
$db1, |
311
|
|
|
$db2, |
312
|
|
|
[], |
313
|
|
|
["DROP TABLE IF EXISTS `t`"] |
314
|
|
|
]; |
315
|
|
|
|
316
|
|
|
// Table removed (but ignored) |
317
|
|
|
$db1 = new CreateDatabase($collationInfo); |
318
|
|
|
$db1->addTable($tableA); |
319
|
|
|
|
320
|
|
|
$db2 = new CreateDatabase($collationInfo); |
321
|
|
|
|
322
|
|
|
$testCases[] = [ |
323
|
|
|
$db1, |
324
|
|
|
$db2, |
325
|
|
|
['dropTable' => false], |
326
|
|
|
[] |
327
|
|
|
]; |
328
|
|
|
|
329
|
|
|
// Engine changed |
330
|
|
|
/** @var CreateTable|Mockery\MockInterface $tableWithEngineBar */ |
331
|
|
|
$tableWithEngineBar = Mockery::mock(CreateTable::class); |
332
|
|
|
$tableWithEngineBar->shouldReceive('getName')->andReturn('t'); |
333
|
|
|
$tableWithEngineBar->shouldReceive('getDDL')->andReturn(["CREATE TABLE `t` (\n `a` int(11) DEFAULT NULL\n) ENGINE=BAR"]); |
334
|
|
|
|
335
|
|
|
/** @var CreateTable|Mockery\MockInterface $tableWithEngineFoo */ |
336
|
|
|
$tableWithEngineFoo = Mockery::mock(CreateTable::class); |
337
|
|
|
$tableWithEngineFoo->shouldReceive('getName')->andReturn('t'); |
338
|
|
|
$tableWithEngineFoo->shouldReceive('getDDL')->andReturn(["CREATE TABLE `t` (\n `a` int(11) DEFAULT NULL\n) ENGINE=FOO"]); |
339
|
|
|
$tableWithEngineFoo |
340
|
|
|
->shouldReceive('diff') |
341
|
|
|
->with( |
342
|
|
|
$tableWithEngineBar, |
343
|
|
|
['alterEngine' => true] |
344
|
|
|
) |
345
|
|
|
->andReturn(["ALTER TABLE `t`\nENGINE=BAR"]); |
346
|
|
|
|
347
|
|
|
$db1 = new CreateDatabase($collationInfo); |
348
|
|
|
$db1->addTable($tableWithEngineFoo); |
349
|
|
|
|
350
|
|
|
$db2 = new CreateDatabase($collationInfo); |
351
|
|
|
$db2->addTable($tableWithEngineBar); |
352
|
|
|
|
353
|
|
|
$testCases[] = [ |
354
|
|
|
$db1, |
355
|
|
|
$db2, |
356
|
|
|
[], |
357
|
|
|
["ALTER TABLE `t`\nENGINE=BAR"] |
358
|
|
|
]; |
359
|
|
|
|
360
|
|
|
// Engine changed (but ignored) |
361
|
|
|
$db1 = new CreateDatabase($collationInfo); |
362
|
|
|
$db1->addTable($tableWithEngineFoo); |
363
|
|
|
|
364
|
|
|
$tableWithEngineFoo |
365
|
|
|
->shouldReceive('diff') |
|
|
|
|
366
|
|
|
->with( |
367
|
|
|
$tableWithEngineBar, |
368
|
|
|
['alterEngine' => false] |
369
|
|
|
) |
370
|
|
|
->andReturn([]); |
371
|
|
|
|
372
|
|
|
$db2 = new CreateDatabase($collationInfo); |
373
|
|
|
$db2->addTable($tableWithEngineBar); |
374
|
|
|
|
375
|
|
|
$testCases[] = [ |
376
|
|
|
$db1, |
377
|
|
|
$db2, |
378
|
|
|
['alterEngine' => false], |
379
|
|
|
[] |
380
|
|
|
]; |
381
|
|
|
|
382
|
|
|
return $testCases; |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|