Completed
Pull Request — 2.10 (#3762)
by Benjamin
64:30
created

CommitOrderCalculatorTest::testCommitOrdering1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 29
rs 9.6333
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Internal;
4
5
use Doctrine\DBAL\Internal\CommitOrderCalculator;
6
use Doctrine\DBAL\Schema\Table;
7
use Doctrine\Tests\DbalTestCase;
8
9
/**
10
 * Tests of the commit order calculation.
11
 *
12
 * IMPORTANT: When writing tests here consider that a lot of graph constellations
13
 * can have many valid orderings, so you may want to build a graph that has only
14
 * 1 valid order to simplify your tests.
15
 */
16
class CommitOrderCalculatorTest extends DbalTestCase
17
{
18
    /** @var CommitOrderCalculator */
19
    private $calculator;
20
21
    protected function setUp() : void
22
    {
23
        $this->calculator = new CommitOrderCalculator();
24
    }
25
26
    public function testCommitOrdering1() : void
27
    {
28
        $table1 = new Table('table1');
29
        $table2 = new Table('table2');
30
        $table3 = new Table('table3');
31
        $table4 = new Table('table4');
32
        $table5 = new Table('table5');
33
34
        $this->assertFalse($this->calculator->hasNode($table1->getName()));
35
36
        $this->calculator->addNode($table1->getName(), $table1);
37
        $this->calculator->addNode($table2->getName(), $table2);
38
        $this->calculator->addNode($table3->getName(), $table3);
39
        $this->calculator->addNode($table4->getName(), $table4);
40
        $this->calculator->addNode($table5->getName(), $table5);
41
42
        $this->assertTrue($this->calculator->hasNode($table1->getName()));
43
44
        $this->calculator->addDependency($table1->getName(), $table2->getName(), 1);
45
        $this->calculator->addDependency($table2->getName(), $table3->getName(), 1);
46
        $this->calculator->addDependency($table3->getName(), $table4->getName(), 1);
47
        $this->calculator->addDependency($table5->getName(), $table1->getName(), 1);
48
49
        $sorted = $this->calculator->sort();
50
51
        // There is only 1 valid ordering for this constellation
52
        $correctOrder = [$table5, $table1, $table2, $table3, $table4];
53
54
        $this->assertSame($correctOrder, $sorted);
55
    }
56
57
    public function testCommitOrdering2() : void
58
    {
59
        $table1 = new Table('table1');
60
        $table2 = new Table('table2');
61
62
        $this->calculator->addNode($table1->getName(), $table1);
63
        $this->calculator->addNode($table2->getName(), $table2);
64
65
        $this->calculator->addDependency($table1->getName(), $table2->getName(), 0);
66
        $this->calculator->addDependency($table2->getName(), $table1->getName(), 1);
67
68
        $sorted = $this->calculator->sort();
69
70
        // There is only 1 valid ordering for this constellation
71
        $correctOrder = [$table2, $table1];
72
73
        $this->assertSame($correctOrder, $sorted);
74
    }
75
76
    public function testCommitOrdering3() : void
77
    {
78
        // this test corresponds to the GH7259Test::testPersistFileBeforeVersion functional test
79
        $table1 = new Table('table1');
80
        $table2 = new Table('table2');
81
        $table3 = new Table('table3');
82
        $table4 = new Table('table4');
83
84
        $this->calculator->addNode($table1->getName(), $table1);
85
        $this->calculator->addNode($table2->getName(), $table2);
86
        $this->calculator->addNode($table3->getName(), $table3);
87
        $this->calculator->addNode($table4->getName(), $table4);
88
89
        $this->calculator->addDependency($table4->getName(), $table1->getName(), 1);
90
        $this->calculator->addDependency($table1->getName(), $table2->getName(), 1);
91
        $this->calculator->addDependency($table4->getName(), $table3->getName(), 1);
92
        $this->calculator->addDependency($table1->getName(), $table4->getName(), 0);
93
94
        $sorted = $this->calculator->sort();
95
96
        // There is only multiple valid ordering for this constellation, but
97
        // the class4, class1, class2 ordering is important to break the cycle
98
        // on the nullable link.
99
        $correctOrders = [
100
            [$table4, $table1, $table2, $table3],
101
            [$table4, $table1, $table3, $table2],
102
            [$table4, $table3, $table1, $table2],
103
        ];
104
105
        // We want to perform a strict comparison of the array
106
        $this->assertContains($sorted, $correctOrders, '');
107
    }
108
}
109