55namespace Doctrine \DBAL \Tests \Functional \SQL \Builder ;
66
77use Doctrine \DBAL \Exception ;
8- use Doctrine \DBAL \Schema \AbstractSchemaManager ;
98use Doctrine \DBAL \Schema \Column ;
9+ use Doctrine \DBAL \Schema \ForeignKeyConstraint ;
1010use Doctrine \DBAL \Schema \Name \OptionallyQualifiedName ;
1111use Doctrine \DBAL \Schema \PrimaryKeyConstraint ;
1212use Doctrine \DBAL \Schema \Schema ;
1313use Doctrine \DBAL \Schema \Table ;
1414use Doctrine \DBAL \Tests \FunctionalTestCase ;
1515use Doctrine \DBAL \Types \Types ;
1616
17+ use function array_values ;
18+
1719class 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 ,
0 commit comments