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