ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilBuddySystemRelationCollectionTest.php
Go to the documentation of this file.
1 <?php
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 
21  public function setUp()
22  {
23  }
24 
29  public function testElementsCanBeInitiallyAdded($elements)
30  {
31  $collection = new ilBuddySystemRelationCollection($elements);
32 
33  $this->assertFalse($collection->isEmpty());
34  $this->assertSame($elements, $collection->toArray());
35  $this->assertSame(array_values($elements), $collection->getValues());
36  $this->assertSame(array_keys($elements), $collection->getKeys());
37 
38  foreach ($elements as $key => $elm) {
39  $this->assertArrayHasKey($collection->getKey($elm), $elements);
40  $this->assertTrue(isset($collection[$key]));
41  $this->assertEquals($collection[$key], $elm);
42  }
43  }
44 
49  public function testElementsCanBeAddedAndRemoved($elements)
50  {
51  $collection = new ilBuddySystemRelationCollection();
52  $this->assertTrue($collection->isEmpty());
53 
54  foreach ($elements as $elm) {
55  $collection->add($elm);
56  $this->assertTrue($collection->contains($elm));
57  }
58 
59  foreach ($elements as $elm) {
60  $collection->removeElement($elm);
61  $this->assertFalse($collection->contains($elm));
62  }
63 
64  $this->assertTrue($collection->isEmpty());
65 
66  foreach ($elements as $elm) {
67  $collection->add($elm);
68  $this->assertTrue($collection->contains($elm));
69  }
70 
71  foreach ($elements as $elm) {
72  $key = $collection->getKey($elm);
73  $collection->remove($key);
74  $this->assertFalse($collection->contains($elm));
75  }
76 
77  $this->assertTrue($collection->isEmpty());
78 
79  foreach ($elements as $key => $elm) {
80  $collection[$key] = $elm;
81  $this->assertTrue($collection->contains($elm));
82  }
83 
84  foreach ($elements as $key => $elm) {
85  unset($collection[$key]);
86  $this->assertFalse($collection->contains($elm));
87  }
88 
89  $this->assertTrue($collection->isEmpty());
90 
91  $collection[] = 5;
92 
93  $data = $collection->toArray();
94  $this->assertSame(5, reset($data));
95  }
96 
101  public function testIterator($elements)
102  {
103  $collection = new ilBuddySystemRelationCollection($elements);
104  $iterations = 0;
105  foreach ($collection->getIterator() as $key => $item) {
106  $this->assertSame($elements[$key], $item, "Item {$key} not match");
107  $iterations++;
108  }
109  $this->assertCount($iterations, $elements, "Number of iterations not match");
110  }
111 
116  {
117  $this->assertException(InvalidArgumentException::class);
118  $collection = new ilBuddySystemRelationCollection();
119  $collection->removeElement(5);
120  }
121 
126  {
127  $this->assertException(InvalidArgumentException::class);
128  $collection = new ilBuddySystemRelationCollection();
129  $collection->remove("phpunit");
130  }
131 
135  public function testElementsCanBeSliced()
136  {
137  $collection = new ilBuddySystemRelationCollection();
138  $collection->add(1);
139  $collection->add(2);
140  $collection->add(3);
141  $collection->add(4);
142 
143  $this->assertCount(2, $collection->filter(function ($elm) {
144  return $elm % 2 === 0;
145  })->toArray());
146  }
147 
151  public function testElementsCanBeFiltered()
152  {
153  $collection = new ilBuddySystemRelationCollection();
154  $collection->add(1);
155  $collection->add(2);
156  $collection->add(3);
157  $collection->add(4);
158 
159  $this->assertSame(array(3), $collection->slice(2, 1)->getValues());
160  }
161 
165  public function provideElements()
166  {
167  $relation_1 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
168  $relation_2 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
169  $relation_3 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
170  $relation_4 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
171  $relation_5 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
172 
173  return array(
174  'indexed' => array(array(0, 1, 2, 3, 4, 5)),
175  'associative' => array(array('A' => 'a', 'B' => 'b', 'C' => 'c')),
176  'mixed' => array(array(0, 'A' => 'a', 1, 'B' => 'b', 2, 3)),
177  'relations' => array(array($relation_1, $relation_2, $relation_3, $relation_4, $relation_5))
178  );
179  }
180 }
testRemovingAnNonExistingElementByKeyRaisesAnException()
InvalidArgumentException
Class ilBuddySystemRelationCollectionTest.
assertException($exception_class)
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list...
testRemovingAnNonExistingElementRaisesAnException()
InvalidArgumentException
toArray($value)
Wrap the given value in an array if it is no array.
$key
Definition: croninfo.php:18
$data
Definition: bench.php:6