ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ObjectiveIteratorTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Tests\Setup;
22
23use ILIAS\Setup;
24use PHPUnit\Framework\MockObject\MockObject;
25use PHPUnit\Framework\TestCase;
26
27class ObjectiveIteratorTest extends TestCase
28{
29 public function testBasicAlgorithm(): void
30 {
31 $hash = "my hash";
32 $objective = $this->newObjective($hash);
33 $environment = $this->createMock(Setup\Environment::class);
34
35 $objective
36 ->expects($this->once())
37 ->method("getPreconditions")
38 ->with($environment)
39 ->willReturn([]);
40
41 $iterator = new Setup\ObjectiveIterator($environment, $objective);
42
43 $this->assertTrue($iterator->valid());
44 $this->assertSame($objective, $iterator->current());
45 $this->assertSame($hash, $iterator->key());
46
47 $iterator->next();
48
49 $this->assertFalse($iterator->valid());
50 }
51
52 public function testRewind(): void
53 {
54 $hash = "my hash";
55 $objective = $this->newObjective($hash);
56 $environment = $this->createMock(Setup\Environment::class);
57
58 $iterator = new Setup\ObjectiveIterator($environment, $objective);
59
60 $objective
61 ->expects($this->once())
62 ->method("getPreconditions")
63 ->with($environment)
64 ->willReturn([]);
65
66 $iterator->next();
67 $iterator->rewind();
68
69 $this->assertTrue($iterator->valid());
70 $this->assertSame($objective, $iterator->current());
71 $this->assertSame($hash, $iterator->key());
72 }
73
74 public function testAllObjectives(): void
75 {
76 $environment = $this->createMock(Setup\Environment::class);
77
78 $objective1 = $this->newObjective();
79 $objective11 = $this->newObjective();
80 $objective12 = $this->newObjective();
81 $objective121 = $this->newObjective();
82
83 $objective1
84 ->method("getPreconditions")
85 ->with($environment)
86 ->willReturn([$objective11, $objective12]);
87
88 $objective11
89 ->method("getPreconditions")
90 ->with($environment)
91 ->willReturn([]);
92
93 $objective12
94 ->method("getPreconditions")
95 ->with($environment)
96 ->willReturn([$objective121]);
97
98 $objective121
99 ->method("getPreconditions")
100 ->with($environment)
101 ->willReturn([]);
102
103 $iterator = new Setup\ObjectiveIterator($environment, $objective1);
104
105 $expected = [
106 $objective11->getHash() => $objective11,
107 $objective121->getHash() => $objective121,
108 $objective12->getHash() => $objective12,
109 $objective1->getHash() => $objective1
110 ];
111
112 $this->assertEquals($expected, iterator_to_array($iterator));
113 }
114
116 {
117 $environment = $this->createMock(Setup\Environment::class);
118
119 $objective1 = $this->newObjective();
120 $objective11 = $this->newObjective();
121
122 $objective1
123 ->method("getPreconditions")
124 ->with($environment)
125 ->willReturn([$objective11, $objective11]);
126
127 $objective11
128 ->method("getPreconditions")
129 ->with($environment)
130 ->willReturn([]);
131
132 $iterator = new Setup\ObjectiveIterator($environment, $objective1);
133
134 $expected = [
135 $objective11->getHash() => $objective11,
136 $objective1->getHash() => $objective1
137 ];
138 $this->assertEquals($expected, iterator_to_array($iterator));
139 }
140
141 public function testAllObjectivesDetectsCycle(): void
142 {
143 $environment = $this->createMock(Setup\Environment::class);
144
145 $objective1 = $this->newObjective();
146 $objective2 = $this->newObjective();
147
148 $objective1
149 ->method("getPreconditions")
150 ->with($environment)
151 ->willReturn([$objective2]);
152
153 $objective2
154 ->method("getPreconditions")
155 ->with($environment)
156 ->willReturn([$objective1]);
157
158 $this->expectException(Setup\UnachievableException::class);
159
160 $iterator = new Setup\ObjectiveIterator($environment, $objective1);
161 iterator_to_array($iterator);
162 }
163
164 public function testSetEnvironment(): void
165 {
166 $env1 = new Setup\ArrayEnvironment([]);
167 $env2 = new Setup\ArrayEnvironment([]);
168
169 $objective1 = $this->newObjective();
170 $objective2 = $this->newObjective();
171
172 $objective1
173 ->expects($this->atLeastOnce())
174 ->method("getPreconditions")
175 ->with($env1)
176 ->willReturn([$objective2]);
177
178 $objective2
179 ->expects($this->atLeastOnce())
180 ->method("getPreconditions")
181 ->with($env2)
182 ->willReturn([]);
183
184 $iterator = new Setup\ObjectiveIterator($env1, $objective1);
185
186 $iterator->setEnvironment($env2);
187 $iterator->next();
188 }
189
190 public function testMarkFailed(): void
191 {
192 $this->expectException(Setup\UnachievableException::class);
193
194 $env = new Setup\ArrayEnvironment([]);
195
196 $objective_fail = $this->newObjective();
197 $objective_1 = $this->newObjective();
198 $objective_2 = $this->newObjective();
199 $objective_3 = $this->newObjective();
200
201 $objective_1
202 ->method("getPreconditions")
203 ->willReturn([]);
204
205 $objective_2
206 ->method("getPreconditions")
207 ->willReturn([]);
208
209 $objective_3
210 ->method("getPreconditions")
211 ->willReturn([$objective_1, $objective_fail, $objective_2]);
212
213 $iterator = new Setup\ObjectiveIterator($env, $objective_3);
214
215
216 $this->assertEquals($objective_1, $iterator->current());
217 $iterator->next();
218 $this->assertEquals($objective_fail, $iterator->current());
219 $iterator->markAsFailed($objective_fail);
220 $iterator->next();
221 $this->assertEquals($objective_2, $iterator->current());
222 $iterator->next();
223 }
224
225 protected function newObjective($hash = null): MockObject
226 {
227 static $no = 0;
228
229 $objective = $this
230 ->getMockBuilder(Setup\Objective::class)
231 ->onlyMethods(["getHash", "getLabel", "isNotable", "getPreconditions", "achieve", "isApplicable"])
232 ->setMockClassName("Mock_ObjectiveNo" . ($no++))
233 ->getMock();
234
235 $objective
236 ->method("getHash")
237 ->willReturn($hash ?? "" . $no);
238
239 return $objective;
240 }
241
243 {
244 $this->expectException(Setup\UnachievableException::class);
245
246 $env = new Setup\ArrayEnvironment([]);
247
248 $objective_fail = $this->newObjective();
249 $objective_1 = $this->newObjective();
250 $objective_2 = $this->newObjective();
251 $objective_3 = $this->newObjective();
252
253 $objective_1
254 ->method("getPreconditions")
255 ->willReturn([$objective_fail]);
256 $objective_2
257 ->method("getPreconditions")
258 ->willReturn([]);
259 $objective_3
260 ->method("getPreconditions")
261 ->willReturn([$objective_1, $objective_2]);
262
263 $iterator = new class ($env, $objective_3, $objective_fail) extends Setup\ObjectiveIterator {
264 public function __construct(
265 Setup\Environment $environment,
266 Setup\Objective $objective,
267 MockObject $objective_fail
268 ) {
269 parent::__construct($environment, $objective);
270 $this->failed[$objective_fail->getHash()] = true;
271 }
272 };
273
274 $this->assertEquals($objective_fail, $iterator->current());
275 $iterator->next();
276 $iterator->next();
277 }
278
279 public function testFailedPreconditionLastOnStack(): void
280 {
281 $this->expectException(Setup\UnachievableException::class);
282
283 $env = new Setup\ArrayEnvironment([]);
284
285 $objective_fail = $this->newObjective();
286 $objective_1 = $this->newObjective();
287 $objective_2 = $this->newObjective();
288
289 $objective_1
290 ->method("getPreconditions")
291 ->willReturn([$objective_fail]);
292 $objective_2
293 ->method("getPreconditions")
294 ->willReturn([$objective_1]);
295
296 $iterator = new class ($env, $objective_2, $objective_fail) extends Setup\ObjectiveIterator {
297 public function __construct(
298 Setup\Environment $environment,
299 Setup\Objective $objective,
300 MockObject $objective_fail
301 ) {
302 parent::__construct($environment, $objective);
303 $this->failed[$objective_fail->getHash()] = true;
304 }
305 };
306
307 $this->assertEquals($objective_fail, $iterator->current());
308 $iterator->next();
309 }
310}
Tries to enumerate all preconditions for the given objective, where the ones that can be achieved (i....
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:31
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...