Skip to content

Commit e7837c9

Browse files
committed
Convert some test to integration tests
1 parent 98cf3a2 commit e7837c9

File tree

2 files changed

+223
-166
lines changed

2 files changed

+223
-166
lines changed

Diff for: tests/Functional/Schema/AlterTableTest.php

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Functional\Schema;
6+
7+
use Doctrine\DBAL\Exception;
8+
use Doctrine\DBAL\Platforms\DB2Platform;
9+
use Doctrine\DBAL\Platforms\MySQLPlatform;
10+
use Doctrine\DBAL\Platforms\OraclePlatform;
11+
use Doctrine\DBAL\Platforms\SQLitePlatform;
12+
use Doctrine\DBAL\Platforms\SQLServerPlatform;
13+
use Doctrine\DBAL\Schema\Table;
14+
use Doctrine\DBAL\Tests\FunctionalTestCase;
15+
use Doctrine\DBAL\Types\Types;
16+
17+
class AlterTableTest extends FunctionalTestCase
18+
{
19+
/** @throws Exception */
20+
public function testAddPrimaryKeyOnExistingColumn(): void
21+
{
22+
if ($this->connection->getDatabasePlatform() instanceof SQLitePlatform) {
23+
self::markTestSkipped(
24+
'SQLite will automatically set up auto-increment behavior on the primary key column, which this test'
25+
. ' does not expect.',
26+
);
27+
}
28+
29+
$table = new Table('alter_pk');
30+
$table->addColumn('id', Types::INTEGER);
31+
$table->addColumn('val', Types::INTEGER);
32+
33+
$this->testMigration($table, static function (Table $table): void {
34+
$table->setPrimaryKey(['id']);
35+
});
36+
}
37+
38+
/** @throws Exception */
39+
public function testAddPrimaryKeyOnNewAutoIncrementColumn(): void
40+
{
41+
$table = new Table('alter_pk');
42+
$table->addColumn('val', Types::INTEGER);
43+
44+
$this->testMigration($table, static function (Table $table): void {
45+
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
46+
$table->setPrimaryKey(['id']);
47+
});
48+
}
49+
50+
/** @throws Exception */
51+
public function testAlterPrimaryKeyFromAutoincrementToNonAutoincrementColumn(): void
52+
{
53+
$platform = $this->connection->getDatabasePlatform();
54+
55+
if ($platform instanceof MySqlPlatform) {
56+
self::markTestIncomplete(
57+
'DBAL should not allow this migration on MySQL because an auto-increment column must be part of the'
58+
. ' primary key constraint.',
59+
);
60+
}
61+
62+
if ($platform instanceof SQLitePlatform) {
63+
self::markTestIncomplete(
64+
'DBAL should not allow this migration on MySQL because an auto-increment column must be part of the'
65+
. ' primary key constraint.',
66+
);
67+
}
68+
69+
$this->ensureDroppingPrimaryKeyConstraintIsSupported();
70+
71+
$table = new Table('alter_pk');
72+
$table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]);
73+
$table->addColumn('id2', Types::INTEGER);
74+
$table->setPrimaryKey(['id1']);
75+
76+
$this->testMigration($table, static function (Table $table): void {
77+
$table->dropPrimaryKey();
78+
$table->setPrimaryKey(['id2']);
79+
});
80+
}
81+
82+
/** @throws Exception */
83+
public function testDropPrimaryKeyWithAutoincrementColumn(): void
84+
{
85+
$platform = $this->connection->getDatabasePlatform();
86+
87+
if ($platform instanceof MySqlPlatform) {
88+
self::markTestIncomplete(
89+
'DBAL should not allow this migration on MySQL because an auto-increment column must be part of the'
90+
. ' primary key constraint.',
91+
);
92+
}
93+
94+
if ($platform instanceof SQLitePlatform) {
95+
self::markTestSkipped(
96+
'SQLite does not support auto-increment columns as part of composite primary key constraint',
97+
);
98+
}
99+
100+
$this->ensureDroppingPrimaryKeyConstraintIsSupported();
101+
102+
$table = new Table('alter_pk');
103+
$table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]);
104+
$table->addColumn('id2', Types::INTEGER);
105+
$table->setPrimaryKey(['id1', 'id2']);
106+
107+
$this->testMigration($table, static function (Table $table): void {
108+
$table->dropPrimaryKey();
109+
});
110+
}
111+
112+
/** @throws Exception */
113+
public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn(): void
114+
{
115+
if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
116+
self::markTestIncomplete(
117+
'DBAL does not restore the auto-increment attribute after dropping and adding the constraint,'
118+
. ' which is a bug.',
119+
);
120+
}
121+
122+
$this->ensureDroppingPrimaryKeyConstraintIsSupported();
123+
124+
$table = new Table('alter_pk');
125+
$table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]);
126+
$table->addColumn('id2', Types::INTEGER);
127+
$table->setPrimaryKey(['id1', 'id2']);
128+
129+
$this->testMigration($table, static function (Table $table): void {
130+
$table->dropPrimaryKey();
131+
$table->setPrimaryKey(['id1']);
132+
});
133+
}
134+
135+
/** @throws Exception */
136+
public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn(): void
137+
{
138+
$platform = $this->connection->getDatabasePlatform();
139+
140+
if ($platform instanceof MySqlPlatform) {
141+
self::markTestIncomplete(
142+
'DBAL does not restore the auto-increment attribute after dropping and adding the constraint,'
143+
. ' which is a bug.',
144+
);
145+
}
146+
147+
if ($platform instanceof SQLitePlatform) {
148+
self::markTestSkipped(
149+
'SQLite does not support auto-increment columns as part of composite primary key constraint',
150+
);
151+
}
152+
153+
$this->ensureDroppingPrimaryKeyConstraintIsSupported();
154+
155+
$table = new Table('alter_pk');
156+
$table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]);
157+
$table->addColumn('id2', Types::INTEGER);
158+
$table->setPrimaryKey(['id1']);
159+
160+
$this->testMigration($table, static function (Table $table): void {
161+
$table->dropPrimaryKey();
162+
$table->setPrimaryKey(['id1', 'id2']);
163+
});
164+
}
165+
166+
/** @throws Exception */
167+
public function testAddNewColumnToPrimaryKey(): void
168+
{
169+
$this->ensureDroppingPrimaryKeyConstraintIsSupported();
170+
171+
$table = new Table('alter_pk');
172+
$table->addColumn('id1', Types::INTEGER);
173+
$table->setPrimaryKey(['id1']);
174+
175+
$this->testMigration($table, static function (Table $table): void {
176+
$table->addColumn('id2', Types::INTEGER);
177+
$table->dropPrimaryKey();
178+
$table->setPrimaryKey(['id1', 'id2']);
179+
});
180+
}
181+
182+
/** @throws Exception */
183+
private function ensureDroppingPrimaryKeyConstraintIsSupported(): void
184+
{
185+
$platform = $this->connection->getDatabasePlatform();
186+
187+
if (
188+
! ($platform instanceof DB2Platform)
189+
&& ! ($platform instanceof OraclePlatform)
190+
&& ! ($platform instanceof SQLServerPlatform)
191+
) {
192+
return;
193+
}
194+
195+
self::markTestIncomplete(
196+
'Dropping primary key constraint on the currently used database platform is not implemented.',
197+
);
198+
}
199+
200+
/** @throws Exception */
201+
private function testMigration(Table $oldTable, callable $migration): void
202+
{
203+
$this->dropAndCreateTable($oldTable);
204+
205+
$newTable = clone $oldTable;
206+
207+
$migration($newTable);
208+
209+
$schemaManager = $this->connection->createSchemaManager();
210+
211+
$diff = $schemaManager->createComparator()
212+
->compareTables($oldTable, $newTable);
213+
214+
$schemaManager->alterTable($diff);
215+
216+
$introspectedTable = $schemaManager->introspectTable($newTable->getName());
217+
218+
$diff = $schemaManager->createComparator()
219+
->compareTables($newTable, $introspectedTable);
220+
221+
self::assertTrue($diff->isEmpty());
222+
}
223+
}

