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