ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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  $consecutive = [
105  ];
106 
107  $map = [
112  ];
113  $this->resource_builder->expects($this->atLeastOnce())
114  ->method('get')
115  ->will($this->returnValueMap($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->assertEquals(
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->assertEquals(
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->assertEquals(
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->assertEquals(
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->assertEquals(
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->assertEquals(
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->assertEquals(
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->assertEquals(
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->assertEquals(
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...