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); |
|
|
|
|
34
|
|
|
|
35
|
|
|
$db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); |
36
|
|
|
|
37
|
|
|
$this->assertCount(count($db->getSchema()->getTableNames()), $db->getSchema()->getTableSchemas()); |
|
|
|
|
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); |
|
|
|
|
52
|
|
|
$this->assertContainsOnlyInstancesOf(Constraint::class, $tablePks); |
|
|
|
|
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')); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testSchemaCache(): void |
136
|
|
|
{ |
137
|
|
|
$db = $this->getConnection(); |
138
|
|
|
|
139
|
|
|
$schema = $db->getSchema(); |
140
|
|
|
|
141
|
|
|
$this->assertNotNull($this->schemaCache); |
|
|
|
|
142
|
|
|
$this->schemaCache->setEnable(true); |
143
|
|
|
|
144
|
|
|
$noCacheTable = $schema->getTableSchema('type', true); |
145
|
|
|
$cachedTable = $schema->getTableSchema('type', false); |
146
|
|
|
|
147
|
|
|
$this->assertEquals($noCacheTable, $cachedTable); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$db->createCommand()->renameTable('type', 'type_test'); |
150
|
|
|
|
151
|
|
|
$noCacheTable = $schema->getTableSchema('type', true); |
152
|
|
|
|
153
|
|
|
$this->assertNotSame($noCacheTable, $cachedTable); |
|
|
|
|
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'])); |
|
|
|
|
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.'); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
fclose($fp); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testColumnSchema(): void |
219
|
|
|
{ |
220
|
|
|
$columns = $this->getExpectedColumns(); |
|
|
|
|
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( |
|
|
|
|
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( |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
public function testQuoterEscapingValue() |
350
|
|
|
{ |
351
|
|
|
$db = $this->getConnection(true); |
352
|
|
|
$quoter = $db->getQuoter(); |
353
|
|
|
|
354
|
|
|
$db->createCommand('delete from {{quoter}}')->execute(); |
355
|
|
|
$data = $this->generateQuoterEscapingValues(); |
356
|
|
|
|
357
|
|
|
foreach ($data as $index => $value) { |
358
|
|
|
$quotedName = $quoter->quoteValue('testValue_' . $index); |
359
|
|
|
$quoteValue = $quoter->quoteValue($value); |
360
|
|
|
|
361
|
|
|
$db->createCommand('insert into {{quoter}}([[name]], [[description]]) values(' . $quotedName . ', ' . $quoteValue . ')')->execute(); |
362
|
|
|
$result = $db->createCommand('select * from {{quoter}} where [[name]]=' . $quotedName)->queryOne(); |
363
|
|
|
$this->assertEquals($value, $result['description']); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function generateQuoterEscapingValues() |
368
|
|
|
{ |
369
|
|
|
$result = []; |
370
|
|
|
$stringLength = 16; |
371
|
|
|
for ($i = 1; $i < 128 - $stringLength; $i += $stringLength) { |
372
|
|
|
$str = ''; |
373
|
|
|
for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) { |
374
|
|
|
$str .= mb_chr($symbol, 'UTF-8'); |
375
|
|
|
} |
376
|
|
|
$result[] = $str; |
377
|
|
|
|
378
|
|
|
$str = ''; |
379
|
|
|
for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) { |
380
|
|
|
$str .= mb_chr($symbol, 'UTF-8') . mb_chr($symbol, 'UTF-8'); |
381
|
|
|
} |
382
|
|
|
$result[] = $str; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
return $result; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function testQuoterEscapingValueFull() |
389
|
|
|
{ |
390
|
|
|
$this->markTestSkipped('Very long test - only for check quoteValue'); |
|
|
|
|
391
|
|
|
$template = 'aaaaa{1}aaa{1}aaaabbbbb{2}bbbb{2}bbbb'; |
392
|
|
|
|
393
|
|
|
$db = $this->getConnection(true); |
394
|
|
|
$quoter = $db->getQuoter(); |
395
|
|
|
|
396
|
|
|
$db->createCommand('delete from {{quoter}}')->execute(); |
397
|
|
|
|
398
|
|
|
for ($symbol1 = 1; $symbol1 <= 127; $symbol1++) { |
399
|
|
|
for ($symbol2 = 1; $symbol2 <= 127; $symbol2++) { |
400
|
|
|
$quotedName = $quoter->quoteValue('test_' . $symbol1 . '_' . $symbol2); |
401
|
|
|
$testString = str_replace(['{1}', '{2}',], [chr($symbol1), chr($symbol2)], $template); |
402
|
|
|
|
403
|
|
|
$quoteValue = $quoter->quoteValue($testString); |
404
|
|
|
|
405
|
|
|
$db->createCommand('insert into {{quoter}}([[name]], [[description]]) values(' . $quotedName . ', ' . $quoteValue . ')')->execute(); |
406
|
|
|
$result = $db->createCommand('select * from {{quoter}} where [[name]]=' . $quotedName)->queryOne(); |
407
|
|
|
$this->assertEquals($testString, $result['description']); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
private function assertMetadataEquals($expected, $actual): void |
413
|
|
|
{ |
414
|
|
|
switch (strtolower(gettype($expected))) { |
415
|
|
|
case 'object': |
416
|
|
|
$this->assertIsObject($actual); |
417
|
|
|
break; |
418
|
|
|
case 'array': |
419
|
|
|
$this->assertIsArray($actual); |
420
|
|
|
break; |
421
|
|
|
case 'null': |
422
|
|
|
$this->assertNull($actual); |
423
|
|
|
break; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
if (is_array($expected)) { |
427
|
|
|
$this->normalizeArrayKeys($expected, false); |
428
|
|
|
/** @psalm-suppress PossiblyInvalidArgument */ |
429
|
|
|
$this->normalizeArrayKeys($actual, false); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
$this->normalizeConstraints($expected, $actual); |
433
|
|
|
|
434
|
|
|
if (is_array($expected)) { |
435
|
|
|
$this->normalizeArrayKeys($expected, true); |
436
|
|
|
$this->normalizeArrayKeys($actual, true); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
$this->assertEquals($expected, $actual); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
private function normalizeArrayKeys(array &$array, bool $caseSensitive): void |
443
|
|
|
{ |
444
|
|
|
$newArray = []; |
445
|
|
|
|
446
|
|
|
foreach ($array as $value) { |
447
|
|
|
if ($value instanceof Constraint) { |
448
|
|
|
$key = (array) $value; |
449
|
|
|
unset( |
450
|
|
|
$key["\000Yiisoft\Db\Constraint\Constraint\000name"], |
451
|
|
|
$key["\u0000Yiisoft\\Db\\Constraint\\ForeignKeyConstraint\u0000foreignSchemaName"] |
452
|
|
|
); |
453
|
|
|
|
454
|
|
|
foreach ($key as $keyName => $keyValue) { |
455
|
|
|
if ($keyValue instanceof AnyCaseValue) { |
456
|
|
|
$key[$keyName] = $keyValue->value; |
457
|
|
|
} elseif ($keyValue instanceof AnyValue) { |
458
|
|
|
$key[$keyName] = '[AnyValue]'; |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
ksort($key, SORT_STRING); |
463
|
|
|
|
464
|
|
|
$newArray[$caseSensitive |
465
|
|
|
? json_encode($key, JSON_THROW_ON_ERROR) |
466
|
|
|
: strtolower(json_encode($key, JSON_THROW_ON_ERROR))] = $value; |
467
|
|
|
} else { |
468
|
|
|
$newArray[] = $value; |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
ksort($newArray, SORT_STRING); |
473
|
|
|
|
474
|
|
|
$array = $newArray; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
private function normalizeConstraints(&$expected, &$actual): void |
478
|
|
|
{ |
479
|
|
|
if (is_array($expected)) { |
480
|
|
|
foreach ($expected as $key => $value) { |
481
|
|
|
if (!$value instanceof Constraint || !isset($actual[$key]) || !$actual[$key] instanceof Constraint) { |
482
|
|
|
continue; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
$this->normalizeConstraintPair($value, $actual[$key]); |
486
|
|
|
} |
487
|
|
|
} elseif ($expected instanceof Constraint && $actual instanceof Constraint) { |
488
|
|
|
$this->normalizeConstraintPair($expected, $actual); |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
private function normalizeConstraintPair(Constraint $expectedConstraint, Constraint $actualConstraint): void |
493
|
|
|
{ |
494
|
|
|
if (get_class($expectedConstraint) !== get_class($actualConstraint)) { |
495
|
|
|
return; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
foreach (array_keys((array) $expectedConstraint) as $name) { |
499
|
|
|
if ($expectedConstraint->getName() instanceof AnyValue) { |
500
|
|
|
$actualConstraint->name($expectedConstraint->getName()); |
501
|
|
|
} elseif ($expectedConstraint->getName() instanceof AnyCaseValue) { |
502
|
|
|
$actualConstraintName = $actualConstraint->getName(); |
503
|
|
|
$this->assertIsString($actualConstraintName); |
|
|
|
|
504
|
|
|
$actualConstraint->name(new AnyCaseValue($actualConstraintName)); |
|
|
|
|
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
public function constraintsProviderTrait(): array |
510
|
|
|
{ |
511
|
|
|
return [ |
512
|
|
|
'1: primary key' => [ |
513
|
|
|
'T_constraints_1', |
514
|
|
|
Schema::PRIMARY_KEY, |
515
|
|
|
(new Constraint()) |
516
|
|
|
->name(AnyValue::getInstance()) |
517
|
|
|
->columnNames(['C_id']), |
518
|
|
|
], |
519
|
|
|
'1: check' => [ |
520
|
|
|
'T_constraints_1', |
521
|
|
|
Schema::CHECKS, |
522
|
|
|
[ |
523
|
|
|
(new CheckConstraint()) |
524
|
|
|
->name(AnyValue::getInstance()) |
525
|
|
|
->columnNames(['C_check']) |
526
|
|
|
->expression("C_check <> ''"), |
527
|
|
|
], |
528
|
|
|
], |
529
|
|
|
'1: unique' => [ |
530
|
|
|
'T_constraints_1', |
531
|
|
|
Schema::UNIQUES, |
532
|
|
|
[ |
533
|
|
|
(new Constraint()) |
534
|
|
|
->name('CN_unique') |
535
|
|
|
->columnNames(['C_unique']), |
536
|
|
|
], |
537
|
|
|
], |
538
|
|
|
'1: index' => [ |
539
|
|
|
'T_constraints_1', |
540
|
|
|
Schema::INDEXES, |
541
|
|
|
[ |
542
|
|
|
(new IndexConstraint()) |
543
|
|
|
->name(AnyValue::getInstance()) |
544
|
|
|
->columnNames(['C_id']) |
545
|
|
|
->unique(true) |
546
|
|
|
->primary(true), |
547
|
|
|
(new IndexConstraint()) |
548
|
|
|
->name('CN_unique') |
549
|
|
|
->columnNames(['C_unique']) |
550
|
|
|
->primary(false) |
551
|
|
|
->unique(true), |
552
|
|
|
], |
553
|
|
|
], |
554
|
|
|
'1: default' => ['T_constraints_1', Schema::DEFAULT_VALUES, false], |
555
|
|
|
|
556
|
|
|
'2: primary key' => [ |
557
|
|
|
'T_constraints_2', |
558
|
|
|
Schema::PRIMARY_KEY, |
559
|
|
|
(new Constraint()) |
560
|
|
|
->name('CN_pk') |
561
|
|
|
->columnNames(['C_id_1', 'C_id_2']), |
562
|
|
|
], |
563
|
|
|
'2: unique' => [ |
564
|
|
|
'T_constraints_2', |
565
|
|
|
Schema::UNIQUES, |
566
|
|
|
[ |
567
|
|
|
(new Constraint()) |
568
|
|
|
->name('CN_constraints_2_multi') |
569
|
|
|
->columnNames(['C_index_2_1', 'C_index_2_2']), |
570
|
|
|
], |
571
|
|
|
], |
572
|
|
|
'2: index' => [ |
573
|
|
|
'T_constraints_2', |
574
|
|
|
Schema::INDEXES, |
575
|
|
|
[ |
576
|
|
|
(new IndexConstraint()) |
577
|
|
|
->name(AnyValue::getInstance()) |
578
|
|
|
->columnNames(['C_id_1', 'C_id_2']) |
579
|
|
|
->unique(true) |
580
|
|
|
->primary(true), |
581
|
|
|
(new IndexConstraint()) |
582
|
|
|
->name('CN_constraints_2_single') |
583
|
|
|
->columnNames(['C_index_1']) |
584
|
|
|
->primary(false) |
585
|
|
|
->unique(false), |
586
|
|
|
(new IndexConstraint()) |
587
|
|
|
->name('CN_constraints_2_multi') |
588
|
|
|
->columnNames(['C_index_2_1', 'C_index_2_2']) |
589
|
|
|
->primary(false) |
590
|
|
|
->unique(true), |
591
|
|
|
], |
592
|
|
|
], |
593
|
|
|
'2: check' => ['T_constraints_2', Schema::CHECKS, []], |
594
|
|
|
'2: default' => ['T_constraints_2', Schema::DEFAULT_VALUES, false], |
595
|
|
|
|
596
|
|
|
'3: primary key' => ['T_constraints_3', Schema::PRIMARY_KEY, null], |
597
|
|
|
'3: foreign key' => [ |
598
|
|
|
'T_constraints_3', |
599
|
|
|
Schema::FOREIGN_KEYS, |
600
|
|
|
[ |
601
|
|
|
(new ForeignKeyConstraint()) |
602
|
|
|
->name('CN_constraints_3') |
603
|
|
|
->columnNames(['C_fk_id_1', 'C_fk_id_2']) |
604
|
|
|
->foreignTableName('T_constraints_2') |
605
|
|
|
->foreignColumnNames(['C_id_1', 'C_id_2']) |
606
|
|
|
->onDelete('CASCADE') |
607
|
|
|
->onUpdate('CASCADE'), |
608
|
|
|
], |
609
|
|
|
], |
610
|
|
|
'3: unique' => ['T_constraints_3', Schema::UNIQUES, []], |
611
|
|
|
'3: index' => [ |
612
|
|
|
'T_constraints_3', |
613
|
|
|
Schema::INDEXES, |
614
|
|
|
[ |
615
|
|
|
(new IndexConstraint()) |
616
|
|
|
->name('CN_constraints_3') |
617
|
|
|
->columnNames(['C_fk_id_1', 'C_fk_id_2']) |
618
|
|
|
->unique(false) |
619
|
|
|
->primary(false), |
620
|
|
|
], |
621
|
|
|
], |
622
|
|
|
'3: check' => ['T_constraints_3', Schema::CHECKS, []], |
623
|
|
|
'3: default' => ['T_constraints_3', Schema::DEFAULT_VALUES, false], |
624
|
|
|
|
625
|
|
|
'4: primary key' => [ |
626
|
|
|
'T_constraints_4', |
627
|
|
|
Schema::PRIMARY_KEY, |
628
|
|
|
(new Constraint()) |
629
|
|
|
->name(AnyValue::getInstance()) |
630
|
|
|
->columnNames(['C_id']), |
631
|
|
|
], |
632
|
|
|
'4: unique' => [ |
633
|
|
|
'T_constraints_4', |
634
|
|
|
Schema::UNIQUES, |
635
|
|
|
[ |
636
|
|
|
(new Constraint()) |
637
|
|
|
->name('CN_constraints_4') |
638
|
|
|
->columnNames(['C_col_1', 'C_col_2']), |
639
|
|
|
], |
640
|
|
|
], |
641
|
|
|
'4: check' => ['T_constraints_4', Schema::CHECKS, []], |
642
|
|
|
'4: default' => ['T_constraints_4', Schema::DEFAULT_VALUES, false], |
643
|
|
|
]; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
public function pdoAttributesProviderTrait(): array |
647
|
|
|
{ |
648
|
|
|
return [ |
649
|
|
|
[[PDO::ATTR_EMULATE_PREPARES => true]], |
650
|
|
|
[[PDO::ATTR_EMULATE_PREPARES => false]], |
651
|
|
|
]; |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
public function tableSchemaCachePrefixesProviderTrait(): array |
655
|
|
|
{ |
656
|
|
|
$configs = [ |
657
|
|
|
[ |
658
|
|
|
'prefix' => '', |
659
|
|
|
'name' => 'type', |
660
|
|
|
], |
661
|
|
|
[ |
662
|
|
|
'prefix' => '', |
663
|
|
|
'name' => '{{%type}}', |
664
|
|
|
], |
665
|
|
|
[ |
666
|
|
|
'prefix' => 'ty', |
667
|
|
|
'name' => '{{%pe}}', |
668
|
|
|
], |
669
|
|
|
]; |
670
|
|
|
|
671
|
|
|
$data = []; |
672
|
|
|
foreach ($configs as $config) { |
673
|
|
|
foreach ($configs as $testConfig) { |
674
|
|
|
if ($config === $testConfig) { |
675
|
|
|
continue; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
$description = sprintf( |
679
|
|
|
"%s (with '%s' prefix) against %s (with '%s' prefix)", |
680
|
|
|
$config['name'], |
681
|
|
|
$config['prefix'], |
682
|
|
|
$testConfig['name'], |
683
|
|
|
$testConfig['prefix'] |
684
|
|
|
); |
685
|
|
|
$data[$description] = [ |
686
|
|
|
$config['prefix'], |
687
|
|
|
$config['name'], |
688
|
|
|
$testConfig['prefix'], |
689
|
|
|
$testConfig['name'], |
690
|
|
|
]; |
691
|
|
|
} |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
return $data; |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
public function lowercaseConstraintsProviderTrait(): array |
698
|
|
|
{ |
699
|
|
|
return $this->constraintsProvider(); |
|
|
|
|
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
public function uppercaseConstraintsProviderTrait(): array |
703
|
|
|
{ |
704
|
|
|
return $this->constraintsProvider(); |
705
|
|
|
} |
706
|
|
|
} |
707
|
|
|
|