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