1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maketok\DataMigration\Unit; |
4
|
|
|
|
5
|
|
|
use Maketok\DataMigration\Unit\Type\Unit; |
6
|
|
|
|
7
|
|
|
class SimpleBagTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var SimpleBag |
11
|
|
|
*/ |
12
|
|
|
protected $bag; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* set up |
16
|
|
|
* 1 - 5 |
17
|
|
|
* / \ |
18
|
|
|
* 3 - 2 4 |
19
|
|
|
* \ |
20
|
|
|
* 6 |
21
|
|
|
*/ |
22
|
|
|
public function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->bag = new SimpleBag(); |
25
|
|
|
|
26
|
|
|
$unit = new Unit('test1'); |
27
|
|
|
$unit2 = new Unit('test2'); |
28
|
|
|
$unit3= new Unit('test3'); |
29
|
|
|
$unit4 = new Unit('test4'); |
30
|
|
|
$unit5 = new Unit('test5'); |
31
|
|
|
$unit6 = new Unit('test6'); |
32
|
|
|
|
33
|
|
|
$unit6->setParent($unit4); |
34
|
|
|
$unit4->setParent($unit); |
35
|
|
|
$unit->addSibling($unit5); |
36
|
|
|
$unit3->addSibling($unit2); |
37
|
|
|
$unit2->setParent($unit); |
38
|
|
|
|
39
|
|
|
$this->bag->addSet([$unit, $unit2, $unit3, $unit4, $unit5, $unit6]); |
40
|
|
|
$this->bag->compileTree(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testing() |
44
|
|
|
{ |
45
|
|
|
$this->assertTrue($this->bag->isLowest('test6')); |
46
|
|
|
$this->assertEquals(3, $this->bag->getLowestLevel()); |
47
|
|
|
$this->assertEquals(2, $this->bag->getUnitLevel('test3')); |
48
|
|
|
$unit = $this->bag->getUnitByCode('test1'); |
49
|
|
|
$unit2 = $this->bag->getUnitByCode('test2'); |
50
|
|
|
$unit3 = $this->bag->getUnitByCode('test3'); |
51
|
|
|
$unit4 = $this->bag->getUnitByCode('test4'); |
52
|
|
|
$unit5 = $this->bag->getUnitByCode('test5'); |
53
|
|
|
$unit6 = $this->bag->getUnitByCode('test6'); |
54
|
|
|
$this->assertSame([$unit6], $this->bag->getChildren('test4')); |
55
|
|
|
$this->assertSame(['test2', 'test3', 'test4'], $this->bag->getUnitsFromLevel(2)); |
56
|
|
|
$this->assertEquals([ |
57
|
|
|
['pc' => ['test4' => $unit4,'test6' => $unit6]], |
58
|
|
|
['s' => ['test3' => $unit3, 'test2' => $unit2]], |
59
|
|
|
['pc' => ['test2' => $unit2, 'test1' => $unit]], |
60
|
|
|
['pc' => ['test4' => $unit4, 'test1' => $unit]], |
61
|
|
|
['s' => ['test1' => $unit, 'test5' => $unit5]], |
62
|
|
|
], $this->bag->getRelations()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testCompileLevels() |
66
|
|
|
{ |
67
|
|
|
$bag = new SimpleBag(); |
68
|
|
|
|
69
|
|
|
$unit = new Unit('test1'); |
70
|
|
|
$unit2 = new Unit('test2'); |
71
|
|
|
$bag->addSet([$unit, $unit2]); |
72
|
|
|
$bag->compileTree(); |
73
|
|
|
$this->assertSame(['test1', 'test2'], $bag->getUnitsFromLevel(1)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testIteratorSort() |
77
|
|
|
{ |
78
|
|
|
$unit1 = new Unit('A'); |
79
|
|
|
$unit2 = new Unit('B'); |
80
|
|
|
$unit2->setParent($unit1); |
81
|
|
|
|
82
|
|
|
$bag = new SimpleBag(); |
83
|
|
|
$bag->addSet([$unit2, $unit1]); |
84
|
|
|
|
85
|
|
|
$i = 0; |
86
|
|
|
foreach ($bag as $unit) { |
87
|
|
|
if ($i == 0) { |
88
|
|
|
$this->assertSame($unit1, $unit); |
89
|
|
|
} elseif ($i == 1) { |
90
|
|
|
$this->assertSame($unit2, $unit); |
91
|
|
|
} |
92
|
|
|
++$i; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testIteratorSort2() |
97
|
|
|
{ |
98
|
|
|
$unit1 = new Unit('A'); |
99
|
|
|
$unit2 = new Unit('B'); |
100
|
|
|
$unit3 = new Unit('C'); |
101
|
|
|
$unit2->setParent($unit1); |
102
|
|
|
$unit3->setParent($unit2); |
103
|
|
|
|
104
|
|
|
$bag = new SimpleBag(); |
105
|
|
|
$bag->addSet([$unit2, $unit3, $unit1]); |
106
|
|
|
|
107
|
|
|
$i = 0; |
108
|
|
|
foreach ($bag as $unit) { |
109
|
|
|
if ($i == 0) { |
110
|
|
|
$this->assertSame($unit1, $unit); |
111
|
|
|
} elseif ($i == 1) { |
112
|
|
|
$this->assertSame($unit2, $unit); |
113
|
|
|
} elseif ($i == 2) { |
114
|
|
|
$this->assertSame($unit3, $unit); |
115
|
|
|
} |
116
|
|
|
++$i; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testIteratorSortEqual() |
121
|
|
|
{ |
122
|
|
|
$unit1 = new Unit('A'); |
123
|
|
|
$unit2 = new Unit('B'); |
124
|
|
|
$unit3 = new Unit('C'); |
125
|
|
|
|
126
|
|
|
$bag = new SimpleBag(); |
127
|
|
|
$bag->addSet([$unit2, $unit3, $unit1]); |
128
|
|
|
|
129
|
|
|
$i = 0; |
130
|
|
|
foreach ($bag as $unit) { |
131
|
|
|
if ($i == 0) { |
132
|
|
|
$this->assertSame($unit2, $unit); |
133
|
|
|
} elseif ($i == 1) { |
134
|
|
|
$this->assertSame($unit3, $unit); |
135
|
|
|
} elseif ($i == 2) { |
136
|
|
|
$this->assertSame($unit1, $unit); |
137
|
|
|
} |
138
|
|
|
++$i; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|