ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
10 {
14  protected $backupGlobals = false;
15 
19  public function setUp()
20  {
21  }
22 
27  public function testElementsCanBeInitiallyAdded($elements)
28  {
29  $collection = new ilBuddySystemRelationCollection($elements);
30 
31  $this->assertFalse($collection->isEmpty());
32  $this->assertSame($elements, $collection->toArray());
33  $this->assertSame(array_values($elements), $collection->getValues());
34  $this->assertSame(array_keys($elements), $collection->getKeys());
35 
36  foreach($elements as $key => $elm)
37  {
38  $this->assertArrayHasKey($collection->getKey($elm), $elements);
39  $this->assertTrue(isset($collection[$key]));
40  $this->assertEquals($collection[$key], $elm);
41  }
42  }
43 
48  public function testElementsCanBeAddedAndRemoved($elements)
49  {
50  $collection = new ilBuddySystemRelationCollection();
51  $this->assertTrue($collection->isEmpty());
52 
53  foreach($elements as $elm)
54  {
55  $collection->add($elm);
56  $this->assertTrue($collection->contains($elm));
57  }
58 
59  foreach($elements as $elm)
60  {
61  $collection->removeElement($elm);
62  $this->assertFalse($collection->contains($elm));
63  }
64 
65  $this->assertTrue($collection->isEmpty());
66 
67  foreach($elements as $elm)
68  {
69  $collection->add($elm);
70  $this->assertTrue($collection->contains($elm));
71  }
72 
73  foreach($elements as $elm)
74  {
75  $key = $collection->getKey($elm);
76  $collection->remove($key);
77  $this->assertFalse($collection->contains($elm));
78  }
79 
80  $this->assertTrue($collection->isEmpty());
81 
82  foreach($elements as $key => $elm)
83  {
84  $collection[$key] = $elm;
85  $this->assertTrue($collection->contains($elm));
86  }
87 
88  foreach($elements as $key => $elm)
89  {
90  unset($collection[$key]);
91  $this->assertFalse($collection->contains($elm));
92  }
93 
94  $this->assertTrue($collection->isEmpty());
95 
96  $collection[] = 5;
97 
98  $data = $collection->toArray();
99  $this->assertSame(5, reset($data));
100  }
101 
106  public function testIterator($elements)
107  {
108  $collection = new ilBuddySystemRelationCollection($elements);
109  $iterations = 0;
110  foreach($collection->getIterator() as $key => $item)
111  {
112  $this->assertSame($elements[$key], $item, "Item {$key} not match");
113  $iterations++;
114  }
115  $this->assertCount($iterations, $elements, "Number of iterations not match");
116  }
117 
122  {
123  $collection = new ilBuddySystemRelationCollection();
124  $collection->removeElement(5);
125  }
126 
131  {
132  $collection = new ilBuddySystemRelationCollection();
133  $collection->remove("phpunit");
134  }
135 
139  public function testElementsCanBeSliced()
140  {
141  $collection = new ilBuddySystemRelationCollection();
142  $collection->add(1);
143  $collection->add(2);
144  $collection->add(3);
145  $collection->add(4);
146 
147  $this->assertCount(2, $collection->filter(function($elm) {
148  return $elm % 2 === 0;
149  })->toArray());
150  }
151 
155  public function testElementsCanBeFiltered()
156  {
157  $collection = new ilBuddySystemRelationCollection();
158  $collection->add(1);
159  $collection->add(2);
160  $collection->add(3);
161  $collection->add(4);
162 
163  $this->assertSame(array(3), $collection->slice(2, 1)->getValues());
164  }
165 
169  public function provideElements()
170  {
171  $relation_1 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
172  $relation_2 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
173  $relation_3 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
174  $relation_4 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
175  $relation_5 = $this->getMockBuilder('ilBuddySystemRelation')->disableOriginalConstructor()->getMock();
176 
177  return array(
178  'indexed' => array(array(0, 1, 2, 3, 4, 5)),
179  'associative' => array(array('A' => 'a', 'B' => 'b', 'C' => 'c')),
180  'mixed' => array(array(0, 'A' => 'a', 1, 'B' => 'b', 2, 3)),
181  'relations' => array(array($relation_1, $relation_2, $relation_3, $relation_4, $relation_5))
182  );
183  }
184 }
testRemovingAnNonExistingElementByKeyRaisesAnException()
InvalidArgumentException
Class ilBuddySystemRelationCollectionTest.
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list...
$data
testRemovingAnNonExistingElementRaisesAnException()
InvalidArgumentException