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