@@ -333,5 +333,83 @@ public function testAmbiguityExceptionWithNoJointure() {
333333 $ this ->assertTrue ($ exceptionTriggered );
334334 }
335335
336+ public function testAmbiguityExceptionWithNoJointureAndModifiedWeight () {
337+ $ schema = $ this ->getBaseSchema ();
338+ $ right = $ schema ->getTable ("right " );
339+ $ right ->addColumn ("role_id " , "integer " , array ("unsigned " => true ));
340+ $ right ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ("role_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
341+
342+ $ right ->addColumn ("role_id2 " , "integer " , array ("unsigned " => true ));
343+ $ right ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ("role_id2 " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
344+
345+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ($ schema ));
346+ $ schemaAnalyzer ->setForeignKeyCost ('right ' , 'role_id ' , SchemaAnalyzer::WEIGHT_IMPORTANT );
347+
348+ $ fks = $ schemaAnalyzer ->getShortestPath ("role " , "right " );
349+
350+ $ this ->assertCount (1 , $ fks );
351+ $ this ->assertEquals ("right " , $ fks [0 ]->getLocalTable ()->getName ());
352+ $ this ->assertEquals ("role " , $ fks [0 ]->getForeignTableName ());
353+ }
354+
355+ public function testAmbiguityExceptionWithJointureAndModifiedWeight () {
356+ $ schema = $ this ->getBaseSchema ();
357+
358+ $ role_right = $ schema ->createTable ("role_right " );
359+ $ role_right ->addColumn ("role_id " , "integer " , array ("unsigned " => true ));
360+ $ role_right ->addColumn ("right_id " , "integer " , array ("unsigned " => true ));
361+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ("role_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
362+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
363+ $ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
364+
365+ $ role_right2 = $ schema ->createTable ("role_right2 " );
366+ $ role_right2 ->addColumn ("role_id " , "integer " , array ("unsigned " => true ));
367+ $ role_right2 ->addColumn ("right_id " , "integer " , array ("unsigned " => true ));
368+ $ role_right2 ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ("role_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
369+ $ role_right2 ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
370+ $ role_right2 ->setPrimaryKey (["role_id " , "right_id " ]);
371+
372+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ($ schema ));
373+ $ schemaAnalyzer ->setTableCostModifier ("role_right2 " , SchemaAnalyzer::WEIGHT_IRRELEVANT );
374+
375+ $ fks = $ schemaAnalyzer ->getShortestPath ("role " , "right " );
376+
377+ $ this ->assertCount (2 , $ fks );
378+ $ this ->assertEquals ("role_right " , $ fks [0 ]->getLocalTable ()->getName ());
379+ $ this ->assertEquals ("role " , $ fks [0 ]->getForeignTableName ());
380+ $ this ->assertEquals ("role_right " , $ fks [1 ]->getLocalTable ()->getName ());
381+ $ this ->assertEquals ("right " , $ fks [1 ]->getForeignTableName ());
382+ }
383+
384+ public function testAmbiguityExceptionWithJointureAndModifiedWeight2 () {
385+ $ schema = $ this ->getBaseSchema ();
386+
387+ $ role_right = $ schema ->createTable ("role_right " );
388+ $ role_right ->addColumn ("role_id " , "integer " , array ("unsigned " => true ));
389+ $ role_right ->addColumn ("right_id " , "integer " , array ("unsigned " => true ));
390+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ("role_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
391+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
392+ $ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
393+
394+ $ role_right2 = $ schema ->createTable ("role_right2 " );
395+ $ role_right2 ->addColumn ("role_id " , "integer " , array ("unsigned " => true ));
396+ $ role_right2 ->addColumn ("right_id " , "integer " , array ("unsigned " => true ));
397+ $ role_right2 ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ("role_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
398+ $ role_right2 ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
399+ $ role_right2 ->setPrimaryKey (["role_id " , "right_id " ]);
400+
401+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ($ schema ));
402+ $ schemaAnalyzer ->setTableCostModifiers (["role_right2 " => SchemaAnalyzer::WEIGHT_IRRELEVANT ]);
403+ $ schemaAnalyzer ->setForeignKeyCosts ([]);
404+
405+ $ fks = $ schemaAnalyzer ->getShortestPath ("role " , "right " );
406+
407+ $ this ->assertCount (2 , $ fks );
408+ $ this ->assertEquals ("role_right " , $ fks [0 ]->getLocalTable ()->getName ());
409+ $ this ->assertEquals ("role " , $ fks [0 ]->getForeignTableName ());
410+ $ this ->assertEquals ("role_right " , $ fks [1 ]->getLocalTable ()->getName ());
411+ $ this ->assertEquals ("right " , $ fks [1 ]->getForeignTableName ());
412+ }
413+
336414}
337415
0 commit comments