ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
40  $this->assertArrayHasKey($collection->getKey($elm), $elements);
41  $this->assertTrue(isset($collection[$key]));
42  $this->assertEquals($collection[$key], $elm);
43  }
44  }
45 
50  public function testElementsCanBeAddedAndRemoved($elements)
51  {
52  $collection = new ilBuddySystemRelationCollection();
53  $this->assertTrue($collection->isEmpty());
54 
55  foreach($elements as $elm)
56  {
57  $collection->add($elm);
58  $this->assertTrue($collection->contains($elm));
59  }
60 
61  foreach($elements as $elm)
62  {
63  $collection->removeElement($elm);
64  $this->assertFalse($collection->contains($elm));
65  }
66 
67  $this->assertTrue($collection->isEmpty());
68 
69  foreach($elements as $elm)
70  {
71  $collection->add($elm);
72  $this->assertTrue($collection->contains($elm));
73  }
74 
75  foreach($elements as $elm)
76  {
77  $key = $collection->getKey($elm);
78  $collection->remove($key);
79  $this->assertFalse($collection->contains($elm));
80  }
81 
82  $this->assertTrue($collection->isEmpty());
83 
84  foreach($elements as $key => $elm)
85  {
86  $collection[$key] = $elm;
87  $this->assertTrue($collection->contains($elm));
88  }
89 
90  foreach($elements as $key => $elm)
91  {
92  unset($collection[$key]);
93  $this->assertFalse($collection->contains($elm));
94  }
95 
96  $this->assertTrue($collection->isEmpty());
97 
98  $collection[] = 5;
99 
100  $data = $collection->toArray();
101  $this->assertSame(5, reset($data));
102  }
103 
108  public function testIterator($elements)
109  {
110  $collection = new ilBuddySystemRelationCollection($elements);
111  $iterations = 0;
112  foreach($collection->getIterator() as $key => $item)
113  {
114  $this->assertSame($elements[$key], $item, "Item {$key} not match");
115  $iterations++;
116  }
117  $this->assertCount($iterations, $elements, "Number of iterations not match");
118  }
119 
124  {
125  $this->assertException(InvalidArgumentException::class);
126  $collection = new ilBuddySystemRelationCollection();
127  $collection->removeElement(5);
128  }
129 
134  {
135  $this->assertException(InvalidArgumentException::class);
136  $collection = new ilBuddySystemRelationCollection();
137  $collection->remove("phpunit");
138  }
139 
143  public function testElementsCanBeSliced()
144  {
145  $collection = new ilBuddySystemRelationCollection();
146  $collection->add(1);
147  $collection->add(2);
148  $collection->add(3);
149  $collection->add(4);
150 
151  $this->assertCount(2, $collection->filter(function($elm) {
152  return $elm % 2 === 0;
153  })->toArray());
154  }
155 
159  public function testElementsCanBeFiltered()
160  {
161  $collection = new ilBuddySystemRelationCollection();
162  $collection->add(1);
163  $collection->add(2);
164  $collection->add(3);
165  $collection->add(4);
166 
167  $this->assertSame(array(3), $collection->slice(2, 1)->getValues());
168  }
169 
173  public function provideElements()
174  {
175  $relation_1 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
176  $relation_2 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
177  $relation_3 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
178  $relation_4 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
179  $relation_5 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
180 
181  return array(
182  'indexed' => array(array(0, 1, 2, 3, 4, 5)),
183  'associative' => array(array('A' => 'a', 'B' => 'b', 'C' => 'c')),
184  'mixed' => array(array(0, 'A' => 'a', 1, 'B' => 'b', 2, 3)),
185  'relations' => array(array($relation_1, $relation_2, $relation_3, $relation_4, $relation_5))
186  );
187  }
188 }
testRemovingAnNonExistingElementByKeyRaisesAnException()
InvalidArgumentException
Class ilBuddySystemRelationCollectionTest.
assertException($exception_class)
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list...
Create styles array
The data for the language used.
testRemovingAnNonExistingElementRaisesAnException()
InvalidArgumentException