ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AgentCollectionTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Tests\Setup;
22
23require_once(__DIR__ . "/Helper.php");
24
25use ILIAS\Setup;
28use ILIAS\Refinery\Factory as Refinery;
29use ILIAS\Data\Factory as DataFactory;
30use PHPUnit\Framework\TestCase;
31
32class AgentCollectionTest extends TestCase
33{
34 use Helper;
35
36 public function testHasConfig(): void
37 {
38 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
39
40 $c1 = $this->newAgent();
41 $c2 = $this->newAgent();
42 $c3 = $this->newAgent();
43 $c4 = $this->newAgent();
44
45 $c1->method("hasConfig")->willReturn(true);
46 $c2->method("hasConfig")->willReturn(true);
47 $c3->method("hasConfig")->willReturn(false);
48 $c4->method("hasConfig")->willReturn(false);
49
50 $col1 = new Setup\AgentCollection($refinery, ["c1" => $c1]);
51 $col2 = new Setup\AgentCollection($refinery, ["c1" => $c1, "c2" => $c2]);
52 $col3 = new Setup\AgentCollection($refinery, ["c1" => $c1, "c3" => $c3]);
53 $col4 = new Setup\AgentCollection($refinery, ["c3" => $c3]);
54 $col5 = new Setup\AgentCollection($refinery, ["c3" => $c3, "c4" => $c4]);
55
56 $this->assertTrue($col1->hasConfig());
57 $this->assertTrue($col2->hasConfig());
58 $this->assertTrue($col3->hasConfig());
59 $this->assertFalse($col4->hasConfig());
60 $this->assertFalse($col5->hasConfig());
61 }
62
63 public function testGetArrayToConfigTransformation(): void
64 {
65 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
66
67 $c1 = $this->newAgent();
68 $c2 = $this->newAgent();
69 $c3 = $this->newAgent();
70
71 $conf1 = $this->newConfig();
72 $conf3 = $this->newConfig();
73
74 foreach ([$c1,$c3] as $c) {
75 $c
76 ->method("hasConfig")
77 ->willReturn(true);
78 }
79 $c2
80 ->method("hasConfig")
81 ->willReturn(false);
82
83 $arr = ["c1" => ["c1_data"], "c3" => ["c3_data"]];
84
85 $c1
86 ->expects($this->once())
87 ->method("getArrayToConfigTransformation")
88 ->with()
89 ->willReturn($refinery->custom()->transformation(function ($v) use ($conf1) {
90 $this->assertEquals($v, ["c1_data"]);
91 return $conf1;
92 }));
93 $c2
94 ->expects($this->never())
95 ->method("getArrayToConfigTransformation");
96 $c3
97 ->expects($this->once())
98 ->method("getArrayToConfigTransformation")
99 ->with()
100 ->willReturn($refinery->custom()->transformation(function ($v) use ($conf3) {
101 $this->assertEquals($v, ["c3_data"]);
102 return $conf3;
103 }));
104
105 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2,"c3" => $c3]);
106 $trafo = $col->getArrayToConfigTransformation();
107 $conf = $trafo($arr);
108
109 $this->assertInstanceOf(Setup\ConfigCollection::class, $conf);
110 $this->assertEquals(["c1", "c3"], $conf->getKeys());
111 $this->assertEquals($conf1, $conf->getConfig("c1"));
112 $this->assertEquals($conf3, $conf->getConfig("c3"));
113 }
114
116 {
117 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
118
119 $c1 = $this->newAgent();
120 $c2 = $this->newAgent();
121 $c3 = $this->newAgent();
122
123 $conf1 = $this->newConfig();
124 $conf3 = $this->newConfig();
125
126 foreach ([$c1,$c3] as $c) {
127 $c
128 ->method("hasConfig")
129 ->willReturn(true);
130 }
131 $c2
132 ->method("hasConfig")
133 ->willReturn(false);
134
135 $arr = ["c1" => ["c1_data"]];
136
137 $c1
138 ->expects($this->once())
139 ->method("getArrayToConfigTransformation")
140 ->with()
141 ->willReturn($refinery->custom()->transformation(function ($v) use ($conf1) {
142 $this->assertEquals($v, ["c1_data"]);
143 return $conf1;
144 }));
145 $c2
146 ->expects($this->never())
147 ->method("getArrayToConfigTransformation");
148 $c3
149 ->expects($this->once())
150 ->method("getArrayToConfigTransformation")
151 ->with()
152 ->willReturn($refinery->custom()->transformation(function ($v) use ($conf3) {
153 $this->assertEquals($v, null);
154 return $conf3;
155 }));
156
157 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2,"c3" => $c3]);
158 $trafo = $col->getArrayToConfigTransformation();
159 $conf = $trafo($arr);
160
161 $this->assertInstanceOf(Setup\ConfigCollection::class, $conf);
162 $this->assertEquals(["c1", "c3"], $conf->getKeys());
163 $this->assertEquals($conf1, $conf->getConfig("c1"));
164 $this->assertEquals($conf3, $conf->getConfig("c3"));
165 }
166
167 public function testGetInstallObjective(): void
168 {
169 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
170
171 $c1 = $this->newAgent();
172 $c2 = $this->newAgent();
173
174 $g1 = $this->newObjective();
175 $g2 = $this->newObjective();
176
177 $conf1 = $this->newConfig();
178
179 $c1
180 ->expects($this->once())
181 ->method("hasConfig")
182 ->willReturn(true);
183 $c2
184 ->expects($this->once())
185 ->method("hasConfig")
186 ->willReturn(false);
187
188 $c1
189 ->expects($this->once())
190 ->method("getInstallObjective")
191 ->with($conf1)
192 ->willReturn($g1);
193 $c2
194 ->expects($this->once())
195 ->method("getInstallObjective")
196 ->with()
197 ->willReturn($g2);
198
199 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2]);
200 $conf = new Setup\ConfigCollection(["c1" => $conf1]);
201
202 $g = $col->getInstallObjective($conf);
203
204 $this->assertInstanceOf(Setup\ObjectiveCollection::class, $g);
205 $this->assertEquals([$g1, $g2], $g->getObjectives());
206 }
207
208 public function testGetUpdateObjective(): void
209 {
210 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
211 $storage = $this->createMock(Setup\Metrics\Storage::class);
212
213 $c1 = $this->newAgent();
214 $c2 = $this->newAgent();
215
216 $g1 = $this->newObjective();
217 $g2 = $this->newObjective();
218
219 $s1 = new Setup\Metrics\StorageOnPathWrapper("c1", $storage);
220 $s2 = new Setup\Metrics\StorageOnPathWrapper("c2", $storage);
221
222 $c1
223 ->expects($this->once())
224 ->method("getStatusObjective")
225 ->with($s1)
226 ->willReturn($g1);
227 $c2
228 ->expects($this->once())
229 ->method("getStatusObjective")
230 ->with($s2)
231 ->willReturn($g2);
232
233 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2]);
234 $conf = new Setup\ConfigCollection(["c1" => $c1]);
235
236 $g = $col->getStatusObjective($storage);
237
238 $this->assertInstanceOf(Setup\ObjectiveCollection::class, $g);
239 $this->assertEquals([$g1, $g2], $g->getObjectives());
240 }
241
242 public function testGetCollectMetricsObjective(): void
243 {
244 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
245
246 $c1 = $this->newAgent();
247 $c2 = $this->newAgent();
248
249 $g1 = $this->newObjective();
250 $g2 = $this->newObjective();
251
252 $conf1 = $this->newConfig();
253
254 $c1
255 ->expects($this->once())
256 ->method("getUpdateObjective")
257 ->with($conf1)
258 ->willReturn($g1);
259 $c2
260 ->expects($this->once())
261 ->method("getUpdateObjective")
262 ->with()
263 ->willReturn($g2);
264
265 $col = new Setup\AgentCollection($refinery, ["c1" => $c1,"c2" => $c2]);
266 $conf = new Setup\ConfigCollection(["c1" => $conf1]);
267
268 $g = $col->getUpdateObjective($conf);
269
270 $this->assertInstanceOf(Setup\ObjectiveCollection::class, $g);
271 $this->assertEquals([$g1, $g2], $g->getObjectives());
272 }
273
274 public function testGetAgent(): void
275 {
276 $refinery = $this->createMock(Refinery::class);
277
278 $c1 = $this->newAgent();
279 $c2 = $this->newAgent();
280 $c3 = $this->newAgent();
281 $c4 = $this->newAgent();
282
284 $refinery,
285 ["c1" => $c1, "c2" => $c2, "c3" => $c3, "c4" => $c4]
286 );
287
288 $this->assertSame($c1, $c->getAgent("c1"));
289 $this->assertSame($c2, $c->getAgent("c2"));
290 $this->assertSame($c3, $c->getAgent("c3"));
291 $this->assertSame($c4, $c->getAgent("c4"));
292 $this->assertNull($c->getAgent("c5"));
293 }
294
295 public function testWithRemovedAgent(): void
296 {
297 $refinery = $this->createMock(Refinery::class);
298
299 $c1 = $this->newAgent();
300 $c2 = $this->newAgent();
301 $c3 = $this->newAgent();
302 $c4 = $this->newAgent();
303
304 $ca = new Setup\AgentCollection(
305 $refinery,
306 ["c1" => $c1, "c2" => $c2, "c3" => $c3, "c4" => $c4]
307 );
308 $cb = $ca->withRemovedAgent("c2");
309
310 $this->assertNotSame($ca, $cb);
311
312 $this->assertSame($c1, $ca->getAgent("c1"));
313 $this->assertSame($c2, $ca->getAgent("c2"));
314 $this->assertSame($c3, $ca->getAgent("c3"));
315 $this->assertSame($c4, $ca->getAgent("c4"));
316 $this->assertNull($ca->getAgent("c5"));
317
318 $this->assertSame($c1, $cb->getAgent("c1"));
319 $this->assertNull($cb->getAgent("c2"));
320 $this->assertSame($c3, $cb->getAgent("c3"));
321 $this->assertSame($c4, $cb->getAgent("c4"));
322 $this->assertNull($cb->getAgent("c5"));
323 }
324
325 public function testWithAdditionalAgent(): void
326 {
327 $refinery = $this->createMock(Refinery::class);
328
329 $c1 = $this->newAgent();
330 $c2 = $this->newAgent();
331 $c3 = $this->newAgent();
332 $c4 = $this->newAgent();
333
334 $ca = new Setup\AgentCollection(
335 $refinery,
336 ["c1" => $c1, "c2" => $c2, "c3" => $c3]
337 );
338 $cb = $ca->withAdditionalAgent("c4", $c4);
339
340 $this->assertNotSame($ca, $cb);
341
342 $this->assertSame($c1, $ca->getAgent("c1"));
343 $this->assertSame($c2, $ca->getAgent("c2"));
344 $this->assertSame($c3, $ca->getAgent("c3"));
345 $this->assertNull($ca->getAgent("c4"));
346 $this->assertNull($ca->getAgent("c5"));
347
348 $this->assertSame($c1, $cb->getAgent("c1"));
349 $this->assertSame($c2, $cb->getAgent("c2"));
350 $this->assertSame($c3, $cb->getAgent("c3"));
351 $this->assertSame($c4, $cb->getAgent("c4"));
352 $this->assertNull($cb->getAgent("c5"));
353 }
354
355 public function testGetNamedObjectivesSorting(): void
356 {
357 $refinery = $this->createMock(Refinery::class);
358 $config = new Setup\ConfigCollection([]);
359
360 $aAgent = $this->newAgent();
361 $bAgent = $this->newAgent();
362 $cAgent = $this->newAgent();
363 $dAgent = $this->newAgent();
364
365 $aReturn = [
366 "a-2" => $this->newObjectiveConstructor(),
367 "a-1" => $this->newObjectiveConstructor()
368 ];
369
370 $bReturn = [
371 "b-1" => $this->newObjectiveConstructor(),
372 "b-3" => $this->newObjectiveConstructor(),
373 "b-2" => $this->newObjectiveConstructor(),
374 ];
375
376 $cReturn = [
377 "c-2" => $this->newObjectiveConstructor(),
378 "c-1" => $this->newObjectiveConstructor()
379 ];
380
381 $dReturn = [
382 "d-2" => $this->newObjectiveConstructor(),
383 "d-3" => $this->newObjectiveConstructor(),
384 "d-1" => $this->newObjectiveConstructor()
385 ];
386
387 $aAgent
388 ->expects($this->once())
389 ->method("getNamedObjectives")
390 ->willReturn($aReturn);
391
392 $bAgent
393 ->expects($this->once())
394 ->method("getNamedObjectives")
395 ->willReturn($bReturn);
396
397 $cAgent
398 ->expects($this->once())
399 ->method("getNamedObjectives")
400 ->willReturn($cReturn);
401
402 $dAgent
403 ->expects($this->once())
404 ->method("getNamedObjectives")
405 ->willReturn($dReturn);
406
407 $testAgentCollection = new Setup\AgentCollection(
408 $refinery,
409 ["aAgent" => $aAgent, "cAgent" => $cAgent, "bAgent" => $bAgent, "dAgent" => $dAgent]
410 );
411
412 $expected = [
413 "aAgent.a-1" => $aReturn["a-1"],
414 "aAgent.a-2" => $aReturn["a-2"],
415 "bAgent.b-1" => $bReturn["b-1"],
416 "bAgent.b-2" => $bReturn["b-2"],
417 "bAgent.b-3" => $bReturn["b-3"],
418 "cAgent.c-1" => $cReturn["c-1"],
419 "cAgent.c-2" => $cReturn["c-2"],
420 "dAgent.d-1" => $dReturn["d-1"],
421 "dAgent.d-2" => $dReturn["d-2"],
422 "dAgent.d-3" => $dReturn["d-3"],
423 ];
424
425 $this->assertSame($expected, $testAgentCollection->getNamedObjectives($config));
426 }
427
428 public function testGetNamedObjectives(): void
429 {
430 $refinery = $this->createMock(Refinery::class);
431 $config = new Setup\ConfigCollection([]);
432
433 $aAgent = $this->newAgent();
434 $bAgent = $this->newAgent();
435
436 $aReturn = [
437 "a-1" => $this->newObjectiveConstructor(),
438 "a-2" => $this->newObjectiveConstructor()
439 ];
440
441 $bReturn = [
442 "b-1" => $this->newObjectiveConstructor(),
443 "b-2" => $this->newObjectiveConstructor(),
444 ];
445
446 $aAgent
447 ->expects($this->once())
448 ->method("getNamedObjectives")
449 ->willReturn($aReturn);
450 $bAgent
451 ->expects($this->once())
452 ->method("getNamedObjectives")
453 ->willReturn($bReturn);
454
455 $testAgentCollection = new Setup\AgentCollection(
456 $refinery,
457 ["aAgent" => $aAgent, "bAgent" => $bAgent]
458 );
459
460 $result = $testAgentCollection->getNamedObjectives($config);
461
462 $this->assertSame($aReturn["a-1"], $result["aAgent.a-1"]);
463 $this->assertSame($aReturn["a-2"], $result["aAgent.a-2"]);
464 $this->assertSame($bReturn["b-1"], $result["bAgent.b-1"]);
465 $this->assertSame($bReturn["b-2"], $result["bAgent.b-2"]);
466 }
467
469 {
470 $refinery = $this->createMock(Refinery::class);
471 $agent = $this->newAgent();
472
473 $seen_config = null;
474 $agent
475 ->method("getNamedObjectives")
476 ->will($this->returnCallback(function ($config) use (&$seen_config) {
477 $seen_config = $config;
478 return [];
479 }));
480
481 $collection = new Setup\AgentCollection(
482 $refinery,
483 ["agent" => $agent]
484 );
485
486 $agent_config = $this->createMock(Setup\Config::class);
487 $config = new Setup\ConfigCollection(
488 ["agent" => $agent_config]
489 );
490
491 $result = $collection->getNamedObjectives($config);
492
493 $this->assertSame($agent_config, $seen_config);
494 }
495
496 public function testGetAgents(): void
497 {
498 $refinery = $this->createMock(Refinery::class);
499
500 $c1 = $this->newAgent();
501 $c2 = $this->newAgent();
502 $c3 = $this->newAgent();
503 $c4 = $this->newAgent();
504
505 $agentCollection = new Setup\AgentCollection(
506 $refinery,
507 ["c1" => $c1, "c2" => $c2, "c3" => $c3, "c4" => $c4]
508 );
509
510 $this->assertEquals([
511 "c1" => $c1,
512 "c2" => $c2,
513 "c3" => $c3,
514 "c4" => $c4
515 ], $agentCollection->getAgents());
516 }
517}
Builds data types.
Definition: Factory.php:36
An agent that is just a collection of some other agents.
A collection of some configurations.
$c
Definition: deliver.php:25
A transformation is a function from one datatype to another.
This is what a factory for input fields looks like.
Definition: Factory.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...