ILIAS  release_8 Revision v8.25
CollectionSortingTest.php
Go to the documentation of this file.
1<?php
2
20
31
37{
38 public const DUMMY_RCID = 'dummy-rcid';
39
50 private Sorter $sorter;
54
55 protected function setUp(): void
56 {
57 parent::setUp();
58 $this->rcid_generator = new DummyIDGenerator(self::DUMMY_RCID);
59 $this->resource_builder = $this->createMock(ResourceBuilder::class);
60 $this->collection_builder = $this->createMock(CollectionBuilder::class);
61 $this->rcid = new ResourceCollectionIdentification(self::DUMMY_RCID);
62 $this->collection = new ResourceCollection(
63 $this->rcid,
65 ''
66 );
67 $this->rid_one = new ResourceIdentification('rid_one');
68 $this->resource_one = $this->createMock(StorableFileResource::class);
69 $this->revision_one = $this->createMock(Revision::class);
70
71 $this->rid_two = new ResourceIdentification('rid_two');
72 $this->resource_two = $this->createMock(StorableFileResource::class);
73 $this->revision_two = $this->createMock(Revision::class);
74
75 $this->rid_three = new ResourceIdentification('rid_three');
76 $this->resource_three = $this->createMock(StorableFileResource::class);
77 $this->revision_three = $this->createMock(Revision::class);
78
79 $this->sorter = new Sorter(
80 $this->resource_builder,
81 $this->collection_builder,
82 $this->collection
83 );
84
85 // RESOURCES
86 $this->collection->add($this->rid_one);
87 $this->collection->add($this->rid_two);
88 $this->collection->add($this->rid_three);
89
90 // EXPECTATIONS
91 $this->resource_one->expects($this->atLeastOnce())
92 ->method('getCurrentRevision')
93 ->willReturn($this->revision_one);
94
95 $this->resource_two->expects($this->atLeastOnce())
96 ->method('getCurrentRevision')
97 ->willReturn($this->revision_two);
98
99 $this->resource_three->expects($this->atLeastOnce())
100 ->method('getCurrentRevision')
101 ->willReturn($this->revision_three);
102
103 $this->resource_builder->expects($this->atLeastOnce())
104 ->method('get')
105 ->withConsecutive([$this->rid_one], [$this->rid_two], [$this->rid_two], [$this->rid_three])
106 ->will(
107 $this->onConsecutiveCalls(
108 $this->resource_one,
109 $this->resource_two,
110 $this->resource_two,
111 $this->resource_three
112 )
113 );
114 }
115
117 {
118 $this->revision_one->expects($this->atLeastOnce())
119 ->method('getInformation')
120 ->willReturn($one);
121 $this->revision_two->expects($this->atLeastOnce())
122 ->method('getInformation')
123 ->willReturn($two);
124 $this->revision_three->expects($this->atLeastOnce())
125 ->method('getInformation')
126 ->willReturn($three);
127 }
128
129 public function testBySizeDescSorting(): void
130 {
131 // SORTING
133 (new FileInformation())->setSize(10),
134 (new FileInformation())->setSize(20),
135 (new FileInformation())->setSize(30)
136 );
137 $sorted_collection = $this->sorter->desc()->bySize();
138 $this->assertEquals(
139 [
140 $this->rid_three->serialize(),
141 $this->rid_two->serialize(),
142 $this->rid_one->serialize()
143 ],
144 $this->getFlatOrder($sorted_collection)
145 );
146 }
147
148 public function testBySizeAscSorting(): void
149 {
150 // SORTING
152 (new FileInformation())->setSize(10),
153 (new FileInformation())->setSize(20),
154 (new FileInformation())->setSize(30)
155 );
156 $sorted_collection = $this->sorter->asc()->bySize();
157 $this->assertEquals(
158 [
159 $this->rid_one->serialize(),
160 $this->rid_two->serialize(),
161 $this->rid_three->serialize(),
162 ],
163 $this->getFlatOrder($sorted_collection)
164 );
165 }
166
167 public function testBySizeDefaultSorting(): void
168 {
169 // SORTING
171 (new FileInformation())->setSize(10),
172 (new FileInformation())->setSize(20),
173 (new FileInformation())->setSize(30)
174 );
175 $sorted_collection = $this->sorter->bySize();
176 $this->assertEquals(
177 [
178 $this->rid_one->serialize(),
179 $this->rid_two->serialize(),
180 $this->rid_three->serialize(),
181 ],
182 $this->getFlatOrder($sorted_collection)
183 );
184 }
185
186 public function testByCreationDateDefaultSorting(): void
187 {
188 // SORTING
190 (new FileInformation())->setCreationDate(new \DateTimeImmutable('2020-01-01')),
191 (new FileInformation())->setCreationDate(new \DateTimeImmutable('2020-02-02')),
192 (new FileInformation())->setCreationDate(new \DateTimeImmutable('2020-03-03'))
193 );
194 $sorted_collection = $this->sorter->byCreationDate();
195 $this->assertEquals(
196 [
197 $this->rid_one->serialize(),
198 $this->rid_two->serialize(),
199 $this->rid_three->serialize(),
200 ],
201 $this->getFlatOrder($sorted_collection)
202 );
203 }
204
205 public function testByCreationDateAscSorting(): void
206 {
207 // SORTING
209 (new FileInformation())->setCreationDate(new \DateTimeImmutable('2020-01-01')),
210 (new FileInformation())->setCreationDate(new \DateTimeImmutable('2020-02-02')),
211 (new FileInformation())->setCreationDate(new \DateTimeImmutable('2020-03-03'))
212 );
213 $sorted_collection = $this->sorter->asc()->byCreationDate();
214 $this->assertEquals(
215 [
216 $this->rid_one->serialize(),
217 $this->rid_two->serialize(),
218 $this->rid_three->serialize(),
219 ],
220 $this->getFlatOrder($sorted_collection)
221 );
222 }
223
224 public function testByCreationDateDescSorting(): void
225 {
226 // SORTING
228 (new FileInformation())->setCreationDate(new \DateTimeImmutable('2020-01-01')),
229 (new FileInformation())->setCreationDate(new \DateTimeImmutable('2020-02-02')),
230 (new FileInformation())->setCreationDate(new \DateTimeImmutable('2020-03-03'))
231 );
232 $sorted_collection = $this->sorter->desc()->byCreationDate();
233 $this->assertEquals(
234 [
235 $this->rid_three->serialize(),
236 $this->rid_two->serialize(),
237 $this->rid_one->serialize(),
238 ],
239 $this->getFlatOrder($sorted_collection)
240 );
241 }
242
243 public function testByTitleDefaultSorting(): void
244 {
245 // SORTING
247 (new FileInformation())->setTitle('1_one.jpg'),
248 (new FileInformation())->setTitle('2_two.jpg'),
249 (new FileInformation())->setTitle('3_three.jpg')
250 );
251 $sorted_collection = $this->sorter->byTitle();
252 $this->assertEquals(
253 [
254 $this->rid_one->serialize(),
255 $this->rid_two->serialize(),
256 $this->rid_three->serialize(),
257 ],
258 $this->getFlatOrder($sorted_collection)
259 );
260 }
261
262 public function testByTitleAscSorting(): void
263 {
264 // SORTING
266 (new FileInformation())->setTitle('1_one.jpg'),
267 (new FileInformation())->setTitle('2_two.jpg'),
268 (new FileInformation())->setTitle('3_three.jpg')
269 );
270 $sorted_collection = $this->sorter->asc()->byTitle();
271 $this->assertEquals(
272 [
273 $this->rid_one->serialize(),
274 $this->rid_two->serialize(),
275 $this->rid_three->serialize(),
276 ],
277 $this->getFlatOrder($sorted_collection)
278 );
279 }
280
281 public function testByTitleDescSorting(): void
282 {
283 // SORTING
285 (new FileInformation())->setTitle('1_one.jpg'),
286 (new FileInformation())->setTitle('2_two.jpg'),
287 (new FileInformation())->setTitle('3_three.jpg')
288 );
289 $sorted_collection = $this->sorter->desc()->byTitle();
290 $this->assertEquals(
291 [
292 $this->rid_three->serialize(),
293 $this->rid_two->serialize(),
294 $this->rid_one->serialize(),
295 ],
296 $this->getFlatOrder($sorted_collection)
297 );
298 }
299
301 {
302 return array_map(
303 fn (ResourceIdentification $rid): string => $rid->serialize(),
305 );
306 }
307}
setUpRevisionExpectations(FileInformation $one, FileInformation $two, FileInformation $three)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...