Diff for: tests/Platforms/AbstractMySQLPlatformTestCase.php

-166
Original file line numberDiff line numberDiff line change
@@ -282,172 +282,6 @@ public function testBlobTypeDeclarationSQL(): void
282282
self::assertEquals('LONGBLOB', $this->platform->getBlobTypeDeclarationSQL([]));
283283
}
284284

285-
public function testAlterTableAddPrimaryKey(): void
286-
{
287-
$table = new Table('alter_table_add_pk');
288-
$table->addColumn('id', Types::INTEGER);
289-
$table->addColumn('foo', Types::INTEGER);
290-
$table->addIndex(['id'], 'idx_id');
291-
292-
$diffTable = clone $table;
293-
294-
$diffTable->dropIndex('idx_id');
295-
$diffTable->setPrimaryKey(['id']);
296-
297-
$diff = $this->createComparator()
298-
->compareTables($table, $diffTable);
299-
300-
self::assertEquals(
301-
['DROP INDEX idx_id ON alter_table_add_pk', 'ALTER TABLE alter_table_add_pk ADD PRIMARY KEY (id)'],
302-
$this->platform->getAlterTableSQL($diff),
303-
);
304-
}
305-
306-
public function testAlterPrimaryKeyWithAutoincrementColumn(): void
307-
{
308-
$table = new Table('alter_primary_key');
309-
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
310-
$table->addColumn('foo', Types::INTEGER);
311-
$table->setPrimaryKey(['id']);
312-
313-
$diffTable = clone $table;
314-
315-
$diffTable->dropPrimaryKey();
316-
$diffTable->setPrimaryKey(['foo']);
317-
318-
$diff = $this->createComparator()
319-
->compareTables($table, $diffTable);
320-
321-
self::assertEquals(
322-
[
323-
'ALTER TABLE alter_primary_key MODIFY id INT NOT NULL',
324-
'DROP INDEX `primary` ON alter_primary_key',
325-
'ALTER TABLE alter_primary_key ADD PRIMARY KEY (foo)',
326-
],
327-
$this->platform->getAlterTableSQL($diff),
328-
);
329-
}
330-
331-
public function testDropPrimaryKeyWithAutoincrementColumn(): void
332-
{
333-
$table = new Table('drop_primary_key');
334-
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
335-
$table->addColumn('foo', Types::INTEGER);
336-
$table->addColumn('bar', Types::INTEGER);
337-
$table->setPrimaryKey(['id', 'foo']);
338-
339-
$diffTable = clone $table;
340-
341-
$diffTable->dropPrimaryKey();
342-
343-
$diff = $this->createComparator()
344-
->compareTables($table, $diffTable);
345-
346-
self::assertEquals(
347-
[
348-
'ALTER TABLE drop_primary_key MODIFY id INT NOT NULL',
349-
'DROP INDEX `primary` ON drop_primary_key',
350-
],
351-
$this->platform->getAlterTableSQL($diff),
352-
);
353-
}
354-
355-
public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn(): void
356-
{
357-
$table = new Table('tbl');
358-
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
359-
$table->addColumn('foo', Types::INTEGER);
360-
$table->addColumn('bar', Types::INTEGER);
361-
$table->setPrimaryKey(['id', 'foo']);
362-
363-
$diffTable = clone $table;
364-
365-
$diffTable->dropPrimaryKey();
366-
$diffTable->setPrimaryKey(['id']);
367-
368-
$diff = $this->createComparator()
369-
->compareTables($table, $diffTable);
370-
371-
self::assertSame(
372-
[
373-
'ALTER TABLE tbl MODIFY id INT NOT NULL',
374-
'DROP INDEX `primary` ON tbl',
375-
'ALTER TABLE tbl ADD PRIMARY KEY (id)',
376-
],
377-
$this->platform->getAlterTableSQL($diff),
378-
);
379-
}
380-
381-
public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn(): void
382-
{
383-
$table = new Table('tbl');
384-
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
385-
$table->addColumn('foo', Types::INTEGER);
386-
$table->addColumn('bar', Types::INTEGER);
387-
$table->setPrimaryKey(['id']);
388-
389-
$diffTable = clone $table;
390-
391-
$diffTable->dropPrimaryKey();
392-
$diffTable->setPrimaryKey(['id', 'foo']);
393-
394-
$diff = $this->createComparator()
395-
->compareTables($table, $diffTable);
396-
397-
self::assertSame(
398-
[
399-
'ALTER TABLE tbl MODIFY id INT NOT NULL',
400-
'DROP INDEX `primary` ON tbl',
401-
'ALTER TABLE tbl ADD PRIMARY KEY (id, foo)',
402-
],
403-
$this->platform->getAlterTableSQL($diff),
404-
);
405-
}
406-
407-
public function testAddAutoIncrementPrimaryKey(): void
408-
{
409-
$keyTable = new Table('foo');
410-
$keyTable->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
411-
$keyTable->addColumn('baz', Types::STRING, ['length' => 32]);
412-
$keyTable->setPrimaryKey(['id']);
413-
414-
$oldTable = new Table('foo');
415-
$oldTable->addColumn('baz', Types::STRING, ['length' => 32]);
416-
417-
$diff = $this->createComparator()
418-
->compareTables($oldTable, $keyTable);
419-
420-
$sql = $this->platform->getAlterTableSQL($diff);
421-
422-
self::assertEquals(['ALTER TABLE foo ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)'], $sql);
423-
}
424-
425-
public function testAlterPrimaryKeyWithNewColumn(): void
426-
{
427-
$table = new Table('yolo');
428-
$table->addColumn('pkc1', Types::INTEGER);
429-
$table->addColumn('col_a', Types::INTEGER);
430-
$table->setPrimaryKey(['pkc1']);
431-
432-
$diffTable = clone $table;
433-
434-
$diffTable->addColumn('pkc2', Types::INTEGER);
435-
$diffTable->dropPrimaryKey();
436-
$diffTable->setPrimaryKey(['pkc1', 'pkc2']);
437-
438-
$diff = $this->createComparator()
439-
->compareTables($table, $diffTable);
440-
441-
self::assertSame(
442-
[
443-
'DROP INDEX `primary` ON yolo',
444-
'ALTER TABLE yolo ADD pkc2 INT NOT NULL',
445-
'ALTER TABLE yolo ADD PRIMARY KEY (pkc1, pkc2)',
446-
],
447-
$this->platform->getAlterTableSQL($diff),
448-
);
449-
}
450-
451285
public function testInitializesDoctrineTypeMappings(): void
452286
{
453287
self::assertTrue($this->platform->hasDoctrineTypeMappingFor('binary'));

0 commit comments

Comments
 (0)