1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Internal\CommitOrderCalculator; |
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
9
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataBuildingContext; |
10
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
11
|
|
|
use Doctrine\ORM\Reflection\RuntimeReflectionService; |
12
|
|
|
use Doctrine\Tests\OrmTestCase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Tests of the commit order calculation. |
16
|
|
|
* |
17
|
|
|
* IMPORTANT: When writing tests here consider that a lot of graph constellations |
18
|
|
|
* can have many valid orderings, so you may want to build a graph that has only |
19
|
|
|
* 1 valid order to simplify your tests. |
20
|
|
|
*/ |
21
|
|
|
class CommitOrderCalculatorTest extends OrmTestCase |
22
|
|
|
{ |
23
|
|
|
/** @var CommitOrderCalculator */ |
24
|
|
|
private $calc; |
25
|
|
|
|
26
|
|
|
/** @var ClassMetadataBuildingContext|\PHPUnit_Framework_MockObject_MockObject */ |
27
|
|
|
private $metadataBuildingContext; |
28
|
|
|
|
29
|
|
|
protected function setUp() : void |
30
|
|
|
{ |
31
|
|
|
$this->calc = new CommitOrderCalculator(); |
32
|
|
|
$this->metadataBuildingContext = new ClassMetadataBuildingContext( |
33
|
|
|
$this->createMock(ClassMetadataFactory::class), |
34
|
|
|
new RuntimeReflectionService() |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testCommitOrdering1() : void |
39
|
|
|
{ |
40
|
|
|
$class1 = new ClassMetadata(NodeClass1::class, $this->metadataBuildingContext); |
41
|
|
|
$class2 = new ClassMetadata(NodeClass2::class, $this->metadataBuildingContext); |
42
|
|
|
$class3 = new ClassMetadata(NodeClass3::class, $this->metadataBuildingContext); |
43
|
|
|
$class4 = new ClassMetadata(NodeClass4::class, $this->metadataBuildingContext); |
44
|
|
|
$class5 = new ClassMetadata(NodeClass5::class, $this->metadataBuildingContext); |
45
|
|
|
|
46
|
|
|
$this->calc->addNode($class1->getClassName(), $class1); |
47
|
|
|
$this->calc->addNode($class2->getClassName(), $class2); |
48
|
|
|
$this->calc->addNode($class3->getClassName(), $class3); |
49
|
|
|
$this->calc->addNode($class4->getClassName(), $class4); |
50
|
|
|
$this->calc->addNode($class5->getClassName(), $class5); |
51
|
|
|
|
52
|
|
|
$this->calc->addDependency($class1->getClassName(), $class2->getClassName(), 1); |
53
|
|
|
$this->calc->addDependency($class2->getClassName(), $class3->getClassName(), 1); |
54
|
|
|
$this->calc->addDependency($class3->getClassName(), $class4->getClassName(), 1); |
55
|
|
|
$this->calc->addDependency($class5->getClassName(), $class1->getClassName(), 1); |
56
|
|
|
|
57
|
|
|
$sorted = $this->calc->sort(); |
58
|
|
|
|
59
|
|
|
// There is only 1 valid ordering for this constellation |
60
|
|
|
$correctOrder = [$class5, $class1, $class2, $class3, $class4]; |
61
|
|
|
|
62
|
|
|
self::assertSame($correctOrder, $sorted); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testCommitOrdering2() : void |
66
|
|
|
{ |
67
|
|
|
$class1 = new ClassMetadata(NodeClass1::class, $this->metadataBuildingContext); |
68
|
|
|
$class2 = new ClassMetadata(NodeClass2::class, $this->metadataBuildingContext); |
69
|
|
|
|
70
|
|
|
$this->calc->addNode($class1->getClassName(), $class1); |
71
|
|
|
$this->calc->addNode($class2->getClassName(), $class2); |
72
|
|
|
|
73
|
|
|
$this->calc->addDependency($class1->getClassName(), $class2->getClassName(), 0); |
74
|
|
|
$this->calc->addDependency($class2->getClassName(), $class1->getClassName(), 1); |
75
|
|
|
|
76
|
|
|
$sorted = $this->calc->sort(); |
77
|
|
|
|
78
|
|
|
// There is only 1 valid ordering for this constellation |
79
|
|
|
$correctOrder = [$class2, $class1]; |
80
|
|
|
|
81
|
|
|
self::assertSame($correctOrder, $sorted); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testCommitOrdering3() |
85
|
|
|
{ |
86
|
|
|
// this test corresponds to the GH7259Test::testPersistFileBeforeVersion functional test |
87
|
|
|
$class1 = new ClassMetadata(NodeClass1::class, $this->metadataBuildingContext); |
88
|
|
|
$class2 = new ClassMetadata(NodeClass2::class, $this->metadataBuildingContext); |
89
|
|
|
$class3 = new ClassMetadata(NodeClass3::class, $this->metadataBuildingContext); |
90
|
|
|
$class4 = new ClassMetadata(NodeClass4::class, $this->metadataBuildingContext); |
91
|
|
|
|
92
|
|
|
$this->calc->addNode($class1->getClassName(), $class1); |
93
|
|
|
$this->calc->addNode($class2->getClassName(), $class2); |
94
|
|
|
$this->calc->addNode($class3->getClassName(), $class3); |
95
|
|
|
$this->calc->addNode($class4->getClassName(), $class4); |
96
|
|
|
|
97
|
|
|
$this->calc->addDependency($class4->getClassName(), $class1->getClassName(), 1); |
98
|
|
|
$this->calc->addDependency($class1->getClassName(), $class2->getClassName(), 1); |
99
|
|
|
$this->calc->addDependency($class4->getClassName(), $class3->getClassName(), 1); |
100
|
|
|
$this->calc->addDependency($class1->getClassName(), $class4->getClassName(), 0); |
101
|
|
|
|
102
|
|
|
$sorted = $this->calc->sort(); |
103
|
|
|
|
104
|
|
|
// There is only multiple valid ordering for this constellation, but |
105
|
|
|
// the class4, class1, class2 ordering is important to break the cycle |
106
|
|
|
// on the nullable link. |
107
|
|
|
$correctOrders = [ |
108
|
|
|
[$class4, $class1, $class2, $class3], |
109
|
|
|
[$class4, $class1, $class3, $class2], |
110
|
|
|
[$class4, $class3, $class1, $class2], |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
// We want to perform a strict comparison of the array |
114
|
|
|
$this->assertContains($sorted, $correctOrders, '', false, true, true); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
class NodeClass1 |
119
|
|
|
{ |
120
|
|
|
} |
121
|
|
|
class NodeClass2 |
122
|
|
|
{ |
123
|
|
|
} |
124
|
|
|
class NodeClass3 |
125
|
|
|
{ |
126
|
|
|
} |
127
|
|
|
class NodeClass4 |
128
|
|
|
{ |
129
|
|
|
} |
130
|
|
|
class NodeClass5 |
131
|
|
|
{ |
132
|
|
|
} |
133
|
|
|
|