ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
MapTest.php
Go to the documentation of this file.
1<?php
2
4
13use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
14use PHPUnit\Framework\TestCase;
16
17// require_once('./libs/composer/vendor/autoload.php');
18
23class MapTest extends TestCase
24{
25 use MockeryPHPUnitIntegration;
26
30 protected $identification;
34 protected $factory;
38 protected $provider;
39
43 protected function setUp() : void
44 {
45 parent::setUp();
46
47 $this->provider = $this->getDummyProvider();
48 $this->identification = new IdentificationFactory(new NullProviderFactory());
49 $this->factory = new MainMenuItemFactory();
50 }
51
52 private function getId(string $id) : IdentificationInterface
53 {
54 return $this->identification->core($this->provider)->identifier($id);
55 }
56
57 public function testAddItem() : void
58 {
59 $map = new Map($this->factory);
60
61 $p1 = $this->getId('parent_1');
62 $p2 = $this->getId('parent_2');
63 $p3 = $this->getId('parent_3');
64 $map->addMultiple(
65 ...[
66 $this->factory->topParentItem($p1),
67 $this->factory->topParentItem($p2),
68 $this->factory->topParentItem($p3),
69 ]
70 );
71
72 $p4 = $this->getId('parent_4');
73 $map->add($this->factory->topParentItem($p4));
74
75 $this->assertTrue($map->has());
76 $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 4);
77 $this->assertTrue($map->existsInFilter($p1));
78 $this->assertTrue($map->existsInFilter($p2));
79 $this->assertTrue($map->existsInFilter($p3));
80 $this->assertTrue($map->existsInFilter($p4));
81 }
82
83 public function testFilterItems() : void
84 {
85 $map = new Map($this->factory);
86
87 $p1 = $this->getId('parent_1');
88 $p2 = $this->getId('parent_2');
89 $p3 = $this->getId('parent_3');
90 $p4 = $this->getId('parent_4');
91 $map->addMultiple(
92 ...[
93 $this->factory->topParentItem($p1),
94 $this->factory->topParentItem($p2),
95 $this->factory->topParentItem($p3),
96 $this->factory->topParentItem($p4)
97 ]
98 );
99
100 $this->assertTrue($map->has());
101 $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 4);
102
103 $map->filter(static function () {
104 return true;
105 });
106
107 $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 4);
108
109 $map->filter(static function (isItem $i) {
110 return $i->getProviderIdentification()->getInternalIdentifier() !== 'parent_1';
111 });
112
113 $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 3);
114 $this->assertFalse($map->existsInFilter($p1));
115 $this->assertTrue($map->existsInFilter($p2));
116 $this->assertTrue($map->existsInFilter($p3));
117 $this->assertTrue($map->existsInFilter($p4));
118
119 $map->filter(static function () {
120 return false;
121 });
122 $this->assertFalse($map->existsInFilter($p1));
123 $this->assertFalse($map->existsInFilter($p2));
124 $this->assertFalse($map->existsInFilter($p3));
125 $this->assertFalse($map->existsInFilter($p4));
126
127 }
128
129 public function testSortingTopItems() : void
130 {
131 $map = new Map($this->factory);
132
133 for ($x = 1; $x <= 10; $x++) {
134 $map->add($this->factory->topParentItem($this->getId((string) $x))->withPosition(11 - $x));
135 }
136
137 $x = 10;
138 foreach ($map->getAllFromFilter() as $i) {
139 $this->assertSame($i->getPosition(), $x);
140 $x--;
141 }
142
143 $map->sort();
144
145 $generator = $map->getAllFromFilter();
146
147 $one = static function () use ($generator): isItem {
148 $i = $generator->current();
149 $generator->next();
150 return $i;
151 };
152
153 $i = $one();
154 $this->assertSame('10', $i->getProviderIdentification()->getInternalIdentifier());
155 $this->assertSame(1, $i->getPosition());
156
157 $i = $one();
158 $this->assertSame('9', $i->getProviderIdentification()->getInternalIdentifier());
159 $this->assertSame(2, $i->getPosition());
160
161 $i = $one();
162 $this->assertSame('8', $i->getProviderIdentification()->getInternalIdentifier());
163 $this->assertSame(3, $i->getPosition());
164
165 $i = $one();
166 $this->assertSame('7', $i->getProviderIdentification()->getInternalIdentifier());
167 $this->assertSame(4, $i->getPosition());
168
169 $i = $one();
170 $this->assertSame('6', $i->getProviderIdentification()->getInternalIdentifier());
171 $this->assertSame(5, $i->getPosition());
172
173 $i = $one();
174 $this->assertSame('5', $i->getProviderIdentification()->getInternalIdentifier());
175 $this->assertSame(6, $i->getPosition());
176
177 $i = $one();
178 $this->assertSame('4', $i->getProviderIdentification()->getInternalIdentifier());
179 $this->assertSame(7, $i->getPosition());
180
181 $i = $one();
182 $this->assertSame('3', $i->getProviderIdentification()->getInternalIdentifier());
183 $this->assertSame(8, $i->getPosition());
184
185 $i = $one();
186 $this->assertSame('2', $i->getProviderIdentification()->getInternalIdentifier());
187 $this->assertSame(9, $i->getPosition());
188
189 $i = $one();
190 $this->assertSame('1', $i->getProviderIdentification()->getInternalIdentifier());
191 $this->assertSame(10, $i->getPosition());
192
193 }
194
195 public function testSortingNestedItems()
196 {
197 $map = new Map($this->factory);
199 $tp_1 = $this->factory->topParentItem($this->getId('tp_1'))
200 ->withPosition(1);
201 $tp_1_1 = $this->factory->link($this->getId('tp_1_1'))
202 ->withParent($tp_1->getProviderIdentification())
203 ->withPosition(1);
204 $tp_1_2 = $this->factory->link($this->getId('tp_1_2'))
205 ->withParent($tp_1->getProviderIdentification())
206 ->withPosition(2);
207 $tp_1 = $tp_1->withChildren([
208 $tp_1_2,
209 $tp_1_1,
210 ]);
211 $map->add($tp_1);
212 $map->add($tp_1_2);
213 $map->add($tp_1_1);
214
215 $tp_2 = $this->factory->topParentItem($this->getId('tp_2'))
216 ->withPosition(2);
217 $tp_2_1 = $this->factory->link($this->getId('tp_2_1'))
218 ->withParent($tp_2->getProviderIdentification())
219 ->withPosition(1);
220 $tp_2_2 = $this->factory->link($this->getId('tp_2_2'))
221 ->withParent($tp_2->getProviderIdentification())
222 ->withPosition(2);
223 $tp_2 = $tp_2->withChildren([
224 $tp_2_2,
225 $tp_2_1,
226 ]);
227 $map->add($tp_2);
228 $map->add($tp_2_1);
229 $map->add($tp_2_2);
230
231 $map->sort();
232
233 $this->assertEquals(1, $map->getSingleItemFromRaw($this->getId('tp_1'))->getPosition());
234 $this->assertEquals(1, $map->getSingleItemFromRaw($this->getId('tp_1_1'))->getPosition());
235 $this->assertEquals(2, $map->getSingleItemFromRaw($this->getId('tp_1_2'))->getPosition());
236 $this->assertEquals(2, $map->getSingleItemFromRaw($this->getId('tp_2'))->getPosition());
237 $this->assertEquals(1, $map->getSingleItemFromRaw($this->getId('tp_2_1'))->getPosition());
238 $this->assertEquals(2, $map->getSingleItemFromRaw($this->getId('tp_2_2'))->getPosition());
239
240 // check position in parent
241 $get_tp_1 = $map->getSingleItemFromFilter($this->getId('tp_1'));
242 $children_of_tp_1 = $get_tp_1->getChildren();
243 $first = reset($children_of_tp_1);
244 $this->assertEquals($tp_1_1, $first);
245 $this->assertEquals(1, $first->getPosition());
246
247 $get_tp_2 = $map->getSingleItemFromFilter($this->getId('tp_2'));
248 $children_of_tp_2 = $get_tp_2->getChildren();
249 $first = reset($children_of_tp_2);
250 $this->assertEquals($tp_2_1, $first);
251 $this->assertEquals(1, $first->getPosition());
252 }
253
254 public function testSamePositionResolution()
255 {
256 $map = new Map($this->factory);
258 $tp_1 = $this->factory->topParentItem($this->getId('tp_1'))
259 ->withPosition(1);
260 $tp_1_1 = $this->factory->link($this->getId('tp_1_1'))
261 ->withParent($tp_1->getProviderIdentification())
262 ->withPosition(1);
263 $tp_1_2 = $this->factory->link($this->getId('tp_1_2'))
264 ->withParent($tp_1->getProviderIdentification())
265 ->withPosition(1);
266 $tp_1 = $tp_1->withChildren([
267 $tp_1_2,
268 $tp_1_1,
269 ]);
270 $map->add($tp_1);
271 $map->add($tp_1_2);
272 $map->add($tp_1_1);
273 $map->sort();
274 $item = $map->getSingleItemFromFilter($this->getId('tp_1'));
275 $this->assertEquals(2, count($item->getChildren()));
276 }
277
278 private function getDummyProvider()
279 {
280 return new class implements StaticMainMenuProvider {
281 public function getAllIdentifications() : array
282 {
283 return [];
284 }
285
286 public function getFullyQualifiedClassName() : string
287 {
288 return 'Provider';
289 }
290
291 public function getProviderNameForPresentation() : string
292 {
293 return 'Provider';
294 }
295
296 public function getStaticTopItems() : array
297 {
298 return [];
299 }
300
301 public function getStaticSubItems() : array
302 {
303 return [];
304 }
305
306 public function provideTypeInformation() : TypeInformationCollection
307 {
308 return new TypeInformationCollection();
309 }
310 };
311 }
312}
An exception for terminatinating execution or to throw for unit testing.
Class FactoryImplTest.
Definition: MapTest.php:24
$i
Definition: metadata.php:24