ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ObjectiveIteratorTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2019 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
6
8
9class ObjectiveIteratorTest extends \PHPUnit\Framework\TestCase
10{
11 public function testBasicAlgorithm()
12 {
13 $hash = "my hash";
14 $objective = $this->newObjective($hash);
15 $environment = $this->createMock(Setup\Environment::class);
16
17 $objective
18 ->expects($this->once())
19 ->method("getPreconditions")
20 ->with($environment)
21 ->willReturn([]);
22
23 $iterator = new Setup\ObjectiveIterator($environment, $objective);
24
25 $this->assertTrue($iterator->valid());
26 $this->assertSame($objective, $iterator->current());
27 $this->assertSame($hash, $iterator->key());
28
29 $iterator->next();
30
31 $this->assertFalse($iterator->valid());
32 }
33
34 public function testRewind()
35 {
36 $hash = "my hash";
37 $objective = $this->newObjective($hash);
38 $environment = $this->createMock(Setup\Environment::class);
39
40 $iterator = new Setup\ObjectiveIterator($environment, $objective);
41
42 $objective
43 ->expects($this->once())
44 ->method("getPreconditions")
45 ->with($environment)
46 ->willReturn([]);
47
48 $iterator->next();
49 $iterator->rewind();
50
51 $this->assertTrue($iterator->valid());
52 $this->assertSame($objective, $iterator->current());
53 $this->assertSame($hash, $iterator->key());
54 }
55
56 public function testAllObjectives()
57 {
58 $environment = $this->createMock(Setup\Environment::class);
59
60 $objective1 = $this->newObjective();
61 $objective11 = $this->newObjective();
62 $objective12 = $this->newObjective();
63 $objective121 = $this->newObjective();
64
65 $objective1
66 ->method("getPreconditions")
67 ->with($environment)
68 ->willReturn([$objective11, $objective12]);
69
70 $objective11
71 ->method("getPreconditions")
72 ->with($environment)
73 ->willReturn([]);
74
75 $objective12
76 ->method("getPreconditions")
77 ->with($environment)
78 ->willReturn([$objective121]);
79
80 $objective121
81 ->method("getPreconditions")
82 ->with($environment)
83 ->willReturn([]);
84
85 $iterator = new Setup\ObjectiveIterator($environment, $objective1);
86
87 $expected = [
88 $objective11->getHash() => $objective11,
89 $objective121->getHash() => $objective121,
90 $objective12->getHash() => $objective12,
91 $objective1->getHash() => $objective1
92 ];
93
94 $this->assertEquals($expected, iterator_to_array($iterator));
95 }
96
98 {
99 $environment = $this->createMock(Setup\Environment::class);
100
101 $objective1 = $this->newObjective();
102 $objective11 = $this->newObjective();
103
104 $objective1
105 ->method("getPreconditions")
106 ->with($environment)
107 ->willReturn([$objective11, $objective11]);
108
109 $objective11
110 ->method("getPreconditions")
111 ->with($environment)
112 ->willReturn([]);
113
114 $iterator = new Setup\ObjectiveIterator($environment, $objective1);
115
116 $expected = [
117 $objective11->getHash() => $objective11,
118 $objective1->getHash() => $objective1
119 ];
120 $this->assertEquals($expected, iterator_to_array($iterator));
121 }
122
124 {
125 $environment = $this->createMock(Setup\Environment::class);
126
127 $objective1 = $this->newObjective();
128 $objective2 = $this->newObjective();
129
130 $objective1
131 ->method("getPreconditions")
132 ->with($environment)
133 ->willReturn([$objective2]);
134
135 $objective2
136 ->method("getPreconditions")
137 ->with($environment)
138 ->willReturn([$objective1]);
139
140 $this->expectException(Setup\UnachievableException::class);
141
142 $iterator = new Setup\ObjectiveIterator($environment, $objective1);
143 iterator_to_array($iterator);
144 }
145
146 public function testSetEnvironment()
147 {
148 $env1 = new Setup\ArrayEnvironment([]);
149 $env2 = new Setup\ArrayEnvironment([]);
150
151 $objective1 = $this->newObjective();
152 $objective2 = $this->newObjective();
153
154 $objective1
155 ->expects($this->atLeastOnce())
156 ->method("getPreconditions")
157 ->with($env1)
158 ->willReturn([$objective2]);
159
160 $objective2
161 ->expects($this->atLeastOnce())
162 ->method("getPreconditions")
163 ->with($env2)
164 ->willReturn([]);
165
166 $iterator = new Setup\ObjectiveIterator($env1, $objective1);
167
168 $iterator->setEnvironment($env2);
169 $iterator->next();
170 }
171
172 public function testMarkFailed()
173 {
174 $this->expectException(Setup\UnachievableException::class);
175
176 $env = new Setup\ArrayEnvironment([]);
177
178 $objective_fail = $this->newObjective();
179 $objective_1 = $this->newObjective();
180 $objective_2 = $this->newObjective();
181 $objective_3 = $this->newObjective();
182
183 $objective_1
184 ->method("getPreconditions")
185 ->willReturn([]);
186
187 $objective_2
188 ->method("getPreconditions")
189 ->willReturn([]);
190
191 $objective_3
192 ->method("getPreconditions")
193 ->willReturn([$objective_1, $objective_fail, $objective_2]);
194
195 $iterator = new Setup\ObjectiveIterator($env, $objective_3);
196
197
198 $this->assertEquals($objective_1, $iterator->current());
199 $iterator->next();
200 $this->assertEquals($objective_fail, $iterator->current());
201 $iterator->markAsFailed($objective_fail);
202 $iterator->next();
203 $this->assertEquals($objective_2, $iterator->current());
204 $iterator->next();
205 }
206
207 protected function newObjective($hash = null)
208 {
209 static $no = 0;
210
211 $objective = $this
212 ->getMockBuilder(Setup\Objective::class)
213 ->setMethods(["getHash", "getLabel", "isNotable", "withResourcesFrom", "getPreconditions", "achieve"])
214 ->setMockClassName("Mock_ObjectiveNo" . ($no++))
215 ->getMock();
216
217 $objective
218 ->method("getHash")
219 ->willReturn($hash ?? "" . $no);
220
221 return $objective;
222 }
223}
An exception for terminatinating execution or to throw for unit testing.
Tries to enumerate all preconditions for the given objective, where the ones that can be achieved (i....