@@ -510,6 +510,46 @@ public function testInheritanceRelationship()
510510 $ this ->assertEquals ([], $ schemaAnalyzer ->getChildrenRelationships ('user ' ));
511511 }
512512
513+ public function testChainedJunctionTables ()
514+ {
515+ $ schema = $ this ->getBaseSchema ();
516+
517+ $ role_right = $ schema ->createTable ('role_right ' );
518+ $ role_right ->addColumn ('role_id ' , 'integer ' , array ('unsigned ' => true ));
519+ $ role_right ->addColumn ('right_id ' , 'integer ' , array ('unsigned ' => true ));
520+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ('role_id ' ), array ('id ' ), array ('onUpdate ' => 'CASCADE ' ), 'right_roles ' );
521+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ('right_id ' ), array ('id ' ), array ('onUpdate ' => 'CASCADE ' ), 'role_rights ' );
522+ $ role_right ->setPrimaryKey (['role_id ' , 'right_id ' ]);
523+
524+ $ user = $ schema ->createTable ('user ' );
525+ $ user ->addColumn ('id ' , 'integer ' , array ('unsigned ' => true ));
526+ $ user ->addColumn ('name ' , 'string ' , array ('length ' => 32 ));
527+ $ user ->setPrimaryKey (['id ' ]);
528+
529+ $ user_role = $ schema ->createTable ('user_role ' );
530+ $ user_role ->addColumn ('user_id ' , 'integer ' , array ('unsigned ' => true ));
531+ $ user_role ->addColumn ('role_id ' , 'integer ' , array ('unsigned ' => true ));
532+ $ user_role ->addForeignKeyConstraint ($ schema ->getTable ('user ' ), array ('user_id ' ), array ('id ' ), array ('onUpdate ' => 'CASCADE ' ), 'role_users ' );
533+ $ user_role ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ('role_id ' ), array ('id ' ), array ('onUpdate ' => 'CASCADE ' ), 'user_roles ' );
534+ $ user_role ->setPrimaryKey (['user_id ' , 'role_id ' ]);
535+
536+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ($ schema ));
537+
538+ $ fks = $ schemaAnalyzer ->getShortestPath ('user ' , 'right ' );
539+ $ this ->assertCount (4 , $ fks );
540+ $ this ->assertEquals ('role_users ' , $ fks [0 ]->getName ());
541+ $ this ->assertEquals ('user_roles ' , $ fks [1 ]->getName ());
542+ $ this ->assertEquals ('right_roles ' , $ fks [2 ]->getName ());
543+ $ this ->assertEquals ('role_rights ' , $ fks [3 ]->getName ());
544+
545+ $ fks = $ schemaAnalyzer ->getShortestPath ('right ' , 'user ' );
546+ $ this ->assertCount (4 , $ fks );
547+ $ this ->assertEquals ('role_rights ' , $ fks [0 ]->getName ());
548+ $ this ->assertEquals ('right_roles ' , $ fks [1 ]->getName ());
549+ $ this ->assertEquals ('user_roles ' , $ fks [2 ]->getName ());
550+ $ this ->assertEquals ('role_users ' , $ fks [3 ]->getName ());
551+ }
552+
513553 /**
514554 * @expectedException \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException
515555 * @expectedExceptionMessage Could not find table 'rights'. Did you mean 'right'?
0 commit comments