ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
FinderTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
10 
11 /******************************************************************************
12  *
13  * This file is part of ILIAS, a powerful learning management system.
14  *
15  * ILIAS is licensed with the GPL-3.0, you should have received a copy
16  * of said license along with the source code.
17  *
18  * If this is not the case or you just want to try ILIAS, you'll find
19  * us at:
20  * https://www.ilias.de
21  * https://github.com/ILIAS-eLearning
22  *
23  *****************************************************************************/
24 
25 class FinderTest extends TestCase
26 {
32  {
33  $fileSystem = $this->getMockBuilder(Filesystem\Filesystem::class)->getMock();
34 
35  $metadata = [
36  new Filesystem\DTO\Metadata('file_1.txt', MetadataType::FILE),
37  new Filesystem\DTO\Metadata('file_2.mp3', MetadataType::FILE),
38  new Filesystem\DTO\Metadata('dir_1', MetadataType::DIRECTORY),
39  ];
40 
41  $fileSystem
42  ->expects($this->atLeast(1))
43  ->method('listContents')
44  ->willReturnCallback(function ($path) use ($metadata) {
45  if ('/' === $path) {
46  return $metadata;
47  }
48 
49  return [];
50  });
51 
52  return $fileSystem;
53  }
54 
60  {
61  $fileSystem = $this->getMockBuilder(Filesystem\Filesystem::class)->getMock();
62 
63  $rootMetadata = [
64  new Filesystem\DTO\Metadata('file_1.txt', MetadataType::FILE),
65  new Filesystem\DTO\Metadata('file_2.mp3', MetadataType::FILE),
66  new Filesystem\DTO\Metadata('dir_1', MetadataType::DIRECTORY),
67  ];
68 
69  $level1Metadata = [
70  new Filesystem\DTO\Metadata('dir_1/file_3.log', MetadataType::FILE),
71  new Filesystem\DTO\Metadata('dir_1/file_4.php', MetadataType::FILE),
72  new Filesystem\DTO\Metadata('dir_1/dir_1_1', MetadataType::DIRECTORY),
73  new Filesystem\DTO\Metadata('dir_1/dir_1_2', MetadataType::DIRECTORY),
74  ];
75 
76  $level11Metadata = [
77  new Filesystem\DTO\Metadata('dir_1/dir_1_1/file_5.cpp', MetadataType::FILE),
78  ];
79 
80  $level12Metadata = [
81  new Filesystem\DTO\Metadata('dir_1/dir_1_2/file_6.py', MetadataType::FILE),
82  new Filesystem\DTO\Metadata('dir_1/dir_1_2/file_7.cpp', MetadataType::FILE),
83  new Filesystem\DTO\Metadata('dir_1/dir_1_2/dir_1_2_1', MetadataType::DIRECTORY),
84  ];
85 
86  $fileSystem
87  ->expects($this->atLeast(1))
88  ->method('listContents')
89  ->willReturnCallback(function ($path) use (
90  $rootMetadata,
91  $level1Metadata,
92  $level11Metadata,
93  $level12Metadata
94  ) {
95  if ('/' === $path) {
96  return $rootMetadata;
97  } elseif ('dir_1' === $path) {
98  return $level1Metadata;
99  } elseif ('dir_1/dir_1_1' === $path) {
100  return $level11Metadata;
101  } elseif ('dir_1/dir_1_2' === $path) {
102  return $level12Metadata;
103  }
104 
105  return [];
106  });
107 
108  return $fileSystem;
109  }
110 
115  {
116  $fileSystem = $this->getMockBuilder(Filesystem\Filesystem::class)->getMock();
117 
118  $fileSystem
119  ->method('listContents')
120  ->willReturn([]);
121 
122  $finder = (new Finder($fileSystem))->in(['/']);
123 
124  $this->assertEmpty(iterator_count($finder));
125  }
126 
131  {
132  $finder = (new Finder($this->getFlatFileSystemStructure()))->in(['/']);
133 
134  $this->assertCount(3, $finder);
135  $this->assertCount(1, $finder->directories());
136  $this->assertCount(2, $finder->files());
137  }
138 
143  {
144  $finder = (new Finder($this->getNestedFileSystemStructure()))->in(['/']);
145 
146  $this->assertCount(11, $finder);
147  $this->assertCount(4, $finder->directories());
148  $this->assertCount(7, $finder->files());
149  }
150 
155  {
156  $finder = (new Finder($this->getNestedFileSystemStructure()))->in(['/']);
157 
158  $level0Finder = $finder->depth(0);
159  $this->assertCount(3, $level0Finder);
160  $this->assertCount(1, $level0Finder->directories());
161  $this->assertCount(2, $level0Finder->files());
162 
163  $greaterLevel0Finder = $finder->depth('> 0');
164  $this->assertCount(8, $greaterLevel0Finder);
165  $this->assertCount(3, $greaterLevel0Finder->directories());
166  $this->assertCount(5, $greaterLevel0Finder->files());
167 
168  $greaterOrEqualLevel0Finder = $finder->depth('>= 0');
169  $this->assertCount(11, $greaterOrEqualLevel0Finder);
170  $this->assertCount(4, $greaterOrEqualLevel0Finder->directories());
171  $this->assertCount(7, $greaterOrEqualLevel0Finder->files());
172 
173  $lowerOrEqualLevel1Finder = $finder->depth('<= 1');
174  $this->assertCount(7, $lowerOrEqualLevel1Finder);
175  $this->assertCount(3, $lowerOrEqualLevel1Finder->directories());
176  $this->assertCount(4, $lowerOrEqualLevel1Finder->files());
177 
178  $lowerLevel2Finder = $finder->depth('< 2');
179  $this->assertCount(7, $lowerLevel2Finder);
180  $this->assertCount(3, $lowerLevel2Finder->directories());
181  $this->assertCount(4, $lowerLevel2Finder->files());
182 
183  $exactlyLevel2Finder = $finder->depth(2);
184  $this->assertCount(4, $exactlyLevel2Finder);
185  $this->assertCount(1, $exactlyLevel2Finder->directories());
186  $this->assertCount(3, $exactlyLevel2Finder->files());
187  }
188 
193  {
194  $finder = (new Finder($this->getNestedFileSystemStructure()))->in(['/']);
195 
196  $finderWithExcludedDir = $finder->exclude(['dir_1/dir_1_1']);
197  $this->assertCount(9, $finderWithExcludedDir);
198  $this->assertCount(3, $finderWithExcludedDir->directories());
199  $this->assertCount(6, $finderWithExcludedDir->files());
200 
201  $finderWithMultipleExcludedDirs = $finder->exclude(['dir_1/dir_1_1', 'dir_1/dir_1_2/dir_1_2_1']);
202  $this->assertCount(8, $finderWithMultipleExcludedDirs);
203  $this->assertCount(2, $finderWithMultipleExcludedDirs->directories());
204  $this->assertCount(6, $finderWithMultipleExcludedDirs->files());
205  }
206 
212  {
213  $now = new \DateTimeImmutable('2019-03-30 13:00:00');
214 
215  $fs = $this->getNestedFileSystemStructure();
216  $fs->method('has')->willReturn(true);
217 
218  $fs
219  ->expects($this->atLeast(1))
220  ->method('getTimestamp')
221  ->willReturnCallback(function ($path) use ($now): \DateTimeImmutable {
222  switch ($path) {
223  case'file_1.txt':
224  return $now;
225 
226  case 'file_2.mp3':
227  return $now->modify('+1 hour');
228 
229  case 'dir_1/file_3.log':
230  return $now->modify('+2 hour');
231 
232  case 'dir_1/file_4.php':
233  return $now->modify('+3 hour');
234 
235  case 'dir_1/dir_1_1/file_5.cpp':
236  return $now->modify('+4 hour');
237 
238  case 'dir_1/dir_1_2/file_6.py':
239  return $now->modify('+5 hour');
240 
241  case 'dir_1/dir_1_2/file_7.cpp':
242  return $now->modify('+6 hour');
243 
244  default:
245  return new \DateTimeImmutable('now');
246  }
247  });
248 
249  $finder = (new Finder($fs))->in(['/']);
250 
251  for ($i = 1; $i <= 7; $i++) {
252  $this->assertCount(8 - $i, $finder->date('>= 2019-03-30 1' . (string) (2 + $i) . ':00')->files());
253  }
254  $this->assertCount(3, $finder->date('>= 2019-03-30 15:00 + 2hours')->files());
255  $this->assertCount(2, $finder->date('> 2019-03-30 15:00 + 2hours')->files());
256  $this->assertCount(1, $finder->date('2019-03-30 15:00 + 2hours')->files());
257  $this->assertCount(2, $finder->date('< 2019-03-30 15:00')->files());
258  $this->assertCount(3, $finder->date('<= 2019-03-30 15:00')->files());
259  $this->assertCount(2, $finder->date('<= 2019-03-30 15:00 - 1minute')->files());
260 
261  return $fs;
262  }
263 
267  public function testFinderWillFilterFilesBySize(): void
268  {
269  $fs = $this->getNestedFileSystemStructure();
270  $fs->method('has')->willReturn(true);
271 
272  $fs->expects($this->atLeast(1))
273  ->method('getSize')
274  ->willReturnCallback(function ($path): \ILIAS\Data\DataSize {
275  switch ($path) {
276  case'file_1.txt':
277  return new DataSize(PHP_INT_MAX, DataSize::Byte);
278 
279  case 'file_2.mp3':
280  return new DataSize(1024, DataSize::Byte);
281 
282  case 'dir_1/file_3.log':
283  return new DataSize(1024 * 1024 * 1024, DataSize::Byte);
284 
285  case 'dir_1/file_4.php':
286  return new DataSize(1024 * 1024 * 127, DataSize::Byte);
287 
288  case 'dir_1/dir_1_1/file_5.cpp':
289  return new DataSize(1024 * 7, DataSize::Byte);
290 
291  case 'dir_1/dir_1_2/file_6.py':
292  return new DataSize(1024 * 100, DataSize::Byte);
293 
294  case 'dir_1/dir_1_2/file_7.cpp':
295  return new DataSize(1, DataSize::Byte);
296 
297  default:
298  return new DataSize(0, DataSize::Byte);
299  }
300  });
301 
302  $finder = (new Finder($fs))->in(['/']);
303 
304  $this->assertCount(1, $finder->size('< 1Ki')->files());
305  $this->assertCount(2, $finder->size('<= 1Ki')->files());
306  $this->assertCount(6, $finder->size('>= 1Ki')->files());
307  $this->assertCount(5, $finder->size('> 1Ki')->files());
308  $this->assertCount(1, $finder->size('1Ki')->files());
309 
310  $this->assertCount(3, $finder->size('> 1Mi')->files());
311  $this->assertCount(2, $finder->size('>= 1Gi')->files());
312  }
313 
319  {
320  $finder = (new Finder($fs))->in(['/']);
321 
322  $this->assertEquals('file_1.txt', $finder->files()->sortByTime()->getIterator()->current()->getPath());
323  $this->assertEquals(
324  'dir_1/dir_1_2/file_7.cpp',
325  $finder->files()->sortByTime()->reverseSorting()->getIterator()->current()->getPath()
326  );
327 
328  $this->assertEquals('dir_1', $finder->sortByName()->getIterator()->current()->getPath());
329  $this->assertEquals('file_2.mp3', $finder->sortByName()->reverseSorting()->getIterator()->current()->getPath());
330 
331  $this->assertEquals('dir_1', $finder->sortByType()->getIterator()->current()->getPath());
332  $this->assertEquals('file_2.mp3', $finder->sortByType()->reverseSorting()->getIterator()->current()->getPath());
333 
334  $customSortFinder = $finder->sort(function (Filesystem\DTO\Metadata $left, Filesystem\DTO\Metadata $right): int {
335  if ('dir_1/dir_1_1/file_5.cpp' === $left->getPath()) {
336  return -1;
337  }
338 
339  return 1;
340  });
341  $this->assertEquals('dir_1/dir_1_1/file_5.cpp', $customSortFinder->getIterator()->current()->getPath());
342  $all = array_values(iterator_to_array($customSortFinder->reverseSorting()->getIterator()));
343  $last = $all[iterator_count($customSortFinder) - 1];
344  $this->assertEquals('dir_1/dir_1_1/file_5.cpp', $last->getPath());
345  }
346 }
getFlatFileSystemStructure()
Definition: FinderTest.php:31
testFinderWillFindFilesAndFoldersInFlatStructure()
Definition: FinderTest.php:130
testSortingWorksAsExpected(Filesystem\Filesystem $fs)
Definition: FinderTest.php:318
Class ChatMainBarProvider .
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:30
getNestedFileSystemStructure()
Definition: FinderTest.php:59
$path
Definition: ltiservices.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testFinderWillFindNoFilesOrFoldersInAnEmptyDirectory()
Definition: FinderTest.php:114
testFinderWillFilterFilesAndFoldersByCreationTimestamp()
Definition: FinderTest.php:211
testFinderWillFindFilesAndFoldersInNestedStructure()
Definition: FinderTest.php:142
testFinderWillFindFilesAndFoldersForACertainDirectoryDepth()
Definition: FinderTest.php:154
testFinderWillFilterFilesBySize()
Definition: FinderTest.php:267
Class FlySystemFileAccessTest disabled disabled disabled.
testFinderWillNotSearchInExcludedFolders()
Definition: FinderTest.php:192
$i
Definition: metadata.php:41