ILIAS  release_7 Revision v7.30-3-g800a261c036
AgentCollectionTest.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
10use ILIAS\Refinery\Factory as Refinery;
11use ILIAS\Data\Factory as DataFactory;
12use PHPUnit\Framework\TestCase;
13
14class AgentCollectionTest extends TestCase
15{
16 use Helper;
17
18 public function testHasConfig() : void
19 {
20 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
21
22 $c1 = $this->newAgent();
23 $c2 = $this->newAgent();
24 $c3 = $this->newAgent();
25 $c4 = $this->newAgent();
26
27 $c1->method("hasConfig")->willReturn(true);
28 $c2->method("hasConfig")->willReturn(true);
29 $c3->method("hasConfig")->willReturn(false);
30 $c4->method("hasConfig")->willReturn(false);
31
32 $col1 = new Setup\AgentCollection($refinery, ["c1" => $c1]);
33 $col2 = new Setup\AgentCollection($refinery, ["c1" => $c1, "c2" => $c2]);
34 $col3 = new Setup\AgentCollection($refinery, ["c1" => $c1, "c3" => $c3]);
35 $col4 = new Setup\AgentCollection($refinery, ["c3" => $c3]);
36 $col5 = new Setup\AgentCollection($refinery, ["c3" => $c3, "c4" => $c4]);
37
38 $this->assertTrue($col1->hasConfig());
39 $this->assertTrue($col2->hasConfig());
40 $this->assertTrue($col3->hasConfig());
41 $this->assertFalse($col4->hasConfig());
42 $this->assertFalse($col5->hasConfig());
43 }
44
45 public function testGetArrayToConfigTransformation() : void
46 {
47 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
48
49 $c1 = $this->newAgent();
50 $c2 = $this->newAgent();
51 $c3 = $this->newAgent();
52
53 $conf1 = $this->newConfig();
54 $conf3 = $this->newConfig();
55
56 foreach ([$c1,$c3] as $c) {
57 $c
58 ->method("hasConfig")
59 ->willReturn(true);
60 }
61 $c2
62 ->method("hasConfig")
63 ->willReturn(false);
64
65 $arr = ["c1" => ["c1_data"], "c3" => ["c3_data"]];
66
67 $c1
68 ->expects($this->once())
69 ->method("getArrayToConfigTransformation")
70 ->with()
71 ->willReturn($refinery->custom()->transformation(function ($v) use ($conf1) {
72 $this->assertEquals($v, ["c1_data"]);
73 return $conf1;
74 }));
75 $c2
76 ->expects($this->never())
77 ->method("getArrayToConfigTransformation");
78 $c3
79 ->expects($this->once())
80 ->method("getArrayToConfigTransformation")
81 ->with()
82 ->willReturn($refinery->custom()->transformation(function ($v) use ($conf3) {
83 $this->assertEquals($v, ["c3_data"]);
84 return $conf3;
85 }));
86
87 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2,"c3" => $c3]);
88 $trafo = $col->getArrayToConfigTransformation();
89 $conf = $trafo($arr);
90
91 $this->assertInstanceOf(Setup\ConfigCollection::class, $conf);
92 $this->assertEquals(["c1", "c3"], $conf->getKeys());
93 $this->assertEquals($conf1, $conf->getConfig("c1"));
94 $this->assertEquals($conf3, $conf->getConfig("c3"));
95 }
96
98 {
99 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
100
101 $c1 = $this->newAgent();
102 $c2 = $this->newAgent();
103 $c3 = $this->newAgent();
104
105 $conf1 = $this->newConfig();
106 $conf3 = $this->newConfig();
107
108 foreach ([$c1,$c3] as $c) {
109 $c
110 ->method("hasConfig")
111 ->willReturn(true);
112 }
113 $c2
114 ->method("hasConfig")
115 ->willReturn(false);
116
117 $arr = ["c1" => ["c1_data"]];
118
119 $c1
120 ->expects($this->once())
121 ->method("getArrayToConfigTransformation")
122 ->with()
123 ->willReturn($refinery->custom()->transformation(function ($v) use ($conf1) {
124 $this->assertEquals($v, ["c1_data"]);
125 return $conf1;
126 }));
127 $c2
128 ->expects($this->never())
129 ->method("getArrayToConfigTransformation");
130 $c3
131 ->expects($this->once())
132 ->method("getArrayToConfigTransformation")
133 ->with()
134 ->willReturn($refinery->custom()->transformation(function ($v) use ($conf3) {
135 $this->assertEquals($v, null);
136 return $conf3;
137 }));
138
139 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2,"c3" => $c3]);
140 $trafo = $col->getArrayToConfigTransformation();
141 $conf = $trafo($arr);
142
143 $this->assertInstanceOf(Setup\ConfigCollection::class, $conf);
144 $this->assertEquals(["c1", "c3"], $conf->getKeys());
145 $this->assertEquals($conf1, $conf->getConfig("c1"));
146 $this->assertEquals($conf3, $conf->getConfig("c3"));
147 }
148
149 public function testGetInstallObjective() : void
150 {
151 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
152
153 $c1 = $this->newAgent();
154 $c2 = $this->newAgent();
155
156 $g1 = $this->newObjective();
157 $g2 = $this->newObjective();
158
159 $conf1 = $this->newConfig();
160
161 $c1
162 ->expects($this->once())
163 ->method("hasConfig")
164 ->willReturn(true);
165 $c2
166 ->expects($this->once())
167 ->method("hasConfig")
168 ->willReturn(false);
169
170 $c1
171 ->expects($this->once())
172 ->method("getInstallObjective")
173 ->with($conf1)
174 ->willReturn($g1);
175 $c2
176 ->expects($this->once())
177 ->method("getInstallObjective")
178 ->with()
179 ->willReturn($g2);
180
181 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2]);
182 $conf = new Setup\ConfigCollection(["c1" => $conf1]);
183
184 $g = $col->getInstallObjective($conf);
185
186 $this->assertInstanceOf(Setup\ObjectiveCollection::class, $g);
187 $this->assertEquals([$g1, $g2], $g->getObjectives());
188 }
189
190 public function testGetUpdateObjective() : void
191 {
192 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
193 $storage = $this->createMock(Setup\Metrics\Storage::class);
194
195 $c1 = $this->newAgent();
196 $c2 = $this->newAgent();
197
198 $g1 = $this->newObjective();
199 $g2 = $this->newObjective();
200
201 $s1 = new Setup\Metrics\StorageOnPathWrapper("c1", $storage);
202 $s2 = new Setup\Metrics\StorageOnPathWrapper("c2", $storage);
203
204 $c1
205 ->expects($this->once())
206 ->method("getStatusObjective")
207 ->with($s1)
208 ->willReturn($g1);
209 $c2
210 ->expects($this->once())
211 ->method("getStatusObjective")
212 ->with($s2)
213 ->willReturn($g2);
214
215 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2]);
216 $g = $col->getStatusObjective($storage);
217
218 $this->assertInstanceOf(Setup\ObjectiveCollection::class, $g);
219 $this->assertEquals([$g1, $g2], $g->getObjectives());
220 }
221
222 public function testGetCollectMetricsObjective() : void
223 {
224 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
225
226 $c1 = $this->newAgent();
227 $c2 = $this->newAgent();
228
229 $g1 = $this->newObjective();
230 $g2 = $this->newObjective();
231
232 $conf1 = $this->newConfig();
233
234 $c1
235 ->expects($this->once())
236 ->method("getUpdateObjective")
237 ->with($conf1)
238 ->willReturn($g1);
239 $c2
240 ->expects($this->once())
241 ->method("getUpdateObjective")
242 ->with()
243 ->willReturn($g2);
244
245 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2]);
246 $conf = new Setup\ConfigCollection(["c1" => $conf1]);
247
248 $g = $col->getUpdateObjective($conf);
249
250 $this->assertInstanceOf(Setup\ObjectiveCollection::class, $g);
251 $this->assertEquals([$g1, $g2], $g->getObjectives());
252 }
253
254 public function testGetAgent()
255 {
256 $refinery = $this->createMock(Refinery::class);
257
258 $c1 = $this->newAgent();
259 $c2 = $this->newAgent();
260 $c3 = $this->newAgent();
261 $c4 = $this->newAgent();
262
264 $refinery,
265 ["c1" => $c1, "c2" => $c2, "c3" => $c3, "c4" => $c4]
266 );
267
268 $this->assertSame($c1, $c->getAgent("c1"));
269 $this->assertSame($c2, $c->getAgent("c2"));
270 $this->assertSame($c3, $c->getAgent("c3"));
271 $this->assertSame($c4, $c->getAgent("c4"));
272 $this->assertNull($c->getAgent("c5"));
273 }
274
275 public function testWithRemovedAgent()
276 {
277 $refinery = $this->createMock(Refinery::class);
278
279 $c1 = $this->newAgent();
280 $c2 = $this->newAgent();
281 $c3 = $this->newAgent();
282 $c4 = $this->newAgent();
283
284 $ca = new Setup\AgentCollection(
285 $refinery,
286 ["c1" => $c1, "c2" => $c2, "c3" => $c3, "c4" => $c4]
287 );
288 $cb = $ca->withRemovedAgent("c2");
289
290 $this->assertNotSame($ca, $cb);
291
292 $this->assertSame($c1, $ca->getAgent("c1"));
293 $this->assertSame($c2, $ca->getAgent("c2"));
294 $this->assertSame($c3, $ca->getAgent("c3"));
295 $this->assertSame($c4, $ca->getAgent("c4"));
296 $this->assertNull($ca->getAgent("c5"));
297
298 $this->assertSame($c1, $cb->getAgent("c1"));
299 $this->assertNull($cb->getAgent("c2"));
300 $this->assertSame($c3, $cb->getAgent("c3"));
301 $this->assertSame($c4, $cb->getAgent("c4"));
302 $this->assertNull($cb->getAgent("c5"));
303 }
304
305 public function testWithAdditionalAgent()
306 {
307 $refinery = $this->createMock(Refinery::class);
308
309 $c1 = $this->newAgent();
310 $c2 = $this->newAgent();
311 $c3 = $this->newAgent();
312 $c4 = $this->newAgent();
313
314 $ca = new Setup\AgentCollection(
315 $refinery,
316 ["c1" => $c1, "c2" => $c2, "c3" => $c3]
317 );
318 $cb = $ca->withAdditionalAgent("c4", $c4);
319
320 $this->assertNotSame($ca, $cb);
321
322 $this->assertSame($c1, $ca->getAgent("c1"));
323 $this->assertSame($c2, $ca->getAgent("c2"));
324 $this->assertSame($c3, $ca->getAgent("c3"));
325 $this->assertNull($ca->getAgent("c4"));
326 $this->assertNull($ca->getAgent("c5"));
327
328 $this->assertSame($c1, $cb->getAgent("c1"));
329 $this->assertSame($c2, $cb->getAgent("c2"));
330 $this->assertSame($c3, $cb->getAgent("c3"));
331 $this->assertSame($c4, $cb->getAgent("c4"));
332 $this->assertNull($cb->getAgent("c5"));
333 }
334
335 public function testGetNamedObjective() : void
336 {
337 $refinery = $this->createMock(Refinery::class);
338
339 $o = $this->newObjective();
340 $c1 = $this->newAgent();
341 $c2 = $this->newAgent();
342 $conf2 = $this->newConfig();
343
344 $conf = new Setup\ConfigCollection(
345 ["sub" => new Setup\ConfigCollection(
346 [ "c2" => $conf2 ]
347 )]
348 );
349
351 $refinery,
352 [ "sub" => new Setup\AgentCollection(
353 $refinery,
354 ["c1" => $c1, "c2" => $c2]
355 )]
356 );
357
358 $c2->expects($this->once())
359 ->method("getNamedObjective")
360 ->with("the_objective", $conf2)
361 ->willReturn($o);
362
363 $res = $c->getNamedObjective("sub.c2.the_objective", $conf);
364 $this->assertSame($o, $res);
365 }
366}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
An agent that is just a collection of some other agents.
A collection of some configurations.
$c
Definition: cli.php:37
A transformation is a function from one datatype to another.
This is what a factory for input fields looks like.
Definition: Factory.php:11
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
foreach($_POST as $key=> $value) $res