/**
  * Tests whether the declaration of dependencies directly on the nodes works correctly
  * and the dependencies are detected by the graph.
  *
  * @test
  */
 public function testDeclarationOnNodes()
 {
     $graph = new DependencyGraph();
     $nodeA = new DependencyNode('A');
     $nodeB = new DependencyNode('B');
     $nodeC = new DependencyNode('C');
     $nodeD = new DependencyNode('D');
     $nodeE = new DependencyNode('E');
     $nodeA->dependsOn($nodeB);
     $nodeA->dependsOn($nodeD);
     $nodeB->dependsOn($nodeC);
     $nodeB->dependsOn($nodeE);
     $nodeC->dependsOn($nodeD);
     $nodeC->dependsOn($nodeE);
     $graph->addNode($nodeA);
     $dependencies = $graph->getDependencies();
     $this->assertEquals(array($nodeB, $nodeD), $dependencies[$nodeA]->getArrayCopy());
     $this->assertEquals(array($nodeC, $nodeE), $dependencies[$nodeB]->getArrayCopy());
     $this->assertEquals(array($nodeD, $nodeE), $dependencies[$nodeC]->getArrayCopy());
     $this->assertEquals(array(), $dependencies[$nodeD]->getArrayCopy());
     $this->assertEquals(array(), $dependencies[$nodeE]->getArrayCopy());
 }
 /**
  * Brings the table into an order which respects dependencies between them.
  *
  * @param Table[] $tables
  *
  * @return Table[]
  */
 public function sortTablesByDependencies(array $tables)
 {
     $nodes = array();
     $dependencyGraph = new DependencyGraph();
     // create nodes for each table
     foreach ($tables as $table) {
         $node = new DependencyNode($table, $table->getName());
         $nodes[$table->getName()] = $node;
         $dependencyGraph->addNode($node);
     }
     foreach ($tables as $table) {
         foreach ($table->getForeignKeys() as $foreignKey) {
             // TODO foreign keys pointing to own table not supported yet
             if ($foreignKey->getForeignTableName() == $table->getName()) {
                 continue;
             }
             $dependencyGraph->addDependency($nodes[$table->getName()], $nodes[$foreignKey->getForeignTableName()]);
         }
     }
     // TODO why the filter?
     return array_filter($dependencyGraph->resolve());
 }