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