ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
ilBuddySystemRelationCollectionTest.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 /* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Contact/BuddySystem/test/ilBuddySystemBaseTest.php';
5 
12 {
16  protected $backupGlobals = false;
17 
22  public function testElementsCanBeInitiallyAdded(array $elements) : void
23  {
24  $collection = new ilBuddySystemRelationCollection($elements);
25 
26  $this->assertFalse($collection->isEmpty());
27  $this->assertSame($elements, $collection->toArray());
28  $this->assertSame(array_values($elements), $collection->getValues());
29  $this->assertSame(array_keys($elements), $collection->getKeys());
30 
31  foreach ($elements as $key => $elm) {
32  $this->assertArrayHasKey($collection->getKey($elm), $elements);
33  $this->assertTrue(isset($collection[$key]));
34  $this->assertEquals($collection[$key], $elm);
35  }
36  }
37 
42  public function testElementsCanBeAddedAndRemoved(array $elements) : void
43  {
44  $collection = new ilBuddySystemRelationCollection();
45  $this->assertTrue($collection->isEmpty());
46 
47  foreach ($elements as $elm) {
48  $collection->add($elm);
49  $this->assertTrue($collection->contains($elm));
50  }
51 
52  foreach ($elements as $elm) {
53  $collection->removeElement($elm);
54  $this->assertFalse($collection->contains($elm));
55  }
56 
57  $this->assertTrue($collection->isEmpty());
58 
59  foreach ($elements as $elm) {
60  $collection->add($elm);
61  $this->assertTrue($collection->contains($elm));
62  }
63 
64  foreach ($elements as $elm) {
65  $key = $collection->getKey($elm);
66  $collection->remove($key);
67  $this->assertFalse($collection->contains($elm));
68  }
69 
70  $this->assertTrue($collection->isEmpty());
71 
72  foreach ($elements as $key => $elm) {
73  $collection[$key] = $elm;
74  $this->assertTrue($collection->contains($elm));
75  }
76 
77  foreach ($elements as $key => $elm) {
78  unset($collection[$key]);
79  $this->assertFalse($collection->contains($elm));
80  }
81 
82  $this->assertTrue($collection->isEmpty());
83 
84  $collection[] = 5;
85 
86  $data = $collection->toArray();
87  $this->assertSame(5, reset($data));
88  }
89 
94  public function testIterator(array $elements) : void
95  {
96  $collection = new ilBuddySystemRelationCollection($elements);
97  $iterations = 0;
98  foreach ($collection->getIterator() as $key => $item) {
99  $this->assertSame($elements[$key], $item, "Item {$key} not match");
100  $iterations++;
101  }
102  $this->assertCount($iterations, $elements, 'Number of iterations not match');
103  }
104 
109  {
110  $this->expectException(InvalidArgumentException::class);
111  $collection = new ilBuddySystemRelationCollection();
112  $collection->removeElement(5);
113  }
114 
119  {
120  $this->expectException(InvalidArgumentException::class);
121  $collection = new ilBuddySystemRelationCollection();
122  $collection->remove('phpunit');
123  }
124 
128  public function testElementsCanBeSliced() : void
129  {
130  $collection = new ilBuddySystemRelationCollection();
131  $collection->add(1);
132  $collection->add(2);
133  $collection->add(3);
134  $collection->add(4);
135 
136  $this->assertCount(2, $collection->filter(function ($elm) {
137  return $elm % 2 === 0;
138  })->toArray());
139  }
140 
144  public function testElementsCanBeFiltered() : void
145  {
146  $collection = new ilBuddySystemRelationCollection();
147  $collection->add(1);
148  $collection->add(2);
149  $collection->add(3);
150  $collection->add(4);
151 
152  $this->assertSame([3], $collection->slice(2, 1)->getValues());
153  }
154 
158  public function provideElements() : array
159  {
160  $relation1 = $this->getMockBuilder(ilBuddySystemRelation::class)->disableOriginalConstructor()->getMock();
161  $relation2 = $this->getMockBuilder(ilBuddySystemRelation::class)->disableOriginalConstructor()->getMock();
162  $relation3 = $this->getMockBuilder(ilBuddySystemRelation::class)->disableOriginalConstructor()->getMock();
163  $relation4 = $this->getMockBuilder(ilBuddySystemRelation::class)->disableOriginalConstructor()->getMock();
164  $relation5 = $this->getMockBuilder(ilBuddySystemRelation::class)->disableOriginalConstructor()->getMock();
165 
166  return [
167  'indexed' => [[0, 1, 2, 3, 4, 5]],
168  'associative' => [['A' => 'a', 'B' => 'b', 'C' => 'c']],
169  'mixed' => [[0, 'A' => 'a', 1, 'B' => 'b', 2, 3]],
170  'relations' => [[$relation1, $relation2, $relation3, $relation4, $relation5]]
171  ];
172  }
173 }
$data
Definition: storeScorm.php:23
Class ilBuddySystemRelationCollectionTest.
testElementsCanBeAddedAndRemoved(array $elements)
provideElements
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list...
testElementsCanBeInitiallyAdded(array $elements)
provideElements
toArray($value)
Wrap the given value in an array if it is no array.