Passed
Push — master ( 914087...c6011d )
by Alexander
11:22
created

TestSchemaTrait::testGetSchemaChecks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestSupport;
6
7
use PDO;
8
use Yiisoft\Db\Constraint\CheckConstraint;
9
use Yiisoft\Db\Constraint\Constraint;
10
use Yiisoft\Db\Constraint\DefaultValueConstraint;
11
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
12
use Yiisoft\Db\Constraint\IndexConstraint;
13
use Yiisoft\Db\Schema\ColumnSchema;
14
use Yiisoft\Db\Schema\Schema;
15
16
use Yiisoft\Db\Schema\TableSchemaInterface;
17
use function array_keys;
18
use function fclose;
19
use function fopen;
20
use function gettype;
21
use function is_array;
22
use function json_encode;
23
use function ksort;
24
use function print_r;
25
use function sort;
26
use function sprintf;
27
use function strtolower;
28
29
trait TestSchemaTrait
30
{
31
    public function testGetTableSchemasWithAttrCase(): void
32
    {
33
        $db = $this->getConnection(false);
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        /** @scrutinizer ignore-call */ 
34
        $db = $this->getConnection(false);
Loading history...
34
35
        $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
36
37
        $this->assertCount(count($db->getSchema()->getTableNames()), $db->getSchema()->getTableSchemas());
0 ignored issues
show
Bug introduced by
It seems like assertCount() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        $this->/** @scrutinizer ignore-call */ 
38
               assertCount(count($db->getSchema()->getTableNames()), $db->getSchema()->getTableSchemas());
Loading history...
38
39
        $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
40
41
        $this->assertCount(count($db->getSchema()->getTableNames()), $db->getSchema()->getTableSchemas());
42
    }
43
44
    public function testGetSchemaPrimaryKeys(): void
45
    {
46
        $db = $this->getConnection(false);
47
48
        $tablePks = $db->getSchema()->getSchemaPrimaryKeys();
49
50
        /** @psalm-suppress RedundantCondition */
51
        $this->assertIsArray($tablePks);
0 ignored issues
show
Bug introduced by
It seems like assertIsArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $this->/** @scrutinizer ignore-call */ 
52
               assertIsArray($tablePks);
Loading history...
52
        $this->assertContainsOnlyInstancesOf(Constraint::class, $tablePks);
0 ignored issues
show
Bug introduced by
It seems like assertContainsOnlyInstancesOf() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        $this->/** @scrutinizer ignore-call */ 
53
               assertContainsOnlyInstancesOf(Constraint::class, $tablePks);
Loading history...
53
    }
54
55
    public function testGetSchemaChecks(): void
56
    {
57
        $db = $this->getConnection(false);
58
59
        $tableChecks = $db->getSchema()->getSchemaChecks();
60
61
        /** @psalm-suppress RedundantCondition */
62
        $this->assertIsArray($tableChecks);
63
64
        foreach ($tableChecks as $checks) {
65
            $this->assertIsArray($checks);
66
            $this->assertContainsOnlyInstancesOf(CheckConstraint::class, $checks);
67
        }
68
    }
69
70
    public function testGetSchemaDefaultValues(): void
71
    {
72
        $db = $this->getConnection(false);
73
74
        $tableDefaultValues = $db->getSchema()->getSchemaDefaultValues();
75
76
        /** @psalm-suppress RedundantCondition */
77
        $this->assertIsArray($tableDefaultValues);
78
79
        foreach ($tableDefaultValues as $defaultValues) {
80
            $this->assertIsArray($defaultValues);
81
            $this->assertContainsOnlyInstancesOf(DefaultValueConstraint::class, $defaultValues);
82
        }
83
    }
84
85
    public function testGetSchemaForeignKeys(): void
86
    {
87
        $db = $this->getConnection(false);
88
89
        $tableForeignKeys = $db->getSchema()->getSchemaForeignKeys();
90
91
        /** @psalm-suppress RedundantCondition */
92
        $this->assertIsArray($tableForeignKeys);
93
94
        foreach ($tableForeignKeys as $foreignKeys) {
95
            $this->assertIsArray($foreignKeys);
96
            $this->assertContainsOnlyInstancesOf(ForeignKeyConstraint::class, $foreignKeys);
97
        }
98
    }
99
100
    public function testGetSchemaIndexes(): void
101
    {
102
        $db = $this->getConnection(false);
103
104
        $tableIndexes = $db->getSchema()->getSchemaIndexes();
105
106
        /** @psalm-suppress RedundantCondition */
107
        $this->assertIsArray($tableIndexes);
108
109
        foreach ($tableIndexes as $indexes) {
110
            $this->assertIsArray($indexes);
111
            $this->assertContainsOnlyInstancesOf(IndexConstraint::class, $indexes);
112
        }
113
    }
114
115
    public function testGetSchemaUniques(): void
116
    {
117
        $db = $this->getConnection(false);
118
119
        $tableUniques = $db->getSchema()->getSchemaUniques();
120
121
        /** @psalm-suppress RedundantCondition */
122
        $this->assertIsArray($tableUniques);
123
124
        foreach ($tableUniques as $uniques) {
125
            $this->assertIsArray($uniques);
126
            $this->assertContainsOnlyInstancesOf(Constraint::class, $uniques);
127
        }
128
    }
129
130
    public function testGetNonExistingTableSchema(): void
131
    {
132
        $this->assertNull($this->getConnection()->getSchema()->getTableSchema('nonexisting_table'));
0 ignored issues
show
Bug introduced by
It seems like assertNull() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        $this->/** @scrutinizer ignore-call */ 
133
               assertNull($this->getConnection()->getSchema()->getTableSchema('nonexisting_table'));
Loading history...
133
    }
134
135
    public function testSchemaCache(): void
136
    {
137
        $db = $this->getConnection();
138
139
        $schema = $db->getSchema();
140
141
        $this->assertNotNull($this->schemaCache);
0 ignored issues
show
Bug introduced by
It seems like assertNotNull() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        $this->/** @scrutinizer ignore-call */ 
142
               assertNotNull($this->schemaCache);
Loading history...
142
        $this->schemaCache->setEnable(true);
143
144
        $noCacheTable = $schema->getTableSchema('type', true);
145
        $cachedTable = $schema->getTableSchema('type', false);
146
147
        $this->assertEquals($noCacheTable, $cachedTable);
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        $this->/** @scrutinizer ignore-call */ 
148
               assertEquals($noCacheTable, $cachedTable);
Loading history...
148
149
        $db->createCommand()->renameTable('type', 'type_test');
150
151
        $noCacheTable = $schema->getTableSchema('type', true);
152
153
        $this->assertNotSame($noCacheTable, $cachedTable);
0 ignored issues
show
Bug introduced by
It seems like assertNotSame() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        $this->/** @scrutinizer ignore-call */ 
154
               assertNotSame($noCacheTable, $cachedTable);
Loading history...
154
155
        $db->createCommand()->renameTable('type_test', 'type');
156
    }
157
158
    /**
159
     * @depends testSchemaCache
160
     */
161
    public function testRefreshTableSchema(): void
162
    {
163
        $schema = $this->getConnection()->getSchema();
164
165
        $this->assertNotNull($this->schemaCache);
166
167
        $this->schemaCache->setEnable(true);
168
169
        $noCacheTable = $schema->getTableSchema('type', true);
170
171
        $schema->refreshTableSchema('type');
172
173
        $refreshedTable = $schema->getTableSchema('type', false);
174
175
        $this->assertNotSame($noCacheTable, $refreshedTable);
176
    }
177
178
    public function testCompositeFk(): void
179
    {
180
        $schema = $this->getConnection()->getSchema();
181
182
        $table = $schema->getTableSchema('composite_fk');
183
184
        $this->assertNotNull($table);
185
186
        $fk = $table->getForeignKeys();
187
188
        $this->assertCount(1, $fk);
189
        $this->assertTrue(isset($fk['FK_composite_fk_order_item']));
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

189
        $this->/** @scrutinizer ignore-call */ 
190
               assertTrue(isset($fk['FK_composite_fk_order_item']));
Loading history...
190
        $this->assertEquals('order_item', $fk['FK_composite_fk_order_item'][0]);
191
        $this->assertEquals('order_id', $fk['FK_composite_fk_order_item']['order_id']);
192
        $this->assertEquals('item_id', $fk['FK_composite_fk_order_item']['item_id']);
193
    }
194
195
    public function testGetPDOType(): void
196
    {
197
        $values = [
198
            [null, PDO::PARAM_NULL],
199
            ['', PDO::PARAM_STR],
200
            ['hello', PDO::PARAM_STR],
201
            [0, PDO::PARAM_INT],
202
            [1, PDO::PARAM_INT],
203
            [1337, PDO::PARAM_INT],
204
            [true, PDO::PARAM_BOOL],
205
            [false, PDO::PARAM_BOOL],
206
            [$fp = fopen(__FILE__, 'rb'), PDO::PARAM_LOB],
207
        ];
208
209
        $schema = $this->getConnection()->getSchema();
210
211
        foreach ($values as $value) {
212
            $this->assertEquals($value[1], $schema->getPdoType($value[0]), 'type for value ' . print_r($value[0], true) . ' does not match.');
0 ignored issues
show
Bug introduced by
Are you sure print_r($value[0], true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

212
            $this->assertEquals($value[1], $schema->getPdoType($value[0]), 'type for value ' . /** @scrutinizer ignore-type */ print_r($value[0], true) . ' does not match.');
Loading history...
213
        }
214
215
        fclose($fp);
216
    }
217
218
    public function testColumnSchema(): void
219
    {
220
        $columns = $this->getExpectedColumns();
0 ignored issues
show
Bug introduced by
It seems like getExpectedColumns() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
        /** @scrutinizer ignore-call */ 
221
        $columns = $this->getExpectedColumns();
Loading history...
221
222
        $table = $this->getConnection(false)->getSchema()->getTableSchema('type', true);
223
        $this->assertNotNull($table);
224
225
        $expectedColNames = array_keys($columns);
226
227
        sort($expectedColNames);
228
229
        $colNames = $table->getColumnNames();
230
231
        sort($colNames);
232
233
        $this->assertEquals($expectedColNames, $colNames);
234
235
        foreach ($table->getColumns() as $name => $column) {
236
            $expected = $columns[$name];
237
            $this->assertSame(
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

237
            $this->/** @scrutinizer ignore-call */ 
238
                   assertSame(
Loading history...
238
                $expected['dbType'],
239
                $column->getDbType(),
240
                "dbType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}."
241
            );
242
            $this->assertSame(
243
                $expected['phpType'],
244
                $column->getPhpType(),
245
                "phpType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}."
246
            );
247
            $this->assertSame($expected['type'], $column->getType(), "type of column $name does not match.");
248
            $this->assertSame(
249
                $expected['allowNull'],
250
                $column->isAllowNull(),
251
                "allowNull of column $name does not match."
252
            );
253
            $this->assertSame(
254
                $expected['autoIncrement'],
255
                $column->isAutoIncrement(),
256
                "autoIncrement of column $name does not match."
257
            );
258
            $this->assertSame(
259
                $expected['enumValues'],
260
                $column->getEnumValues(),
261
                "enumValues of column $name does not match."
262
            );
263
            $this->assertSame($expected['size'], $column->getSize(), "size of column $name does not match.");
264
            $this->assertSame(
265
                $expected['precision'],
266
                $column->getPrecision(),
267
                "precision of column $name does not match."
268
            );
269
            $this->assertSame($expected['scale'], $column->getScale(), "scale of column $name does not match.");
270
            if (\is_object($expected['defaultValue'])) {
271
                $this->assertIsObject(
0 ignored issues
show
Bug introduced by
It seems like assertIsObject() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
                $this->/** @scrutinizer ignore-call */ 
272
                       assertIsObject(
Loading history...
272
                    $column->getDefaultValue(),
273
                    "defaultValue of column $name is expected to be an object but it is not."
274
                );
275
                $this->assertEquals(
276
                    (string) $expected['defaultValue'],
277
                    (string) $column->getDefaultValue(),
278
                    "defaultValue of column $name does not match."
279
                );
280
            } else {
281
                $this->assertEquals(
282
                    $expected['defaultValue'],
283
                    $column->getDefaultValue(),
284
                    "defaultValue of column $name does not match."
285
                );
286
            }
287
            /* Pgsql only */
288
            if (isset($expected['dimension'])) {
289
                /** @psalm-suppress UndefinedMethod */
290
                $this->assertSame(
291
                    $expected['dimension'],
292
                    $column->getDimension(),
293
                    "dimension of column $name does not match"
294
                );
295
            }
296
        }
297
    }
298
299
    public function testColumnSchemaDbTypecastWithEmptyCharType(): void
300
    {
301
        $columnSchema = new ColumnSchema();
302
303
        $columnSchema->setType(Schema::TYPE_CHAR);
304
305
        $this->assertSame('', $columnSchema->dbTypecast(''));
306
    }
307
308
    public function testNegativeDefaultValues(): void
309
    {
310
        $schema = $this->getConnection()->getSchema();
311
312
        $table = $schema->getTableSchema('negative_default_values');
313
314
        $this->assertNotNull($table);
315
        $this->assertEquals(-123, $table->getColumn('tinyint_col')?->getDefaultValue());
316
        $this->assertEquals(-123, $table->getColumn('smallint_col')?->getDefaultValue());
317
        $this->assertEquals(-123, $table->getColumn('int_col')?->getDefaultValue());
318
        $this->assertEquals(-123, $table->getColumn('bigint_col')?->getDefaultValue());
319
        $this->assertEquals(-12345.6789, $table->getColumn('float_col')?->getDefaultValue());
320
        $this->assertEquals(-33.22, $table->getColumn('numeric_col')?->getDefaultValue());
321
    }
322
323
    public function testContraintTablesExistance(): void
324
    {
325
        $tableNames = [
326
            'T_constraints_1',
327
            'T_constraints_2',
328
            'T_constraints_3',
329
            'T_constraints_4',
330
        ];
331
332
        $schema = $this->getConnection()->getSchema();
333
334
        foreach ($tableNames as $tableName) {
335
            $tableSchema = $schema->getTableSchema($tableName);
336
            $this->assertInstanceOf(TableSchemaInterface::class, $tableSchema, $tableName);
0 ignored issues
show
Bug introduced by
It seems like assertInstanceOf() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

336
            $this->/** @scrutinizer ignore-call */ 
337
                   assertInstanceOf(TableSchemaInterface::class, $tableSchema, $tableName);
Loading history...
337
        }
338
    }
339
340
    public function testGetColumnNoExist(): void
341
    {
342
        $schema = $this->getConnection()->getSchema();
343
        $table = $schema->getTableSchema('negative_default_values');
344
345
        $this->assertNotNull($table);
346
        $this->assertNull($table->getColumn('no_exist'));
347
    }
348
349
    private function assertMetadataEquals($expected, $actual): void
350
    {
351
        switch (strtolower(gettype($expected))) {
352
            case 'object':
353
                $this->assertIsObject($actual);
354
                break;
355
            case 'array':
356
                $this->assertIsArray($actual);
357
                break;
358
            case 'null':
359
                $this->assertNull($actual);
360
                break;
361
        }
362
363
        if (is_array($expected)) {
364
            $this->normalizeArrayKeys($expected, false);
365
            /** @psalm-suppress PossiblyInvalidArgument */
366
            $this->normalizeArrayKeys($actual, false);
367
        }
368
369
        $this->normalizeConstraints($expected, $actual);
370
371
        if (is_array($expected)) {
372
            $this->normalizeArrayKeys($expected, true);
373
            $this->normalizeArrayKeys($actual, true);
374
        }
375
376
        $this->assertEquals($expected, $actual);
377
    }
378
379
    private function normalizeArrayKeys(array &$array, bool $caseSensitive): void
380
    {
381
        $newArray = [];
382
383
        foreach ($array as $value) {
384
            if ($value instanceof Constraint) {
385
                $key = (array) $value;
386
                unset(
387
                    $key["\000Yiisoft\Db\Constraint\Constraint\000name"],
388
                    $key["\u0000Yiisoft\\Db\\Constraint\\ForeignKeyConstraint\u0000foreignSchemaName"]
389
                );
390
391
                foreach ($key as $keyName => $keyValue) {
392
                    if ($keyValue instanceof AnyCaseValue) {
393
                        $key[$keyName] = $keyValue->value;
394
                    } elseif ($keyValue instanceof AnyValue) {
395
                        $key[$keyName] = '[AnyValue]';
396
                    }
397
                }
398
399
                ksort($key, SORT_STRING);
400
401
                $newArray[$caseSensitive
402
                    ? json_encode($key, JSON_THROW_ON_ERROR)
403
                    : strtolower(json_encode($key, JSON_THROW_ON_ERROR))] = $value;
404
            } else {
405
                $newArray[] = $value;
406
            }
407
        }
408
409
        ksort($newArray, SORT_STRING);
410
411
        $array = $newArray;
412
    }
413
414
    private function normalizeConstraints(&$expected, &$actual): void
415
    {
416
        if (is_array($expected)) {
417
            foreach ($expected as $key => $value) {
418
                if (!$value instanceof Constraint || !isset($actual[$key]) || !$actual[$key] instanceof Constraint) {
419
                    continue;
420
                }
421
422
                $this->normalizeConstraintPair($value, $actual[$key]);
423
            }
424
        } elseif ($expected instanceof Constraint && $actual instanceof Constraint) {
425
            $this->normalizeConstraintPair($expected, $actual);
426
        }
427
    }
428
429
    private function normalizeConstraintPair(Constraint $expectedConstraint, Constraint $actualConstraint): void
430
    {
431
        if (get_class($expectedConstraint) !== get_class($actualConstraint)) {
432
            return;
433
        }
434
435
        foreach (array_keys((array) $expectedConstraint) as $name) {
436
            if ($expectedConstraint->getName() instanceof AnyValue) {
437
                $actualConstraint->name($expectedConstraint->getName());
438
            } elseif ($expectedConstraint->getName() instanceof AnyCaseValue) {
439
                $actualConstraintName = $actualConstraint->getName();
440
                $this->assertIsString($actualConstraintName);
0 ignored issues
show
Bug introduced by
It seems like assertIsString() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

440
                $this->/** @scrutinizer ignore-call */ 
441
                       assertIsString($actualConstraintName);
Loading history...
441
                $actualConstraint->name(new AnyCaseValue($actualConstraintName));
0 ignored issues
show
Bug introduced by
It seems like $actualConstraintName can also be of type null and object; however, parameter $value of Yiisoft\Db\TestSupport\AnyCaseValue::__construct() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

441
                $actualConstraint->name(new AnyCaseValue(/** @scrutinizer ignore-type */ $actualConstraintName));
Loading history...
442
            }
443
        }
444
    }
445
446
    public function constraintsProviderTrait(): array
447
    {
448
        return [
449
            '1: primary key' => [
450
                'T_constraints_1',
451
                Schema::PRIMARY_KEY,
452
                (new Constraint())
453
                    ->name(AnyValue::getInstance())
454
                    ->columnNames(['C_id']),
455
            ],
456
            '1: check' => [
457
                'T_constraints_1',
458
                Schema::CHECKS,
459
                [
460
                    (new CheckConstraint())
461
                        ->name(AnyValue::getInstance())
462
                        ->columnNames(['C_check'])
463
                        ->expression("C_check <> ''"),
464
                ],
465
            ],
466
            '1: unique' => [
467
                'T_constraints_1',
468
                Schema::UNIQUES,
469
                [
470
                    (new Constraint())
471
                        ->name('CN_unique')
472
                        ->columnNames(['C_unique']),
473
                ],
474
            ],
475
            '1: index' => [
476
                'T_constraints_1',
477
                Schema::INDEXES,
478
                [
479
                    (new IndexConstraint())
480
                        ->name(AnyValue::getInstance())
481
                        ->columnNames(['C_id'])
482
                        ->unique(true)
483
                        ->primary(true),
484
                    (new IndexConstraint())
485
                        ->name('CN_unique')
486
                        ->columnNames(['C_unique'])
487
                        ->primary(false)
488
                        ->unique(true),
489
                ],
490
            ],
491
            '1: default' => ['T_constraints_1', Schema::DEFAULT_VALUES, false],
492
493
            '2: primary key' => [
494
                'T_constraints_2',
495
                Schema::PRIMARY_KEY,
496
                (new Constraint())
497
                    ->name('CN_pk')
498
                    ->columnNames(['C_id_1', 'C_id_2']),
499
            ],
500
            '2: unique' => [
501
                'T_constraints_2',
502
                Schema::UNIQUES,
503
                [
504
                    (new Constraint())
505
                        ->name('CN_constraints_2_multi')
506
                        ->columnNames(['C_index_2_1', 'C_index_2_2']),
507
                ],
508
            ],
509
            '2: index' => [
510
                'T_constraints_2',
511
                Schema::INDEXES,
512
                [
513
                    (new IndexConstraint())
514
                        ->name(AnyValue::getInstance())
515
                        ->columnNames(['C_id_1', 'C_id_2'])
516
                        ->unique(true)
517
                        ->primary(true),
518
                    (new IndexConstraint())
519
                        ->name('CN_constraints_2_single')
520
                        ->columnNames(['C_index_1'])
521
                        ->primary(false)
522
                        ->unique(false),
523
                    (new IndexConstraint())
524
                        ->name('CN_constraints_2_multi')
525
                        ->columnNames(['C_index_2_1', 'C_index_2_2'])
526
                        ->primary(false)
527
                        ->unique(true),
528
                ],
529
            ],
530
            '2: check' => ['T_constraints_2', Schema::CHECKS, []],
531
            '2: default' => ['T_constraints_2', Schema::DEFAULT_VALUES, false],
532
533
            '3: primary key' => ['T_constraints_3', Schema::PRIMARY_KEY, null],
534
            '3: foreign key' => [
535
                'T_constraints_3',
536
                Schema::FOREIGN_KEYS,
537
                [
538
                    (new ForeignKeyConstraint())
539
                        ->name('CN_constraints_3')
540
                        ->columnNames(['C_fk_id_1', 'C_fk_id_2'])
541
                        ->foreignTableName('T_constraints_2')
542
                        ->foreignColumnNames(['C_id_1', 'C_id_2'])
543
                        ->onDelete('CASCADE')
544
                        ->onUpdate('CASCADE'),
545
                ],
546
            ],
547
            '3: unique' => ['T_constraints_3', Schema::UNIQUES, []],
548
            '3: index' => [
549
                'T_constraints_3',
550
                Schema::INDEXES,
551
                [
552
                    (new IndexConstraint())
553
                        ->name('CN_constraints_3')
554
                        ->columnNames(['C_fk_id_1', 'C_fk_id_2'])
555
                        ->unique(false)
556
                        ->primary(false),
557
                ],
558
            ],
559
            '3: check' => ['T_constraints_3', Schema::CHECKS, []],
560
            '3: default' => ['T_constraints_3', Schema::DEFAULT_VALUES, false],
561
562
            '4: primary key' => [
563
                'T_constraints_4',
564
                Schema::PRIMARY_KEY,
565
                (new Constraint())
566
                    ->name(AnyValue::getInstance())
567
                    ->columnNames(['C_id']),
568
            ],
569
            '4: unique' => [
570
                'T_constraints_4',
571
                Schema::UNIQUES,
572
                [
573
                    (new Constraint())
574
                        ->name('CN_constraints_4')
575
                        ->columnNames(['C_col_1', 'C_col_2']),
576
                ],
577
            ],
578
            '4: check' => ['T_constraints_4', Schema::CHECKS, []],
579
            '4: default' => ['T_constraints_4', Schema::DEFAULT_VALUES, false],
580
        ];
581
    }
582
583
    public function pdoAttributesProviderTrait(): array
584
    {
585
        return [
586
            [[PDO::ATTR_EMULATE_PREPARES => true]],
587
            [[PDO::ATTR_EMULATE_PREPARES => false]],
588
        ];
589
    }
590
591
    public function tableSchemaCachePrefixesProviderTrait(): array
592
    {
593
        $configs = [
594
            [
595
                'prefix' => '',
596
                'name' => 'type',
597
            ],
598
            [
599
                'prefix' => '',
600
                'name' => '{{%type}}',
601
            ],
602
            [
603
                'prefix' => 'ty',
604
                'name' => '{{%pe}}',
605
            ],
606
        ];
607
608
        $data = [];
609
        foreach ($configs as $config) {
610
            foreach ($configs as $testConfig) {
611
                if ($config === $testConfig) {
612
                    continue;
613
                }
614
615
                $description = sprintf(
616
                    "%s (with '%s' prefix) against %s (with '%s' prefix)",
617
                    $config['name'],
618
                    $config['prefix'],
619
                    $testConfig['name'],
620
                    $testConfig['prefix']
621
                );
622
                $data[$description] = [
623
                    $config['prefix'],
624
                    $config['name'],
625
                    $testConfig['prefix'],
626
                    $testConfig['name'],
627
                ];
628
            }
629
        }
630
631
        return $data;
632
    }
633
634
    public function lowercaseConstraintsProviderTrait(): array
635
    {
636
        return $this->constraintsProvider();
0 ignored issues
show
Bug introduced by
The method constraintsProvider() does not exist on Yiisoft\Db\TestSupport\TestSchemaTrait. Did you maybe mean constraintsProviderTrait()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

636
        return $this->/** @scrutinizer ignore-call */ constraintsProvider();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
637
    }
638
639
    public function uppercaseConstraintsProviderTrait(): array
640
    {
641
        return $this->constraintsProvider();
642
    }
643
}
644