1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Tests\Internal; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Internal\DependencyOrderCalculator; |
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Tests of the commit order calculation. |
13
|
|
|
* |
14
|
|
|
* IMPORTANT: When writing tests here consider that a lot of graph constellations |
15
|
|
|
* can have many valid orderings, so you may want to build a graph that has only |
16
|
|
|
* 1 valid order to simplify your tests. |
17
|
|
|
*/ |
18
|
|
|
class DependencyOrderCalculatorTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** @var DependencyOrderCalculator */ |
21
|
|
|
private $calculator; |
22
|
|
|
|
23
|
|
|
protected function setUp() : void |
24
|
|
|
{ |
25
|
|
|
$this->calculator = new DependencyOrderCalculator(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testCommitOrdering1() : void |
29
|
|
|
{ |
30
|
|
|
$table1 = new Table('table1'); |
31
|
|
|
$table2 = new Table('table2'); |
32
|
|
|
$table3 = new Table('table3'); |
33
|
|
|
$table4 = new Table('table4'); |
34
|
|
|
$table5 = new Table('table5'); |
35
|
|
|
|
36
|
|
|
self::assertFalse($this->calculator->hasNode($table1->getName())); |
37
|
|
|
|
38
|
|
|
$this->calculator->addNode($table1->getName(), $table1); |
39
|
|
|
$this->calculator->addNode($table2->getName(), $table2); |
40
|
|
|
$this->calculator->addNode($table3->getName(), $table3); |
41
|
|
|
$this->calculator->addNode($table4->getName(), $table4); |
42
|
|
|
$this->calculator->addNode($table5->getName(), $table5); |
43
|
|
|
|
44
|
|
|
self::assertTrue($this->calculator->hasNode($table1->getName())); |
45
|
|
|
|
46
|
|
|
$this->calculator->addDependency($table1->getName(), $table2->getName()); |
47
|
|
|
$this->calculator->addDependency($table2->getName(), $table3->getName()); |
48
|
|
|
$this->calculator->addDependency($table3->getName(), $table4->getName()); |
49
|
|
|
$this->calculator->addDependency($table5->getName(), $table1->getName()); |
50
|
|
|
|
51
|
|
|
$sorted = $this->calculator->sort(); |
52
|
|
|
|
53
|
|
|
// There is only 1 valid ordering for this constellation |
54
|
|
|
$correctOrder = [$table5, $table1, $table2, $table3, $table4]; |
55
|
|
|
|
56
|
|
|
self::assertSame($correctOrder, $sorted); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|