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