-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AbstractPlatformTestCase.php
1494 lines (1223 loc) · 51 KB
/
AbstractPlatformTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Doctrine\DBAL\Tests\Platforms;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\InvalidLockMode;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\UniqueConstraint;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use function get_class;
use function implode;
use function sprintf;
use function str_repeat;
/** @template T of AbstractPlatform */
abstract class AbstractPlatformTestCase extends TestCase
{
/** @var T */
protected AbstractPlatform $platform;
private ?Type $backedUpType = null;
/** @return T */
abstract public function createPlatform(): AbstractPlatform;
protected function setUp(): void
{
$this->platform = $this->createPlatform();
}
public function testQuoteIdentifier(): void
{
if ($this->platform instanceof SQLServerPlatform) {
self::markTestSkipped('Not working this way on mssql.');
}
$c = $this->platform->getIdentifierQuoteCharacter();
self::assertEquals($c . 'test' . $c, $this->platform->quoteIdentifier('test'));
self::assertEquals($c . 'test' . $c . '.' . $c . 'test' . $c, $this->platform->quoteIdentifier('test.test'));
self::assertEquals(str_repeat($c, 4), $this->platform->quoteIdentifier($c));
}
public function testQuoteSingleIdentifier(): void
{
if ($this->platform instanceof SQLServerPlatform) {
self::markTestSkipped('Not working this way on mssql.');
}
$c = $this->platform->getIdentifierQuoteCharacter();
self::assertEquals($c . 'test' . $c, $this->platform->quoteSingleIdentifier('test'));
self::assertEquals($c . 'test.test' . $c, $this->platform->quoteSingleIdentifier('test.test'));
self::assertEquals(str_repeat($c, 4), $this->platform->quoteSingleIdentifier($c));
}
/** @dataProvider getReturnsForeignKeyReferentialActionSQL */
public function testReturnsForeignKeyReferentialActionSQL(string $action, string $expectedSQL): void
{
self::assertSame($expectedSQL, $this->platform->getForeignKeyReferentialActionSQL($action));
}
/** @return mixed[][] */
public static function getReturnsForeignKeyReferentialActionSQL(): iterable
{
return [
['CASCADE', 'CASCADE'],
['SET NULL', 'SET NULL'],
['NO ACTION', 'NO ACTION'],
['RESTRICT', 'RESTRICT'],
['SET DEFAULT', 'SET DEFAULT'],
['CaScAdE', 'CASCADE'],
];
}
public function testGetInvalidForeignKeyReferentialActionSQL(): void
{
$this->expectException(InvalidArgumentException::class);
$this->platform->getForeignKeyReferentialActionSQL('unknown');
}
public function testGetUnknownDoctrineMappingType(): void
{
$this->expectException(Exception::class);
$this->platform->getDoctrineTypeMapping('foobar');
}
public function testRegisterDoctrineMappingType(): void
{
$this->platform->registerDoctrineTypeMapping('foo', Types::INTEGER);
self::assertEquals(Types::INTEGER, $this->platform->getDoctrineTypeMapping('foo'));
}
public function testCaseInsensitiveDoctrineTypeMappingFromType(): void
{
$type = new class () extends Type {
/**
* {@inheritDoc}
*/
public function getMappedDatabaseTypes(AbstractPlatform $platform): array
{
return ['TESTTYPE'];
}
public function getName(): string
{
return 'testtype';
}
/**
* {@inheritDoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getDecimalTypeDeclarationSQL($column);
}
};
if (Type::hasType($type->getName())) {
Type::overrideType($type->getName(), get_class($type));
} else {
Type::addType($type->getName(), get_class($type));
}
self::assertSame($type->getName(), $this->platform->getDoctrineTypeMapping('TeStTyPe'));
}
public function testRegisterUnknownDoctrineMappingType(): void
{
$this->expectException(Exception::class);
$this->platform->registerDoctrineTypeMapping('foo', 'bar');
}
/** @psalm-suppress DeprecatedConstant */
public function testRegistersCommentedDoctrineMappingTypeImplicitly(): void
{
$type = Type::getType(Types::ARRAY);
$this->platform->registerDoctrineTypeMapping('foo', Types::ARRAY);
self::assertTrue($this->platform->isCommentedDoctrineType($type));
}
/** @dataProvider getIsCommentedDoctrineType */
public function testIsCommentedDoctrineType(Type $type, bool $commented): void
{
self::assertSame($commented, $this->platform->isCommentedDoctrineType($type));
}
/** @return mixed[] */
public function getIsCommentedDoctrineType(): iterable
{
$this->setUp();
$data = [];
foreach (Type::getTypesMap() as $typeName => $className) {
$type = Type::getType($typeName);
$data[$typeName] = [
$type,
$type->requiresSQLCommentHint($this->platform),
];
}
return $data;
}
public function testCreateWithNoColumns(): void
{
$table = new Table('test');
$this->expectException(Exception::class);
$this->platform->getCreateTableSQL($table);
}
public function testGeneratesTableCreationSql(): void
{
$table = new Table('test');
$table->addColumn('id', Types::INTEGER, ['notnull' => true, 'autoincrement' => true]);
$table->addColumn('test', Types::STRING, ['notnull' => false, 'length' => 255]);
$table->setPrimaryKey(['id']);
$sql = $this->platform->getCreateTableSQL($table);
self::assertEquals($this->getGenerateTableSql(), $sql[0]);
}
abstract public function getGenerateTableSql(): string;
public function testGenerateTableWithMultiColumnUniqueIndex(): void
{
$table = new Table('test');
$table->addColumn('foo', Types::STRING, ['notnull' => false, 'length' => 255]);
$table->addColumn('bar', Types::STRING, ['notnull' => false, 'length' => 255]);
$table->addUniqueIndex(['foo', 'bar']);
$sql = $this->platform->getCreateTableSQL($table);
self::assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
}
/** @return string[] */
abstract public function getGenerateTableWithMultiColumnUniqueIndexSql(): array;
public function testGeneratesIndexCreationSql(): void
{
$indexDef = new Index('my_idx', ['user_name', 'last_login']);
self::assertEquals(
$this->getGenerateIndexSql(),
$this->platform->getCreateIndexSQL($indexDef, 'mytable'),
);
}
abstract public function getGenerateIndexSql(): string;
public function testGeneratesUniqueIndexCreationSql(): void
{
$indexDef = new Index('index_name', ['test', 'test2'], true);
$sql = $this->platform->getCreateIndexSQL($indexDef, 'test');
self::assertEquals($this->getGenerateUniqueIndexSql(), $sql);
}
abstract public function getGenerateUniqueIndexSql(): string;
public function testGeneratesPartialIndexesSqlOnlyWhenSupportingPartialIndexes(): void
{
$where = 'test IS NULL AND test2 IS NOT NULL';
$indexDef = new Index('name', ['test', 'test2'], false, false, [], ['where' => $where]);
$uniqueConstraint = new UniqueConstraint('name', ['test', 'test2'], [], []);
$expected = ' WHERE ' . $where;
$indexes = [];
if ($this->supportsInlineIndexDeclaration()) {
$indexes[] = $this->platform->getIndexDeclarationSQL('name', $indexDef);
}
$uniqueConstraintSQL = $this->platform->getUniqueConstraintDeclarationSQL('name', $uniqueConstraint);
$this->assertStringEndsNotWith($expected, $uniqueConstraintSQL, 'WHERE clause should NOT be present');
$indexes[] = $this->platform->getCreateIndexSQL($indexDef, 'table');
foreach ($indexes as $index) {
if ($this->platform->supportsPartialIndexes()) {
self::assertStringEndsWith($expected, $index, 'WHERE clause should be present');
} else {
self::assertStringEndsNotWith($expected, $index, 'WHERE clause should NOT be present');
}
}
}
public function testGeneratesForeignKeyCreationSql(): void
{
$fk = new ForeignKeyConstraint(['fk_name_id'], 'other_table', ['id'], '');
$sql = $this->platform->getCreateForeignKeySQL($fk, 'test');
self::assertEquals($sql, $this->getGenerateForeignKeySql());
}
abstract protected function getGenerateForeignKeySql(): string;
public function testGeneratesConstraintCreationSql(): void
{
$idx = new Index('constraint_name', ['test'], true, false);
$sql = $this->platform->getCreateConstraintSQL($idx, 'test');
self::assertEquals($this->getGenerateConstraintUniqueIndexSql(), $sql);
$pk = new Index('constraint_name', ['test'], true, true);
$sql = $this->platform->getCreateConstraintSQL($pk, 'test');
self::assertEquals($this->getGenerateConstraintPrimaryIndexSql(), $sql);
$uc = new UniqueConstraint('constraint_name', ['test']);
$sql = $this->platform->getCreateConstraintSQL($uc, 'test');
self::assertEquals($this->getGenerateConstraintUniqueIndexSql(), $sql);
$fk = new ForeignKeyConstraint(['fk_name'], 'foreign', ['id'], 'constraint_fk');
$sql = $this->platform->getCreateConstraintSQL($fk, 'test');
self::assertEquals($this->getGenerateConstraintForeignKeySql($fk), $sql);
}
protected function getBitAndComparisonExpressionSql(string $value1, string $value2): string
{
return '(' . $value1 . ' & ' . $value2 . ')';
}
public function testGeneratesBitAndComparisonExpressionSql(): void
{
$sql = $this->platform->getBitAndComparisonExpression(2, 4);
self::assertEquals($this->getBitAndComparisonExpressionSql(2, 4), $sql);
}
protected function getBitOrComparisonExpressionSql(string $value1, string $value2): string
{
return '(' . $value1 . ' | ' . $value2 . ')';
}
public function testGeneratesBitOrComparisonExpressionSql(): void
{
$sql = $this->platform->getBitOrComparisonExpression(2, 4);
self::assertEquals($this->getBitOrComparisonExpressionSql(2, 4), $sql);
}
public function getGenerateConstraintUniqueIndexSql(): string
{
return 'ALTER TABLE test ADD CONSTRAINT constraint_name UNIQUE (test)';
}
public function getGenerateConstraintPrimaryIndexSql(): string
{
return 'ALTER TABLE test ADD CONSTRAINT constraint_name PRIMARY KEY (test)';
}
public function getGenerateConstraintForeignKeySql(ForeignKeyConstraint $fk): string
{
$quotedForeignTable = $fk->getQuotedForeignTableName($this->platform);
return sprintf(
'ALTER TABLE test ADD CONSTRAINT constraint_fk FOREIGN KEY (fk_name) REFERENCES %s (id)',
$quotedForeignTable,
);
}
public function testGetCustomColumnDeclarationSql(): void
{
self::assertEquals(
'foo MEDIUMINT(6) UNSIGNED',
$this->platform->getColumnDeclarationSQL('foo', ['columnDefinition' => 'MEDIUMINT(6) UNSIGNED']),
);
}
public function testGetCreateTableSqlDispatchEvent(): void
{
$listenerMock = $this->createMock(GetCreateTableSqlDispatchEventListener::class);
$listenerMock
->expects(self::once())
->method('onSchemaCreateTable');
$listenerMock
->expects(self::exactly(2))
->method('onSchemaCreateTableColumn');
$eventManager = new EventManager();
$eventManager->addEventListener([
Events::onSchemaCreateTable,
Events::onSchemaCreateTableColumn,
], $listenerMock);
$this->platform->setEventManager($eventManager);
$table = new Table('test');
$table->addColumn('foo', Types::STRING, ['notnull' => false, 'length' => 255]);
$table->addColumn('bar', Types::STRING, ['notnull' => false, 'length' => 255]);
$this->platform->getCreateTableSQL($table);
}
public function testGetDropTableSqlDispatchEvent(): void
{
$listenerMock = $this->createMock(GetDropTableSqlDispatchEventListener::class);
$listenerMock
->expects(self::once())
->method('onSchemaDropTable');
$eventManager = new EventManager();
$eventManager->addEventListener([Events::onSchemaDropTable], $listenerMock);
$this->platform->setEventManager($eventManager);
$this->platform->getDropTableSQL('TABLE');
}
public function testGetAlterTableSqlDispatchEvent(): void
{
$listenerMock = $this->createMock(GetAlterTableSqlDispatchEventListener::class);
$listenerMock
->expects(self::once())
->method('onSchemaAlterTable');
$listenerMock
->expects(self::once())
->method('onSchemaAlterTableAddColumn');
$listenerMock
->expects(self::once())
->method('onSchemaAlterTableRemoveColumn');
$listenerMock
->expects(self::once())
->method('onSchemaAlterTableChangeColumn');
$listenerMock
->expects(self::once())
->method('onSchemaAlterTableRenameColumn');
$eventManager = new EventManager();
$events = [
Events::onSchemaAlterTable,
Events::onSchemaAlterTableAddColumn,
Events::onSchemaAlterTableRemoveColumn,
Events::onSchemaAlterTableChangeColumn,
Events::onSchemaAlterTableRenameColumn,
];
$eventManager->addEventListener($events, $listenerMock);
$this->platform->setEventManager($eventManager);
$table = new Table('mytable');
$table->addColumn('removed', Types::INTEGER);
$table->addColumn('changed', Types::INTEGER);
$table->addColumn('renamed', Types::INTEGER);
$tableDiff = new TableDiff('mytable');
$tableDiff->fromTable = $table;
$tableDiff->addedColumns['added'] = new Column('added', Type::getType(Types::INTEGER), []);
$tableDiff->removedColumns['removed'] = new Column('removed', Type::getType(Types::INTEGER), []);
$tableDiff->changedColumns['changed'] = new ColumnDiff(
'changed',
new Column(
'changed2',
Type::getType(Types::STRING),
[],
),
[],
);
$tableDiff->renamedColumns['renamed'] = new Column('renamed2', Type::getType(Types::INTEGER), []);
$this->platform->getAlterTableSQL($tableDiff);
}
public function testCreateTableColumnComments(): void
{
$table = new Table('test');
$table->addColumn('id', Types::INTEGER, ['comment' => 'This is a comment']);
$table->setPrimaryKey(['id']);
self::assertEquals($this->getCreateTableColumnCommentsSQL(), $this->platform->getCreateTableSQL($table));
}
public function testAlterTableColumnComments(): void
{
$tableDiff = new TableDiff('mytable');
$tableDiff->addedColumns['quota'] = new Column(
'quota',
Type::getType(Types::INTEGER),
['comment' => 'A comment'],
);
$tableDiff->changedColumns['foo'] = new ColumnDiff(
'foo',
new Column(
'foo',
Type::getType(Types::STRING),
),
['comment'],
);
$tableDiff->changedColumns['bar'] = new ColumnDiff(
'bar',
new Column(
'baz',
Type::getType(Types::STRING),
['comment' => 'B comment'],
),
['comment'],
);
self::assertEquals($this->getAlterTableColumnCommentsSQL(), $this->platform->getAlterTableSQL($tableDiff));
}
public function testCreateTableColumnTypeComments(): void
{
$table = new Table('test');
$table->addColumn('id', Types::INTEGER);
/** @psalm-suppress DeprecatedConstant */
$table->addColumn('data', Types::ARRAY);
$table->setPrimaryKey(['id']);
self::assertEquals($this->getCreateTableColumnTypeCommentsSQL(), $this->platform->getCreateTableSQL($table));
}
/** @return string[] */
public function getCreateTableColumnCommentsSQL(): array
{
self::markTestSkipped('Platform does not support Column comments.');
}
/** @return string[] */
public function getAlterTableColumnCommentsSQL(): array
{
self::markTestSkipped('Platform does not support Column comments.');
}
/** @return string[] */
public function getCreateTableColumnTypeCommentsSQL(): array
{
self::markTestSkipped('Platform does not support Column comments.');
}
public function testGetDefaultValueDeclarationSQL(): void
{
// non-timestamp value will get single quotes
self::assertEquals(" DEFAULT 'non_timestamp'", $this->platform->getDefaultValueDeclarationSQL([
'type' => Type::getType(Types::STRING),
'default' => 'non_timestamp',
]));
}
public function testGetDefaultValueDeclarationSQLDateTime(): void
{
$types = [
Types::DATETIME_MUTABLE,
Types::DATETIMETZ_MUTABLE,
Types::DATETIME_IMMUTABLE,
Types::DATETIMETZ_IMMUTABLE,
];
// timestamps on datetime types should not be quoted
foreach ($types as $type) {
self::assertSame(
' DEFAULT ' . $this->platform->getCurrentTimestampSQL(),
$this->platform->getDefaultValueDeclarationSQL([
'type' => Type::getType($type),
'default' => $this->platform->getCurrentTimestampSQL(),
]),
);
}
}
public function testGetDefaultValueDeclarationSQLForIntegerTypes(): void
{
foreach ([Types::BIGINT, Types::INTEGER, Types::SMALLINT] as $type) {
self::assertEquals(
' DEFAULT 1',
$this->platform->getDefaultValueDeclarationSQL([
'type' => Type::getType($type),
'default' => 1,
]),
);
}
}
public function testGetDefaultValueDeclarationSQLForDateType(): void
{
$currentDateSql = $this->platform->getCurrentDateSQL();
foreach ([Types::DATE_MUTABLE, Types::DATE_IMMUTABLE] as $type) {
self::assertSame(
' DEFAULT ' . $currentDateSql,
$this->platform->getDefaultValueDeclarationSQL([
'type' => Type::getType($type),
'default' => $currentDateSql,
]),
);
}
}
public function testKeywordList(): void
{
$keywordList = $this->platform->getReservedKeywordsList();
self::assertInstanceOf(KeywordList::class, $keywordList);
self::assertTrue($keywordList->isKeyword('table'));
}
public function testQuotedColumnInPrimaryKeyPropagation(): void
{
$table = new Table('`quoted`');
$table->addColumn('create', Types::STRING);
$table->setPrimaryKey(['create']);
$sql = $this->platform->getCreateTableSQL($table);
self::assertEquals($this->getQuotedColumnInPrimaryKeySQL(), $sql);
}
/** @return string[] */
abstract protected function getQuotedColumnInPrimaryKeySQL(): array;
/** @return string[] */
abstract protected function getQuotedColumnInIndexSQL(): array;
/** @return string[] */
abstract protected function getQuotedNameInIndexSQL(): array;
/** @return string[] */
abstract protected function getQuotedColumnInForeignKeySQL(): array;
public function testQuotedColumnInIndexPropagation(): void
{
$table = new Table('`quoted`');
$table->addColumn('create', Types::STRING);
$table->addIndex(['create']);
$sql = $this->platform->getCreateTableSQL($table);
self::assertEquals($this->getQuotedColumnInIndexSQL(), $sql);
}
public function testQuotedNameInIndexSQL(): void
{
$table = new Table('test');
$table->addColumn('column1', Types::STRING);
$table->addIndex(['column1'], '`key`');
$sql = $this->platform->getCreateTableSQL($table);
self::assertEquals($this->getQuotedNameInIndexSQL(), $sql);
}
public function testQuotedColumnInForeignKeyPropagation(): void
{
$table = new Table('`quoted`');
$table->addColumn('create', Types::STRING);
$table->addColumn('foo', Types::STRING);
$table->addColumn('`bar`', Types::STRING);
// Foreign table with reserved keyword as name (needs quotation).
$foreignTable = new Table('foreign');
// Foreign column with reserved keyword as name (needs quotation).
$foreignTable->addColumn('create', Types::STRING);
// Foreign column with non-reserved keyword as name (does not need quotation).
$foreignTable->addColumn('bar', Types::STRING);
// Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
$foreignTable->addColumn('`foo-bar`', Types::STRING);
$table->addForeignKeyConstraint(
$foreignTable,
['create', 'foo', '`bar`'],
['create', 'bar', '`foo-bar`'],
[],
'FK_WITH_RESERVED_KEYWORD',
);
// Foreign table with non-reserved keyword as name (does not need quotation).
$foreignTable = new Table('foo');
// Foreign column with reserved keyword as name (needs quotation).
$foreignTable->addColumn('create', Types::STRING);
// Foreign column with non-reserved keyword as name (does not need quotation).
$foreignTable->addColumn('bar', Types::STRING);
// Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
$foreignTable->addColumn('`foo-bar`', Types::STRING);
$table->addForeignKeyConstraint(
$foreignTable,
['create', 'foo', '`bar`'],
['create', 'bar', '`foo-bar`'],
[],
'FK_WITH_NON_RESERVED_KEYWORD',
);
// Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
$foreignTable = new Table('`foo-bar`');
// Foreign column with reserved keyword as name (needs quotation).
$foreignTable->addColumn('create', Types::STRING);
// Foreign column with non-reserved keyword as name (does not need quotation).
$foreignTable->addColumn('bar', Types::STRING);
// Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
$foreignTable->addColumn('`foo-bar`', Types::STRING);
$table->addForeignKeyConstraint(
$foreignTable,
['create', 'foo', '`bar`'],
['create', 'bar', '`foo-bar`'],
[],
'FK_WITH_INTENDED_QUOTATION',
);
$sql = $this->platform->getCreateTableSQL(
$table,
AbstractPlatform::CREATE_INDEXES | AbstractPlatform::CREATE_FOREIGNKEYS,
);
self::assertEquals($this->getQuotedColumnInForeignKeySQL(), $sql);
}
public function testQuotesReservedKeywordInUniqueConstraintDeclarationSQL(): void
{
$constraint = new UniqueConstraint('select', ['foo'], [], []);
self::assertSame(
$this->getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(),
$this->platform->getUniqueConstraintDeclarationSQL('select', $constraint),
);
}
abstract protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(): string;
public function testQuotesReservedKeywordInTruncateTableSQL(): void
{
self::assertSame(
$this->getQuotesReservedKeywordInTruncateTableSQL(),
$this->platform->getTruncateTableSQL('select'),
);
}
abstract protected function getQuotesReservedKeywordInTruncateTableSQL(): string;
public function testQuotesReservedKeywordInIndexDeclarationSQL(): void
{
$index = new Index('select', ['foo']);
if (! $this->supportsInlineIndexDeclaration()) {
$this->expectException(Exception::class);
}
self::assertSame(
$this->getQuotesReservedKeywordInIndexDeclarationSQL(),
$this->platform->getIndexDeclarationSQL('select', $index),
);
}
abstract protected function getQuotesReservedKeywordInIndexDeclarationSQL(): string;
protected function supportsInlineIndexDeclaration(): bool
{
return true;
}
public function testSupportsCommentOnStatement(): void
{
self::assertSame($this->supportsCommentOnStatement(), $this->platform->supportsCommentOnStatement());
}
protected function supportsCommentOnStatement(): bool
{
return false;
}
public function testGetCreateSchemaSQL(): void
{
$this->expectException(Exception::class);
$this->platform->getCreateSchemaSQL('schema');
}
public function testAlterTableChangeQuotedColumn(): void
{
$table = new Table('mytable');
$table->addColumn('select', Types::INTEGER);
$tableDiff = new TableDiff('mytable');
$tableDiff->fromTable = $table;
$tableDiff->changedColumns['select'] = new ColumnDiff(
'select',
new Column(
'select',
Type::getType(Types::STRING),
),
['type'],
);
self::assertStringContainsString(
$this->platform->quoteIdentifier('select'),
implode(';', $this->platform->getAlterTableSQL($tableDiff)),
);
}
public function testUsesSequenceEmulatedIdentityColumns(): void
{
self::assertFalse($this->platform->usesSequenceEmulatedIdentityColumns());
}
/** @group DBAL-563 */
public function testReturnsIdentitySequenceName(): void
{
$this->expectException(Exception::class);
$this->platform->getIdentitySequenceName('mytable', 'mycolumn');
}
public function testReturnsBinaryDefaultLength(): void
{
self::assertSame($this->getBinaryDefaultLength(), $this->platform->getBinaryDefaultLength());
}
protected function getBinaryDefaultLength(): int
{
return 255;
}
public function testReturnsBinaryMaxLength(): void
{
self::assertSame($this->getBinaryMaxLength(), $this->platform->getBinaryMaxLength());
}
protected function getBinaryMaxLength(): int
{
return 4000;
}
public function testReturnsBinaryTypeDeclarationSQL(): void
{
$this->expectException(Exception::class);
$this->platform->getBinaryTypeDeclarationSQL([]);
}
public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL(): void
{
$this->markTestSkipped('Not applicable to the platform');
}
public function hasNativeJsonType(): void
{
self::assertFalse($this->platform->hasNativeJsonType());
}
public function testReturnsJsonTypeDeclarationSQL(): void
{
$column = [
'length' => 666,
'notnull' => true,
'type' => Type::getType(Types::JSON),
];
self::assertSame(
$this->platform->getClobTypeDeclarationSQL($column),
$this->platform->getJsonTypeDeclarationSQL($column),
);
}
public function testAlterTableRenameIndex(): void
{
$tableDiff = new TableDiff('mytable');
$tableDiff->fromTable = new Table('mytable');
$tableDiff->fromTable->addColumn('id', Types::INTEGER);
$tableDiff->fromTable->setPrimaryKey(['id']);
$tableDiff->renamedIndexes = [
'idx_foo' => new Index('idx_bar', ['id']),
];
self::assertSame(
$this->getAlterTableRenameIndexSQL(),
$this->platform->getAlterTableSQL($tableDiff),
);
}
/** @return string[] */
protected function getAlterTableRenameIndexSQL(): array
{
return [
'DROP INDEX idx_foo',
'CREATE INDEX idx_bar ON mytable (id)',
];
}
public function testQuotesAlterTableRenameIndex(): void
{
$tableDiff = new TableDiff('table');
$tableDiff->fromTable = new Table('table');
$tableDiff->fromTable->addColumn('id', Types::INTEGER);
$tableDiff->fromTable->setPrimaryKey(['id']);
$tableDiff->renamedIndexes = [
'create' => new Index('select', ['id']),
'`foo`' => new Index('`bar`', ['id']),
];
self::assertSame(
$this->getQuotedAlterTableRenameIndexSQL(),
$this->platform->getAlterTableSQL($tableDiff),
);
}
/** @return string[] */
protected function getQuotedAlterTableRenameIndexSQL(): array
{
return [
'DROP INDEX "create"',
'CREATE INDEX "select" ON "table" (id)',
'DROP INDEX "foo"',
'CREATE INDEX "bar" ON "table" (id)',
];
}
public function testQuotesAlterTableRenameColumn(): void
{
$fromTable = new Table('mytable');
$fromTable->addColumn('unquoted1', Types::INTEGER, ['comment' => 'Unquoted 1']);
$fromTable->addColumn('unquoted2', Types::INTEGER, ['comment' => 'Unquoted 2']);
$fromTable->addColumn('unquoted3', Types::INTEGER, ['comment' => 'Unquoted 3']);
$fromTable->addColumn('create', Types::INTEGER, ['comment' => 'Reserved keyword 1']);
$fromTable->addColumn('table', Types::INTEGER, ['comment' => 'Reserved keyword 2']);
$fromTable->addColumn('select', Types::INTEGER, ['comment' => 'Reserved keyword 3']);
$fromTable->addColumn('`quoted1`', Types::INTEGER, ['comment' => 'Quoted 1']);
$fromTable->addColumn('`quoted2`', Types::INTEGER, ['comment' => 'Quoted 2']);
$fromTable->addColumn('`quoted3`', Types::INTEGER, ['comment' => 'Quoted 3']);
$toTable = new Table('mytable');
// unquoted -> unquoted
$toTable->addColumn('unquoted', Types::INTEGER, ['comment' => 'Unquoted 1']);
// unquoted -> reserved keyword
$toTable->addColumn('where', Types::INTEGER, ['comment' => 'Unquoted 2']);
// unquoted -> quoted
$toTable->addColumn('`foo`', Types::INTEGER, ['comment' => 'Unquoted 3']);
// reserved keyword -> unquoted
$toTable->addColumn('reserved_keyword', Types::INTEGER, ['comment' => 'Reserved keyword 1']);
// reserved keyword -> reserved keyword
$toTable->addColumn('from', Types::INTEGER, ['comment' => 'Reserved keyword 2']);
// reserved keyword -> quoted
$toTable->addColumn('`bar`', Types::INTEGER, ['comment' => 'Reserved keyword 3']);
// quoted -> unquoted
$toTable->addColumn('quoted', Types::INTEGER, ['comment' => 'Quoted 1']);
// quoted -> reserved keyword
$toTable->addColumn('and', Types::INTEGER, ['comment' => 'Quoted 2']);
// quoted -> quoted
$toTable->addColumn('`baz`', Types::INTEGER, ['comment' => 'Quoted 3']);
$diff = (new Comparator())->diffTable($fromTable, $toTable);
self::assertNotFalse($diff);
self::assertEquals(
$this->getQuotedAlterTableRenameColumnSQL(),
$this->platform->getAlterTableSQL($diff),
);
}
/**
* Returns SQL statements for {@link testQuotesAlterTableRenameColumn}.
*
* @return string[]
*/
abstract protected function getQuotedAlterTableRenameColumnSQL(): array;
public function testQuotesAlterTableChangeColumnLength(): void
{
$fromTable = new Table('mytable');
$fromTable->addColumn('unquoted1', Types::STRING, ['comment' => 'Unquoted 1', 'length' => 10]);
$fromTable->addColumn('unquoted2', Types::STRING, ['comment' => 'Unquoted 2', 'length' => 10]);
$fromTable->addColumn('unquoted3', Types::STRING, ['comment' => 'Unquoted 3', 'length' => 10]);
$fromTable->addColumn('create', Types::STRING, ['comment' => 'Reserved keyword 1', 'length' => 10]);
$fromTable->addColumn('table', Types::STRING, ['comment' => 'Reserved keyword 2', 'length' => 10]);
$fromTable->addColumn('select', Types::STRING, ['comment' => 'Reserved keyword 3', 'length' => 10]);
$toTable = new Table('mytable');
$toTable->addColumn('unquoted1', Types::STRING, ['comment' => 'Unquoted 1', 'length' => 255]);
$toTable->addColumn('unquoted2', Types::STRING, ['comment' => 'Unquoted 2', 'length' => 255]);
$toTable->addColumn('unquoted3', Types::STRING, ['comment' => 'Unquoted 3', 'length' => 255]);
$toTable->addColumn('create', Types::STRING, ['comment' => 'Reserved keyword 1', 'length' => 255]);
$toTable->addColumn('table', Types::STRING, ['comment' => 'Reserved keyword 2', 'length' => 255]);
$toTable->addColumn('select', Types::STRING, ['comment' => 'Reserved keyword 3', 'length' => 255]);
$diff = (new Comparator())->diffTable($fromTable, $toTable);
self::assertNotFalse($diff);
self::assertEquals(
$this->getQuotedAlterTableChangeColumnLengthSQL(),
$this->platform->getAlterTableSQL($diff),
);
}
/**
* Returns SQL statements for {@link testQuotesAlterTableChangeColumnLength}.
*
* @return string[]
*/
abstract protected function getQuotedAlterTableChangeColumnLengthSQL(): array;
public function testAlterTableRenameIndexInSchema(): void
{
$tableDiff = new TableDiff('myschema.mytable');
$tableDiff->fromTable = new Table('myschema.mytable');
$tableDiff->fromTable->addColumn('id', Types::INTEGER);
$tableDiff->fromTable->setPrimaryKey(['id']);
$tableDiff->renamedIndexes = [
'idx_foo' => new Index('idx_bar', ['id']),
];
self::assertSame(
$this->getAlterTableRenameIndexInSchemaSQL(),
$this->platform->getAlterTableSQL($tableDiff),
);
}