Total Complexity | 92 |
Total Lines | 1030 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like CommonSchemaTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CommonSchemaTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | abstract class CommonSchemaTest extends AbstractSchemaTest |
||
40 | { |
||
41 | /** |
||
42 | * @throws Exception |
||
43 | * @throws InvalidConfigException |
||
44 | * @throws Throwable |
||
45 | */ |
||
46 | public function testColumnComment(): void |
||
47 | { |
||
48 | $db = $this->getConnection(); |
||
49 | |||
50 | $command = $db->createCommand(); |
||
51 | $schema = $db->getSchema(); |
||
52 | |||
53 | if ($schema->getTableSchema('testCommentTable') !== null) { |
||
54 | $command->dropTable('testCommentTable')->execute(); |
||
55 | } |
||
56 | |||
57 | $command->createTable('testCommentTable', ['bar' => Schema::TYPE_INTEGER,])->execute(); |
||
58 | $command->addCommentOnColumn('testCommentTable', 'bar', 'Test comment for column.')->execute(); |
||
59 | |||
60 | $this->assertSame( |
||
61 | 'Test comment for column.', |
||
62 | $schema->getTableSchema('testCommentTable')->getColumn('bar')->getComment(), |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::columns() |
||
68 | */ |
||
69 | public function testColumnSchema(array $columns, string $tableName): void |
||
70 | { |
||
71 | $this->columnSchema($columns, $tableName); |
||
72 | } |
||
73 | |||
74 | public function testCompositeFk(): void |
||
75 | { |
||
76 | $db = $this->getConnection(true); |
||
77 | |||
78 | $schema = $db->getSchema(); |
||
79 | $table = $schema->getTableSchema('composite_fk'); |
||
80 | |||
81 | $this->assertNotNull($table); |
||
82 | |||
83 | $fk = $table->getForeignKeys(); |
||
84 | |||
85 | $expectedKey = match ($db->getName()) { |
||
86 | 'mysql', 'sqlsrv' => $fk['FK_composite_fk_order_item'], |
||
87 | default => $fk['fk_composite_fk_order_item'], |
||
88 | }; |
||
89 | |||
90 | $this->assertCount(1, $fk); |
||
91 | $this->assertTrue(isset($expectedKey)); |
||
92 | $this->assertSame('order_item', $expectedKey[0]); |
||
93 | $this->assertSame('order_id', $expectedKey['order_id']); |
||
94 | $this->assertSame('item_id', $expectedKey['item_id']); |
||
95 | } |
||
96 | |||
97 | public function testContraintTablesExistance(): void |
||
98 | { |
||
99 | $db = $this->getConnection(true); |
||
100 | |||
101 | $tableNames = ['T_constraints_1', 'T_constraints_2', 'T_constraints_3', 'T_constraints_4']; |
||
102 | $schema = $db->getSchema(); |
||
103 | |||
104 | foreach ($tableNames as $tableName) { |
||
105 | $tableSchema = $schema->getTableSchema($tableName); |
||
106 | $this->assertInstanceOf(TableSchemaInterface::class, $tableSchema, $tableName); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @throws Exception |
||
112 | * @throws InvalidConfigException |
||
113 | * @throws Throwable |
||
114 | */ |
||
115 | public function testFindUniquesIndexes(): void |
||
116 | { |
||
117 | $db = $this->getConnection(); |
||
118 | |||
119 | $command = $db->createCommand(); |
||
120 | $schema = $db->getSchema(); |
||
121 | |||
122 | try { |
||
123 | $command->dropTable('uniqueIndex')->execute(); |
||
124 | } catch (Exception) { |
||
125 | } |
||
126 | |||
127 | $command->createTable( |
||
128 | 'uniqueIndex', |
||
129 | ['somecol' => 'string', 'someCol2' => 'string', 'someCol3' => 'string'], |
||
130 | )->execute(); |
||
131 | $tableSchema = $schema->getTableSchema('uniqueIndex', true); |
||
132 | |||
133 | $this->assertNotNull($tableSchema); |
||
134 | |||
135 | $uniqueIndexes = $schema->findUniqueIndexes($tableSchema); |
||
136 | |||
137 | $this->assertSame([], $uniqueIndexes); |
||
138 | |||
139 | $command->createIndex('somecolUnique', 'uniqueIndex', 'somecol', QueryBuilder::INDEX_UNIQUE)->execute(); |
||
140 | $tableSchema = $schema->getTableSchema('uniqueIndex', true); |
||
141 | |||
142 | $this->assertNotNull($tableSchema); |
||
143 | |||
144 | $uniqueIndexes = $schema->findUniqueIndexes($tableSchema); |
||
145 | |||
146 | $this->assertSame(['somecolUnique' => ['somecol']], $uniqueIndexes); |
||
147 | |||
148 | /** |
||
149 | * Create another column with upper case letter that fails postgres. |
||
150 | * |
||
151 | * @link https://github.com/yiisoft/yii2/issues/10613 |
||
152 | */ |
||
153 | $command->createIndex('someCol2Unique', 'uniqueIndex', 'someCol2', QueryBuilder::INDEX_UNIQUE)->execute(); |
||
154 | $tableSchema = $schema->getTableSchema('uniqueIndex', true); |
||
155 | |||
156 | $this->assertNotNull($tableSchema); |
||
157 | |||
158 | $uniqueIndexes = $schema->findUniqueIndexes($tableSchema); |
||
159 | |||
160 | $this->assertSame(['someCol2Unique' => ['someCol2'], 'somecolUnique' => ['somecol']], $uniqueIndexes); |
||
161 | |||
162 | /** @link https://github.com/yiisoft/yii2/issues/13814 */ |
||
163 | $command->createIndex('another unique index', 'uniqueIndex', 'someCol3', QueryBuilder::INDEX_UNIQUE)->execute(); |
||
164 | $tableSchema = $schema->getTableSchema('uniqueIndex', true); |
||
165 | |||
166 | $this->assertNotNull($tableSchema); |
||
167 | |||
168 | $uniqueIndexes = $schema->findUniqueIndexes($tableSchema); |
||
169 | |||
170 | $this->assertSame( |
||
171 | ['another unique index' => ['someCol3'], 'someCol2Unique' => ['someCol2'], 'somecolUnique' => ['somecol']], |
||
172 | $uniqueIndexes, |
||
173 | ); |
||
174 | } |
||
175 | |||
176 | public function testGetColumnNoExist(): void |
||
177 | { |
||
178 | $db = $this->getConnection(true); |
||
179 | |||
180 | $schema = $db->getSchema(); |
||
181 | $table = $schema->getTableSchema('negative_default_values'); |
||
182 | |||
183 | $this->assertNotNull($table); |
||
184 | $this->assertNull($table->getColumn('no_exist')); |
||
185 | } |
||
186 | |||
187 | public function testGetDefaultSchema(): void |
||
188 | { |
||
189 | $db = $this->getConnection(); |
||
190 | |||
191 | $schema = $db->getSchema(); |
||
192 | |||
193 | $this->assertNull($schema->getDefaultSchema()); |
||
194 | } |
||
195 | |||
196 | public function testGetNonExistingTableSchema(): void |
||
197 | { |
||
198 | $db = $this->getConnection(); |
||
199 | |||
200 | $schema = $db->getSchema(); |
||
201 | |||
202 | $this->assertNull($schema->getTableSchema('nonexisting_table')); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @throws Exception |
||
207 | * @throws InvalidCallException |
||
208 | * @throws InvalidConfigException |
||
209 | * @throws Throwable |
||
210 | */ |
||
211 | public function testGetPrimaryKey(): void |
||
212 | { |
||
213 | $db = $this->getConnection(true); |
||
214 | |||
215 | $command = $db->createCommand(); |
||
216 | |||
217 | $insertResult = $command->insertEx('animal', ['type' => 'cat']); |
||
218 | $selectResult = $command->setSql( |
||
219 | DbHelper::replaceQuotes( |
||
220 | <<<SQL |
||
221 | SELECT [[id]] FROM [[animal]] WHERE [[type]] = 'cat' |
||
222 | SQL, |
||
223 | $db->getName(), |
||
224 | ) |
||
225 | )->queryOne(); |
||
226 | |||
227 | $this->assertIsArray($insertResult); |
||
228 | $this->assertIsArray($selectResult); |
||
229 | $this->assertEquals($selectResult['id'], $insertResult['id']); |
||
230 | } |
||
231 | |||
232 | public function testGetSchemaChecks(): void |
||
233 | { |
||
234 | $db = $this->getConnection(); |
||
235 | |||
236 | $schema = $db->getSchema(); |
||
237 | $tableChecks = $schema->getSchemaChecks(); |
||
238 | |||
239 | $this->assertIsArray($tableChecks); |
||
240 | |||
241 | foreach ($tableChecks as $checks) { |
||
242 | $this->assertIsArray($checks); |
||
243 | $this->assertContainsOnlyInstancesOf(CheckConstraint::class, $checks); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | public function testGetSchemaDefaultValues(): void |
||
248 | { |
||
249 | $db = $this->getConnection(); |
||
250 | |||
251 | $schema = $db->getSchema(); |
||
252 | $tableDefaultValues = $schema->getSchemaDefaultValues(); |
||
253 | |||
254 | $this->assertIsArray($tableDefaultValues); |
||
255 | |||
256 | foreach ($tableDefaultValues as $defaultValues) { |
||
257 | $this->assertIsArray($defaultValues); |
||
258 | $this->assertContainsOnlyInstancesOf(DefaultValueConstraint::class, $defaultValues); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | public function testGetSchemaForeignKeys(): void |
||
263 | { |
||
264 | $db = $this->getConnection(); |
||
265 | |||
266 | $schema = $db->getSchema(); |
||
267 | $tableForeignKeys = $schema->getSchemaForeignKeys(); |
||
268 | |||
269 | $this->assertIsArray($tableForeignKeys); |
||
270 | |||
271 | foreach ($tableForeignKeys as $foreignKeys) { |
||
272 | $this->assertIsArray($foreignKeys); |
||
273 | $this->assertContainsOnlyInstancesOf(ForeignKeyConstraint::class, $foreignKeys); |
||
274 | } |
||
275 | } |
||
276 | |||
277 | public function testGetSchemaIndexes(): void |
||
278 | { |
||
279 | $db = $this->getConnection(); |
||
280 | |||
281 | $schema = $db->getSchema(); |
||
282 | $tableIndexes = $schema->getSchemaIndexes(); |
||
283 | |||
284 | $this->assertIsArray($tableIndexes); |
||
285 | |||
286 | foreach ($tableIndexes as $indexes) { |
||
287 | $this->assertIsArray($indexes); |
||
288 | $this->assertContainsOnlyInstancesOf(IndexConstraint::class, $indexes); |
||
289 | } |
||
290 | } |
||
291 | |||
292 | public function testGetSchemaPrimaryKeys(): void |
||
293 | { |
||
294 | $db = $this->getConnection(); |
||
295 | |||
296 | $schema = $db->getSchema(); |
||
297 | $tablePks = $schema->getSchemaPrimaryKeys(); |
||
298 | |||
299 | $this->assertIsArray($tablePks); |
||
300 | $this->assertContainsOnlyInstancesOf(Constraint::class, $tablePks); |
||
301 | } |
||
302 | |||
303 | public function testGetSchemaUniques(): void |
||
304 | { |
||
305 | $db = $this->getConnection(); |
||
306 | |||
307 | $schema = $db->getSchema(); |
||
308 | $tableUniques = $schema->getSchemaUniques(); |
||
309 | |||
310 | $this->assertIsArray($tableUniques); |
||
311 | |||
312 | foreach ($tableUniques as $uniques) { |
||
313 | $this->assertIsArray($uniques); |
||
314 | $this->assertContainsOnlyInstancesOf(Constraint::class, $uniques); |
||
315 | } |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::columnsTypeChar() |
||
320 | */ |
||
321 | public function testGetStringFieldsSize( |
||
322 | string $columnName, |
||
323 | string $columnType, |
||
324 | int|null $columnSize, |
||
325 | string $columnDbType |
||
326 | ): void { |
||
327 | $db = $this->getConnection(true); |
||
328 | |||
329 | $schema = $db->getSchema(); |
||
330 | $tableSchema = $schema->getTableSchema('type'); |
||
331 | |||
332 | $this->assertInstanceOf(TableSchemaInterface::class, $tableSchema); |
||
333 | |||
334 | $columns = $tableSchema->getColumns(); |
||
335 | |||
336 | foreach ($columns as $name => $column) { |
||
337 | $type = $column->getType(); |
||
338 | $size = $column->getSize(); |
||
339 | $dbType = $column->getDbType(); |
||
340 | |||
341 | if ($name === $columnName) { |
||
342 | $this->assertSame($columnType, $type); |
||
343 | $this->assertSame($columnSize, $size); |
||
344 | $this->assertSame($columnDbType, $dbType); |
||
345 | } |
||
346 | } |
||
347 | } |
||
348 | |||
349 | public function testGetTableChecks(): void |
||
350 | { |
||
351 | $db = $this->getConnection(true); |
||
352 | |||
353 | $schema = $db->getSchema(); |
||
354 | $tableChecks = $schema->getTableChecks('T_constraints_1'); |
||
355 | |||
356 | $this->assertIsArray($tableChecks); |
||
357 | |||
358 | $this->assertContainsOnlyInstancesOf(CheckConstraint::class, $tableChecks); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::pdoAttributes() |
||
363 | * |
||
364 | * @throws NotSupportedException |
||
365 | */ |
||
366 | public function testGetTableNames(array $pdoAttributes): void |
||
367 | { |
||
368 | $db = $this->getConnection(true); |
||
369 | |||
370 | foreach ($pdoAttributes as $name => $value) { |
||
371 | if ($name === PDO::ATTR_EMULATE_PREPARES) { |
||
372 | continue; |
||
373 | } |
||
374 | |||
375 | $db->getPDO()?->setAttribute($name, $value); |
||
376 | } |
||
377 | |||
378 | $schema = $db->getSchema(); |
||
379 | $tablesNames = $schema->getTableNames(); |
||
380 | |||
381 | $this->assertContains('customer', $tablesNames); |
||
382 | $this->assertContains('category', $tablesNames); |
||
383 | $this->assertContains('item', $tablesNames); |
||
384 | $this->assertContains('order', $tablesNames); |
||
385 | $this->assertContains('order_item', $tablesNames); |
||
386 | $this->assertContains('type', $tablesNames); |
||
387 | $this->assertContains('animal', $tablesNames); |
||
388 | $this->assertContains('animal_view', $tablesNames); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::tableSchema() |
||
393 | */ |
||
394 | public function testGetTableSchema(string $name, string $expectedName): void |
||
395 | { |
||
396 | $db = $this->getConnection(true); |
||
397 | |||
398 | $tableSchema = $db->getSchema()->getTableSchema($name); |
||
399 | |||
400 | $this->assertInstanceOf(TableSchemaInterface::class, $tableSchema); |
||
401 | $this->assertEquals($expectedName, $tableSchema->getName()); |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::pdoAttributes() |
||
406 | * |
||
407 | * @throws NotSupportedException |
||
408 | */ |
||
409 | public function testGetTableSchemas(array $pdoAttributes): void |
||
410 | { |
||
411 | $db = $this->getConnection(true); |
||
412 | |||
413 | foreach ($pdoAttributes as $name => $value) { |
||
414 | if ($name === PDO::ATTR_EMULATE_PREPARES) { |
||
415 | continue; |
||
416 | } |
||
417 | |||
418 | $db->getPDO()?->setAttribute($name, $value); |
||
419 | } |
||
420 | |||
421 | $schema = $db->getSchema(); |
||
422 | $tables = $schema->getTableSchemas(); |
||
423 | |||
424 | $this->assertCount(count($schema->getTableNames()), $tables); |
||
425 | |||
426 | foreach ($tables as $table) { |
||
427 | $this->assertInstanceOf(TableSchemaInterface::class, $table); |
||
428 | } |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @throws InvalidConfigException |
||
433 | * @throws NotSupportedException |
||
434 | * @throws Exception |
||
435 | */ |
||
436 | public function testGetTableSchemaWithAttrCase(): void |
||
437 | { |
||
438 | $db = $this->getConnection(true); |
||
439 | |||
440 | $schema = $db->getSchema(); |
||
441 | $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); |
||
442 | |||
443 | $this->assertCount(count($schema->getTableNames()), $schema->getTableSchemas()); |
||
444 | |||
445 | $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); |
||
446 | |||
447 | $this->assertCount(count($schema->getTableNames()), $schema->getTableSchemas()); |
||
448 | } |
||
449 | |||
450 | public function testGetViewNames(): void |
||
451 | { |
||
452 | $db = $this->getConnection(true); |
||
453 | |||
454 | $schema = $db->getSchema(); |
||
455 | $views = $schema->getViewNames(); |
||
456 | |||
457 | $this->assertSame(['animal_view'], $views); |
||
458 | } |
||
459 | |||
460 | public function testNegativeDefaultValues(): void |
||
461 | { |
||
462 | $schema = $this->getConnection(true); |
||
463 | |||
464 | $schema = $schema->getSchema(); |
||
465 | $table = $schema->getTableSchema('negative_default_values'); |
||
466 | |||
467 | $this->assertNotNull($table); |
||
468 | $this->assertSame(-123, $table->getColumn('tinyint_col')?->getDefaultValue()); |
||
469 | $this->assertSame(-123, $table->getColumn('smallint_col')?->getDefaultValue()); |
||
470 | $this->assertSame(-123, $table->getColumn('int_col')?->getDefaultValue()); |
||
471 | $this->assertSame(-123, $table->getColumn('bigint_col')?->getDefaultValue()); |
||
472 | $this->assertSame(-12345.6789, $table->getColumn('float_col')?->getDefaultValue()); |
||
473 | $this->assertEquals(-33.22, $table->getColumn('numeric_col')?->getDefaultValue()); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * @throws Exception |
||
478 | * @throws InvalidConfigException |
||
479 | * @throws Throwable |
||
480 | */ |
||
481 | public function testQuoterEscapingValue(): void |
||
482 | { |
||
483 | $db = $this->getConnection(true); |
||
484 | |||
485 | $quoter = $db->getQuoter(); |
||
486 | $db->createCommand( |
||
487 | <<<SQL |
||
488 | DELETE FROM [[quoter]] |
||
489 | SQL |
||
490 | )->execute(); |
||
491 | $data = $this->generateQuoterEscapingValues(); |
||
492 | |||
493 | foreach ($data as $index => $value) { |
||
494 | $quotedName = $quoter->quoteValue('testValue_' . $index); |
||
495 | $quoteValue = $quoter->quoteValue($value); |
||
496 | $db->createCommand( |
||
497 | <<<SQL |
||
498 | INSERT INTO [[quoter]] ([[name]], [[description]]) VALUES ($quotedName, $quoteValue) |
||
499 | SQL, |
||
500 | )->execute(); |
||
501 | $result = $db->createCommand( |
||
502 | <<<SQL |
||
503 | SELECT * FROM [[quoter]] WHERE [[name]]=$quotedName |
||
504 | SQL, |
||
505 | )->queryOne(); |
||
506 | |||
507 | $this->assertSame($value, $result['description']); |
||
508 | } |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * @depends testSchemaCache |
||
513 | */ |
||
514 | public function testRefreshTableSchema(): void |
||
515 | { |
||
516 | $db = $this->getConnection(true); |
||
517 | |||
518 | $schema = $db->getSchema(); |
||
519 | $schema->schemaCacheEnable(true); |
||
520 | $noCacheTable = $schema->getTableSchema('type', true); |
||
521 | $schema->refreshTableSchema('type'); |
||
522 | $refreshedTable = $schema->getTableSchema('type'); |
||
523 | |||
524 | $this->assertNotSame($noCacheTable, $refreshedTable); |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * @throws Exception |
||
529 | * @throws InvalidConfigException |
||
530 | */ |
||
531 | public function testSchemaCache(): void |
||
532 | { |
||
533 | $db = $this->getConnection(true); |
||
534 | |||
535 | $schema = $db->getSchema(); |
||
536 | $schema->schemaCacheEnable(true); |
||
537 | $noCacheTable = $schema->getTableSchema('type', true); |
||
538 | $cachedTable = $schema->getTableSchema('type'); |
||
539 | |||
540 | $this->assertSame($noCacheTable, $cachedTable); |
||
541 | |||
542 | $db->createCommand()->renameTable('type', 'type_test')->execute(); |
||
543 | $noCacheTable = $schema->getTableSchema('type', true); |
||
544 | |||
545 | $this->assertNotSame($noCacheTable, $cachedTable); |
||
546 | |||
547 | $db->createCommand()->renameTable('type_test', 'type')->execute(); |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::tableSchemaCachePrefixes() |
||
552 | */ |
||
553 | public function testTableSchemaCacheWithTablePrefixes( |
||
554 | string $tablePrefix, |
||
555 | string $tableName, |
||
556 | string $testTablePrefix, |
||
557 | string $testTableName |
||
558 | ): void { |
||
559 | $db = $this->getConnection(true); |
||
560 | |||
561 | $schema = $db->getSchema(); |
||
562 | $schema->schemaCacheEnable(true); |
||
563 | $db->setTablePrefix($tablePrefix); |
||
564 | $noCacheTable = $schema->getTableSchema($tableName, true); |
||
565 | |||
566 | $this->assertInstanceOf(TableSchemaInterface::class, $noCacheTable); |
||
567 | |||
568 | /* Compare */ |
||
569 | $db->setTablePrefix($testTablePrefix); |
||
570 | $testNoCacheTable = $schema->getTableSchema($testTableName); |
||
571 | |||
572 | $this->assertSame($noCacheTable, $testNoCacheTable); |
||
573 | |||
574 | $db->setTablePrefix($tablePrefix); |
||
575 | $schema->refreshTableSchema($tableName); |
||
576 | $refreshedTable = $schema->getTableSchema($tableName); |
||
577 | |||
578 | $this->assertInstanceOf(TableSchemaInterface::class, $refreshedTable); |
||
579 | $this->assertNotSame($noCacheTable, $refreshedTable); |
||
580 | |||
581 | /* Compare */ |
||
582 | $db->setTablePrefix($testTablePrefix); |
||
583 | $schema->refreshTableSchema($testTablePrefix); |
||
584 | $testRefreshedTable = $schema->getTableSchema($testTableName); |
||
585 | |||
586 | $this->assertInstanceOf(TableSchemaInterface::class, $testRefreshedTable); |
||
587 | $this->assertEquals($refreshedTable, $testRefreshedTable); |
||
588 | $this->assertNotSame($testNoCacheTable, $testRefreshedTable); |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::constraints() |
||
593 | * |
||
594 | * @throws Exception |
||
595 | * @throws JsonException |
||
596 | */ |
||
597 | public function testTableSchemaConstraints(string $tableName, string $type, mixed $expected): void |
||
598 | { |
||
599 | if ($expected === false) { |
||
600 | $this->expectException(NotSupportedException::class); |
||
601 | } |
||
602 | |||
603 | $db = $this->getConnection(true); |
||
604 | $schema = $db->getSchema(); |
||
605 | $constraints = $schema->{'getTable' . ucfirst($type)}($tableName); |
||
606 | |||
607 | $this->assertMetadataEquals($expected, $constraints); |
||
608 | |||
609 | $db->close(); |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::constraints() |
||
614 | * |
||
615 | * @throws Exception |
||
616 | * @throws JsonException |
||
617 | * @throws InvalidConfigException |
||
618 | */ |
||
619 | public function testTableSchemaConstraintsWithPdoLowercase(string $tableName, string $type, mixed $expected): void |
||
620 | { |
||
621 | if ($expected === false) { |
||
622 | $this->expectException(NotSupportedException::class); |
||
623 | } |
||
624 | |||
625 | $db = $this->getConnection(true); |
||
626 | |||
627 | $schema = $db->getSchema(); |
||
628 | $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); |
||
629 | $constraints = $schema->{'getTable' . ucfirst($type)}($tableName, true); |
||
630 | |||
631 | $this->assertMetadataEquals($expected, $constraints); |
||
632 | |||
633 | $db->close(); |
||
634 | } |
||
635 | |||
636 | /** |
||
637 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::constraints() |
||
638 | * |
||
639 | * @throws Exception |
||
640 | * @throws JsonException |
||
641 | * @throws InvalidConfigException |
||
642 | */ |
||
643 | public function testTableSchemaConstraintsWithPdoUppercase(string $tableName, string $type, mixed $expected): void |
||
644 | { |
||
645 | if ($expected === false) { |
||
646 | $this->expectException(NotSupportedException::class); |
||
647 | } |
||
648 | |||
649 | $db = $this->getConnection(true); |
||
650 | |||
651 | $schema = $db->getSchema(); |
||
652 | $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); |
||
653 | $constraints = $schema->{'getTable' . ucfirst($type)}($tableName, true); |
||
654 | |||
655 | $this->assertMetadataEquals($expected, $constraints); |
||
656 | |||
657 | $db->close(); |
||
658 | } |
||
659 | |||
660 | private function generateQuoterEscapingValues(): array |
||
661 | { |
||
662 | $result = []; |
||
663 | $stringLength = 16; |
||
664 | |||
665 | for ($i = 32; $i < 128 - $stringLength; $i += $stringLength) { |
||
666 | $str = ''; |
||
667 | |||
668 | for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) { |
||
669 | $str .= mb_chr($symbol, 'UTF-8'); |
||
670 | } |
||
671 | |||
672 | $result[] = $str; |
||
673 | $str = ''; |
||
674 | |||
675 | for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) { |
||
676 | $str .= mb_chr($symbol, 'UTF-8') . mb_chr($symbol, 'UTF-8'); |
||
677 | } |
||
678 | |||
679 | $result[] = $str; |
||
680 | } |
||
681 | |||
682 | return $result; |
||
683 | } |
||
684 | |||
685 | /** |
||
686 | * @throws JsonException |
||
687 | */ |
||
688 | protected function assertMetadataEquals($expected, $actual): void |
||
689 | { |
||
690 | switch (strtolower(gettype($expected))) { |
||
691 | case 'object': |
||
692 | $this->assertIsObject($actual); |
||
693 | break; |
||
694 | case 'array': |
||
695 | $this->assertIsArray($actual); |
||
696 | break; |
||
697 | case 'null': |
||
698 | $this->assertNull($actual); |
||
699 | break; |
||
700 | } |
||
701 | |||
702 | if (is_array($expected)) { |
||
703 | $this->normalizeArrayKeys($expected, false); |
||
704 | $this->normalizeArrayKeys($actual, false); |
||
705 | } |
||
706 | |||
707 | $this->normalizeConstraints($expected, $actual); |
||
708 | |||
709 | if (is_array($expected)) { |
||
710 | $this->normalizeArrayKeys($expected, true); |
||
711 | $this->normalizeArrayKeys($actual, true); |
||
712 | } |
||
713 | |||
714 | $this->assertEquals($expected, $actual); |
||
715 | } |
||
716 | |||
717 | protected function columnSchema(array $columns, string $table): void |
||
718 | { |
||
719 | $db = $this->getConnection(true); |
||
720 | |||
721 | $table = $db->getTableSchema($table, true); |
||
722 | |||
723 | $this->assertNotNull($table); |
||
724 | |||
725 | $expectedColNames = array_keys($columns); |
||
726 | sort($expectedColNames); |
||
727 | $colNames = $table->getColumnNames(); |
||
728 | sort($colNames); |
||
729 | |||
730 | $this->assertSame($expectedColNames, $colNames); |
||
731 | |||
732 | foreach ($table->getColumns() as $name => $column) { |
||
733 | $expected = $columns[$name]; |
||
734 | |||
735 | $this->assertSame( |
||
736 | $expected['dbType'], |
||
737 | $column->getDbType(), |
||
738 | "dbType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}." |
||
739 | ); |
||
740 | $this->assertSame( |
||
741 | $expected['phpType'], |
||
742 | $column->getPhpType(), |
||
743 | "phpType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}." |
||
744 | ); |
||
745 | $this->assertSame( |
||
746 | $expected['primaryKey'], |
||
747 | $column->isPrimaryKey(), |
||
748 | "primaryKey of column $name does not match." |
||
749 | ); |
||
750 | $this->assertSame($expected['type'], $column->getType(), "type of column $name does not match."); |
||
751 | $this->assertSame( |
||
752 | $expected['allowNull'], |
||
753 | $column->isAllowNull(), |
||
754 | "allowNull of column $name does not match." |
||
755 | ); |
||
756 | $this->assertSame( |
||
757 | $expected['autoIncrement'], |
||
758 | $column->isAutoIncrement(), |
||
759 | "autoIncrement of column $name does not match." |
||
760 | ); |
||
761 | $this->assertSame( |
||
762 | $expected['enumValues'], |
||
763 | $column->getEnumValues(), |
||
764 | "enumValues of column $name does not match." |
||
765 | ); |
||
766 | $this->assertSame($expected['size'], $column->getSize(), "size of column $name does not match."); |
||
767 | $this->assertSame( |
||
768 | $expected['precision'], |
||
769 | $column->getPrecision(), |
||
770 | "precision of column $name does not match." |
||
771 | ); |
||
772 | |||
773 | $this->assertSame($expected['scale'], $column->getScale(), "scale of column $name does not match."); |
||
774 | |||
775 | if (is_object($expected['defaultValue'])) { |
||
776 | $this->assertIsObject( |
||
777 | $column->getDefaultValue(), |
||
778 | "defaultValue of column $name is expected to be an object but it is not." |
||
779 | ); |
||
780 | $this->assertSame( |
||
781 | (string) $expected['defaultValue'], |
||
782 | (string) $column->getDefaultValue(), |
||
783 | "defaultValue of column $name does not match." |
||
784 | ); |
||
785 | } else { |
||
786 | $this->assertSame( |
||
787 | $expected['defaultValue'], |
||
788 | $column->getDefaultValue(), |
||
789 | "defaultValue of column $name does not match." |
||
790 | ); |
||
791 | } |
||
792 | |||
793 | /* Pgsql only */ |
||
794 | if (isset($expected['dimension'])) { |
||
795 | /** @psalm-suppress UndefinedMethod */ |
||
796 | $this->assertSame( |
||
797 | $expected['dimension'], |
||
798 | $column->getDimension(), |
||
799 | "dimension of column $name does not match" |
||
800 | ); |
||
801 | } |
||
802 | } |
||
803 | |||
804 | $db->close(); |
||
805 | } |
||
806 | |||
807 | /** |
||
808 | * @throws JsonException |
||
809 | */ |
||
810 | private function normalizeArrayKeys(array &$array, bool $caseSensitive): void |
||
811 | { |
||
812 | $newArray = []; |
||
813 | |||
814 | foreach ($array as $value) { |
||
815 | if ($value instanceof Constraint) { |
||
816 | $key = (array) $value; |
||
817 | unset( |
||
818 | $key["\000Yiisoft\Db\Constraint\Constraint\000name"], |
||
819 | $key["\u0000Yiisoft\\Db\\Constraint\\ForeignKeyConstraint\u0000foreignSchemaName"] |
||
820 | ); |
||
821 | |||
822 | foreach ($key as $keyName => $keyValue) { |
||
823 | if ($keyValue instanceof AnyCaseValue) { |
||
824 | $key[$keyName] = $keyValue->value; |
||
825 | } elseif ($keyValue instanceof AnyValue) { |
||
826 | $key[$keyName] = '[AnyValue]'; |
||
827 | } |
||
828 | } |
||
829 | |||
830 | ksort($key, SORT_STRING); |
||
831 | $newArray[$caseSensitive |
||
832 | ? json_encode($key, JSON_THROW_ON_ERROR) |
||
833 | : strtolower(json_encode($key, JSON_THROW_ON_ERROR))] = $value; |
||
834 | } else { |
||
835 | $newArray[] = $value; |
||
836 | } |
||
837 | } |
||
838 | |||
839 | ksort($newArray, SORT_STRING); |
||
840 | $array = $newArray; |
||
841 | } |
||
842 | |||
843 | private function normalizeConstraints($expected, $actual): void |
||
844 | { |
||
845 | if (is_array($expected)) { |
||
846 | foreach ($expected as $key => $value) { |
||
847 | if (!$value instanceof Constraint || !isset($actual[$key]) || !$actual[$key] instanceof Constraint) { |
||
848 | continue; |
||
849 | } |
||
850 | |||
851 | $this->normalizeConstraintPair($value, $actual[$key]); |
||
852 | } |
||
853 | } elseif ($expected instanceof Constraint && $actual instanceof Constraint) { |
||
854 | $this->normalizeConstraintPair($expected, $actual); |
||
855 | } |
||
856 | } |
||
857 | |||
858 | private function normalizeConstraintPair(Constraint $expectedConstraint, Constraint $actualConstraint): void |
||
859 | { |
||
860 | if ($expectedConstraint::class !== $actualConstraint::class) { |
||
861 | return; |
||
862 | } |
||
863 | |||
864 | foreach (array_keys((array) $expectedConstraint) as $name) { |
||
865 | if ($expectedConstraint->getName() instanceof AnyValue) { |
||
866 | $actualConstraint->name($expectedConstraint->getName()); |
||
867 | } elseif ($expectedConstraint->getName() instanceof AnyCaseValue) { |
||
868 | $actualConstraintName = $actualConstraint->getName(); |
||
869 | |||
870 | $this->assertIsString($actualConstraintName); |
||
871 | |||
872 | $actualConstraint->name(new AnyCaseValue($actualConstraintName)); |
||
873 | } |
||
874 | } |
||
875 | } |
||
876 | |||
877 | public function testWorkWithUniqueConstraint(): void |
||
878 | { |
||
879 | $tableName = 'test_table_with'; |
||
880 | $constraintName = 't_constraint'; |
||
881 | $columnName = 't_field'; |
||
882 | |||
883 | $db = $this->getConnection(); |
||
884 | |||
885 | $this->createTableForIndexAndConstraintTests($db, $tableName, $columnName); |
||
886 | $db->createCommand()->addUnique($constraintName, $tableName, $columnName)->execute(); |
||
887 | |||
888 | /** @var Constraint[] $constraints */ |
||
889 | $constraints = $db->getSchema()->getTableUniques($tableName, true); |
||
890 | |||
891 | $this->assertIsArray($constraints); |
||
892 | $this->assertCount(1, $constraints); |
||
893 | $this->assertInstanceOf(Constraint::class, $constraints[0]); |
||
894 | $this->assertEquals($constraintName, $constraints[0]->getName()); |
||
895 | $this->assertEquals([$columnName], $constraints[0]->getColumnNames()); |
||
896 | |||
897 | $db->createCommand()->dropUnique($constraintName, $tableName)->execute(); |
||
898 | |||
899 | $constraints = $db->getSchema()->getTableUniques($tableName, true); |
||
900 | |||
901 | $this->assertIsArray($constraints); |
||
902 | $this->assertCount(0, $constraints); |
||
903 | |||
904 | $this->dropTableForIndexAndConstraintTests($db, $tableName); |
||
905 | } |
||
906 | |||
907 | public function testWorkWithCheckConstraint(): void |
||
908 | { |
||
909 | $tableName = 'test_table_with'; |
||
910 | $constraintName = 't_constraint'; |
||
911 | $columnName = 't_field'; |
||
912 | |||
913 | $db = $this->getConnection(); |
||
914 | |||
915 | $this->createTableForIndexAndConstraintTests($db, $tableName, $columnName, 'int'); |
||
916 | $db->createCommand()->addCheck($constraintName, $tableName, $db->getQuoter()->quoteColumnName($columnName) . ' > 0')->execute(); |
||
917 | |||
918 | /** @var CheckConstraint[] $constraints */ |
||
919 | $constraints = $db->getSchema()->getTableChecks($tableName, true); |
||
920 | |||
921 | $this->assertIsArray($constraints); |
||
922 | $this->assertCount(1, $constraints); |
||
923 | $this->assertInstanceOf(CheckConstraint::class, $constraints[0]); |
||
924 | $this->assertEquals($constraintName, $constraints[0]->getName()); |
||
925 | $this->assertEquals([$columnName], $constraints[0]->getColumnNames()); |
||
926 | $this->assertStringContainsString($columnName, $constraints[0]->getExpression()); |
||
927 | |||
928 | $db->createCommand()->dropCheck($constraintName, $tableName)->execute(); |
||
929 | |||
930 | $constraints = $db->getSchema()->getTableChecks($tableName, true); |
||
931 | |||
932 | $this->assertIsArray($constraints); |
||
933 | $this->assertCount(0, $constraints); |
||
934 | |||
935 | $this->dropTableForIndexAndConstraintTests($db, $tableName); |
||
936 | } |
||
937 | |||
938 | public function testWorkWithDefaultValueConstraint(): void |
||
939 | { |
||
940 | $tableName = 'test_table_with'; |
||
941 | $constraintName = 't_constraint'; |
||
942 | $columnName = 't_field'; |
||
943 | |||
944 | $db = $this->getConnection(); |
||
945 | |||
946 | $this->createTableForIndexAndConstraintTests($db, $tableName, $columnName); |
||
947 | $db->createCommand()->addDefaultValue($constraintName, $tableName, $columnName, 919)->execute(); |
||
948 | |||
949 | /** @var DefaultValueConstraint[] $constraints */ |
||
950 | $constraints = $db->getSchema()->getTableDefaultValues($tableName, true); |
||
951 | |||
952 | $this->assertIsArray($constraints); |
||
953 | $this->assertCount(1, $constraints); |
||
954 | $this->assertInstanceOf(DefaultValueConstraint::class, $constraints[0]); |
||
955 | $this->assertEquals($constraintName, $constraints[0]->getName()); |
||
956 | $this->assertEquals([$columnName], $constraints[0]->getColumnNames()); |
||
957 | $this->assertStringContainsString('919', $constraints[0]->getValue()); |
||
958 | |||
959 | $db->createCommand()->dropDefaultValue($constraintName, $tableName)->execute(); |
||
960 | |||
961 | $constraints = $db->getSchema()->getTableDefaultValues($tableName, true); |
||
962 | |||
963 | $this->assertIsArray($constraints); |
||
964 | $this->assertCount(0, $constraints); |
||
965 | |||
966 | $this->dropTableForIndexAndConstraintTests($db, $tableName); |
||
967 | } |
||
968 | |||
969 | public function testWorkWithPrimaryKeyConstraint(): void |
||
970 | { |
||
971 | $tableName = 'test_table_with'; |
||
972 | $constraintName = 't_constraint'; |
||
973 | $columnName = 't_field'; |
||
974 | |||
975 | $db = $this->getConnection(); |
||
976 | |||
977 | $this->createTableForIndexAndConstraintTests($db, $tableName, $columnName); |
||
978 | $db->createCommand()->addPrimaryKey($constraintName, $tableName, $columnName)->execute(); |
||
979 | |||
980 | $constraints = $db->getSchema()->getTablePrimaryKey($tableName, true); |
||
981 | |||
982 | $this->assertInstanceOf(Constraint::class, $constraints); |
||
983 | $this->assertEquals($constraintName, $constraints->getName()); |
||
984 | $this->assertEquals([$columnName], $constraints->getColumnNames()); |
||
985 | |||
986 | $db->createCommand()->dropPrimaryKey($constraintName, $tableName)->execute(); |
||
987 | |||
988 | $constraints = $db->getSchema()->getTablePrimaryKey($tableName, true); |
||
989 | |||
990 | $this->assertNull($constraints); |
||
991 | |||
992 | $this->dropTableForIndexAndConstraintTests($db, $tableName); |
||
993 | } |
||
994 | |||
995 | /** |
||
996 | * @dataProvider withIndexDataProvider |
||
997 | */ |
||
998 | public function testWorkWithIndex( |
||
999 | ?string $indexType = null, |
||
1000 | ?string $indexMethod = null, |
||
1001 | ?string $columnType = null, |
||
1002 | bool $isPrimary = false, |
||
1003 | bool $isUnique = false |
||
1004 | ): void { |
||
1005 | $tableName = 'test_table_with'; |
||
1006 | $indexName = 't_index'; |
||
1007 | $columnName = 't_field'; |
||
1008 | |||
1009 | $db = $this->getConnection(); |
||
1010 | $qb = $db->getQueryBuilder(); |
||
1011 | |||
1012 | $this->createTableForIndexAndConstraintTests($db, $tableName, $columnName, $columnType); |
||
1013 | |||
1014 | $indexSql = $qb->createIndex($indexName, $tableName, $columnName, $indexType, $indexMethod); |
||
1015 | $db->createCommand($indexSql)->execute(); |
||
1016 | |||
1017 | /** @var IndexConstraint[] $indexes */ |
||
1018 | $indexes = $db->getSchema()->getTableIndexes($tableName, true); |
||
1019 | $this->assertIsArray($indexes); |
||
1020 | $this->assertCount(1, $indexes); |
||
1021 | $this->assertInstanceOf(IndexConstraint::class, $indexes[0]); |
||
1022 | $this->assertEquals($indexName, $indexes[0]->getName()); |
||
1023 | $this->assertEquals([$columnName], $indexes[0]->getColumnNames()); |
||
1024 | $this->assertSame($isUnique, $indexes[0]->isUnique()); |
||
1025 | $this->assertSame($isPrimary, $indexes[0]->isPrimary()); |
||
1026 | |||
1027 | $this->dropTableForIndexAndConstraintTests($db, $tableName); |
||
1028 | } |
||
1029 | |||
1030 | public function withIndexDataProvider(): array |
||
1031 | { |
||
1032 | return [ |
||
1033 | [ |
||
1034 | 'indexType' => QueryBuilder::INDEX_UNIQUE, |
||
1035 | 'indexMethod' => null, |
||
1036 | 'columnType' => null, |
||
1037 | 'isPrimary' => false, |
||
1038 | 'isUnique' => true, |
||
1039 | ], |
||
1040 | ]; |
||
1041 | } |
||
1042 | |||
1043 | protected function createTableForIndexAndConstraintTests(ConnectionInterface $db, string $tableName, string $columnName, ?string $columnType = null): void |
||
1044 | { |
||
1045 | $qb = $db->getQueryBuilder(); |
||
1046 | |||
1047 | if ($db->getTableSchema($tableName, true) !== null) { |
||
1048 | $db->createCommand($qb->dropTable($tableName))->execute(); |
||
1049 | } |
||
1050 | |||
1051 | $createTableSql = $qb->createTable( |
||
1052 | $tableName, |
||
1053 | [ |
||
1054 | $columnName => $columnType ?? 'int NOT NULL', |
||
1055 | ], |
||
1056 | ); |
||
1057 | |||
1058 | $db->createCommand($createTableSql)->execute(); |
||
1059 | $tableSchema = $db->getTableSchema($tableName, true); |
||
1060 | $this->assertInstanceOf(TableSchemaInterface::class, $tableSchema); |
||
1061 | } |
||
1062 | |||
1063 | protected function dropTableForIndexAndConstraintTests(ConnectionInterface $db, $tableName): void |
||
1064 | { |
||
1065 | $qb = $db->getQueryBuilder(); |
||
1066 | |||
1067 | $db->createCommand($qb->dropTable($tableName))->execute(); |
||
1068 | $this->assertNull($db->getTableSchema($tableName, true)); |
||
1069 | } |
||
1071 |