Skip to content

Commit a1d55ab

Browse files
committed
Rework more table/column tests
1 parent 09d248d commit a1d55ab

File tree

8 files changed

+292
-125
lines changed

8 files changed

+292
-125
lines changed

tests/Functional/SQL/Builder/CreateAndDropSchemaObjectsSQLBuilderTest.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
namespace Doctrine\DBAL\Tests\Functional\SQL\Builder;
66

77
use Doctrine\DBAL\Exception;
8-
use Doctrine\DBAL\Schema\AbstractSchemaManager;
98
use Doctrine\DBAL\Schema\Column;
9+
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
1010
use Doctrine\DBAL\Schema\Name\OptionallyQualifiedName;
1111
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1212
use Doctrine\DBAL\Schema\Schema;
1313
use Doctrine\DBAL\Schema\Table;
1414
use Doctrine\DBAL\Tests\FunctionalTestCase;
1515
use Doctrine\DBAL\Types\Types;
1616

17+
use function array_values;
18+
1719
class CreateAndDropSchemaObjectsSQLBuilderTest extends FunctionalTestCase
1820
{
1921
/** @throws Exception */
@@ -33,8 +35,13 @@ public function testCreateAndDropTablesWithCircularForeignKeys(): void
3335
self::assertTrue($schemaManager->tablesExist([$name1->toString()]));
3436
self::assertTrue($schemaManager->tablesExist([$name2->toString()]));
3537

36-
$this->introspectForeignKey($schemaManager, $name1, $name2);
37-
$this->introspectForeignKey($schemaManager, $name2, $name1);
38+
$table1 = $schemaManager->introspectTable('t1');
39+
$table2 = $schemaManager->introspectTable('t2');
40+
41+
$this->assertForeignKey($table1, $name2);
42+
$this->assertForeignKey($table2, $name1);
43+
44+
$schema = new Schema([$table1, $table2]);
3845

3946
$schemaManager->dropSchemaObjects($schema);
4047

@@ -46,7 +53,7 @@ private function createTable(
4653
OptionallyQualifiedName $name,
4754
OptionallyQualifiedName $otherName,
4855
): Table {
49-
$table = Table::editor()
56+
return Table::editor()
5057
->setName($name)
5158
->setColumns(
5259
Column::editor()
@@ -58,25 +65,25 @@ private function createTable(
5865
->setTypeName(Types::INTEGER)
5966
->create(),
6067
)
68+
->setForeignKeyConstraints(
69+
ForeignKeyConstraint::editor()
70+
->setUnquotedReferencingColumnNames('other_id')
71+
->setReferencedTableName($otherName)
72+
->setUnquotedReferencedColumnNames('id')
73+
->create(),
74+
)
6175
->setPrimaryKeyConstraint(
6276
PrimaryKeyConstraint::editor()
6377
->setUnquotedColumnNames('id')
6478
->create(),
6579
)
6680
->create();
67-
68-
$table->addForeignKeyConstraint($otherName->toString(), ['other_id'], ['id']);
69-
70-
return $table;
7181
}
7282

7383
/** @throws Exception */
74-
private function introspectForeignKey(
75-
AbstractSchemaManager $schemaManager,
76-
OptionallyQualifiedName $tableName,
77-
OptionallyQualifiedName $expectedReferencedTableName,
78-
): void {
79-
$foreignKeys = $schemaManager->listTableForeignKeys($tableName->toString());
84+
private function assertForeignKey(Table $table, OptionallyQualifiedName $expectedReferencedTableName): void
85+
{
86+
$foreignKeys = array_values($table->getForeignKeys());
8087
self::assertCount(1, $foreignKeys);
8188
$this->assertOptionallyQualifiedNameEquals(
8289
$expectedReferencedTableName,

tests/Functional/Schema/PostgreSQL/ComparatorTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Doctrine\DBAL\Schema\Table;
1313
use Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils;
1414
use Doctrine\DBAL\Tests\FunctionalTestCase;
15-
use Doctrine\DBAL\Types\Type;
1615
use Doctrine\DBAL\Types\Types;
1716

1817
final class ComparatorTest extends FunctionalTestCase
@@ -87,9 +86,11 @@ public function testPlatformOptionsChangedColumnComparison(): void
8786
)
8887
->create();
8988

90-
$onlineTable = clone $table;
91-
$table->getColumn('test')
92-
->setType(Type::getType(Types::JSONB));
89+
$onlineTable = $table->edit()
90+
->modifyColumnByUnquotedName('test', static function (ColumnEditor $editor): void {
91+
$editor->setTypeName(Types::JSONB);
92+
})
93+
->create();
9394

9495
$compareResult = $this->comparator->compareTables($onlineTable, $table);
9596
self::assertCount(1, $compareResult->getChangedColumns());

tests/Functional/Schema/PrimaryKeyConstraintTest.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
88
use Doctrine\DBAL\Platforms\SQLitePlatform;
99
use Doctrine\DBAL\Platforms\SQLServerPlatform;
10+
use Doctrine\DBAL\Schema\Column;
1011
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1112
use Doctrine\DBAL\Schema\Table;
1213
use Doctrine\DBAL\Tests\FunctionalTestCase;
@@ -34,9 +35,16 @@ public function testNameIntrospection(): void
3435
->setUnquotedColumnNames('id')
3536
->create();
3637

37-
$table = new Table('users');
38-
$table->addColumn('id', Types::INTEGER);
39-
$table->addPrimaryKeyConstraint($primaryKeyConstraint);
38+
$table = Table::editor()
39+
->setUnquotedName('users')
40+
->setColumns(
41+
Column::editor()
42+
->setUnquotedName('id')
43+
->setTypeName(Types::INTEGER)
44+
->create(),
45+
)
46+
->setPrimaryKeyConstraint($primaryKeyConstraint)
47+
->create();
4048

4149
$this->dropAndCreateTable($table);
4250

@@ -66,9 +74,16 @@ public function testIsClusteredIntrospection(bool $isClustered): void
6674
->setIsClustered($isClustered)
6775
->create();
6876

69-
$table = new Table('users');
70-
$table->addColumn('id', Types::INTEGER);
71-
$table->addPrimaryKeyConstraint($primaryKeyConstraint);
77+
$table = Table::editor()
78+
->setUnquotedName('users')
79+
->setColumns(
80+
Column::editor()
81+
->setUnquotedName('id')
82+
->setTypeName(Types::INTEGER)
83+
->create(),
84+
)
85+
->setPrimaryKeyConstraint($primaryKeyConstraint)
86+
->create();
7287

7388
$this->dropAndCreateTable($table);
7489

tests/Functional/Schema/SQLiteSchemaManagerTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ public function testListDatabases(): void
4343
$this->schemaManager->listDatabases();
4444
}
4545

46-
public function createListTableColumns(): Table
47-
{
48-
$table = parent::createListTableColumns();
49-
$table->getColumn('id')->setAutoincrement(true);
50-
51-
return $table;
52-
}
53-
5446
/** @throws Exception */
5547
public function testListForeignKeysFromExistingDatabase(): void
5648
{

tests/Functional/Schema/SchemaManagerTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,15 @@ public function testAutoIncrementColumnInCompositePrimaryKeyIntrospection(bool $
251251
/** @throws Exception */
252252
public function testIntrospectTableWithDotInName(): void
253253
{
254-
$table = new Table('"example.com"');
255-
$table->addColumn('id', Types::INTEGER);
254+
$table = Table::editor()
255+
->setQuotedName('example.com')
256+
->setColumns(
257+
Column::editor()
258+
->setUnquotedName('id')
259+
->setTypeName(Types::INTEGER)
260+
->create(),
261+
)
262+
->create();
256263

257264
$this->dropAndCreateTable($table);
258265

tests/Platforms/AbstractPlatformTestCase.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,14 +1075,21 @@ public function testNamedPrimaryKeyConstraintIsReportedAsUnsupported(): void
10751075
self::markTestSkipped('This current database platform supports named primary key constraints.');
10761076
}
10771077

1078-
$table = new Table('users');
1079-
$table->addColumn('id', Types::INTEGER);
1080-
$table->addPrimaryKeyConstraint(
1081-
PrimaryKeyConstraint::editor()
1082-
->setUnquotedName('users_pk')
1083-
->setUnquotedColumnNames('id')
1084-
->create(),
1085-
);
1078+
$table = Table::editor()
1079+
->setUnquotedName('users')
1080+
->setColumns(
1081+
Column::editor()
1082+
->setUnquotedName('id')
1083+
->setTypeName(Types::INTEGER)
1084+
->create(),
1085+
)
1086+
->setPrimaryKeyConstraint(
1087+
PrimaryKeyConstraint::editor()
1088+
->setUnquotedName('users_pk')
1089+
->setUnquotedColumnNames('id')
1090+
->create(),
1091+
)
1092+
->create();
10861093

10871094
$this->expectException(UnsupportedPrimaryKeyConstraintDefinition::class);
10881095
$this->platform->getCreateTableSQL($table);
@@ -1098,14 +1105,21 @@ public function testNonClusteredPrimaryKeyConstraintIsReportedAsUnsupported(): v
10981105
self::markTestSkipped('This current database platform supports non-clustered primary key constraints.');
10991106
}
11001107

1101-
$table = new Table('users');
1102-
$table->addColumn('id', Types::INTEGER);
1103-
$table->addPrimaryKeyConstraint(
1104-
PrimaryKeyConstraint::editor()
1105-
->setIsClustered(false)
1106-
->setUnquotedColumnNames('id')
1107-
->create(),
1108-
);
1108+
$table = Table::editor()
1109+
->setUnquotedName('users')
1110+
->setColumns(
1111+
Column::editor()
1112+
->setUnquotedName('id')
1113+
->setTypeName(Types::INTEGER)
1114+
->create(),
1115+
)
1116+
->setPrimaryKeyConstraint(
1117+
PrimaryKeyConstraint::editor()
1118+
->setIsClustered(false)
1119+
->setUnquotedColumnNames('id')
1120+
->create(),
1121+
)
1122+
->create();
11091123

11101124
$this->expectException(UnsupportedPrimaryKeyConstraintDefinition::class);
11111125
$this->platform->getCreateTableSQL($table);

tests/Schema/SchemaTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,10 @@ public function testAddObjectWithQualifiedNameAfterUnqualifiedNameWithDefaultNam
422422
$schemaConfig = new SchemaConfig();
423423
$schemaConfig->setName('public');
424424

425-
$schema = new Schema([new Table('t'), new Table('public.s')], [], $schemaConfig);
425+
$schema = new Schema([
426+
$this->createTable('t'),
427+
$this->createTable('s', 'public'),
428+
], [], $schemaConfig);
426429

427430
self::assertTrue($schema->hasTable('t'));
428431
self::assertTrue($schema->hasTable('public.s'));
@@ -433,7 +436,10 @@ public function testAddObjectWithUnqualifiedNameAfterQualifiedNameWithDefaultNam
433436
$schemaConfig = new SchemaConfig();
434437
$schemaConfig->setName('public');
435438

436-
$schema = new Schema([new Table('public.t'), new Table('s')], [], $schemaConfig);
439+
$schema = new Schema([
440+
$this->createTable('t', 'public'),
441+
$this->createTable('s'),
442+
], [], $schemaConfig);
437443

438444
self::assertTrue($schema->hasTable('public.t'));
439445
self::assertTrue($schema->hasTable('s'));

0 commit comments

Comments
 (0)