ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
FinderTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 
34 class FinderTest extends TestCase
35 {
39  private function getFlatFileSystemStructure(): MockObject
40  {
41  $fileSystem = $this->getMockBuilder(Filesystem\Filesystem::class)->getMock();
42 
43  $metadata = [
44  new Metadata('file_1.txt', MetadataType::FILE),
45  new Metadata('file_2.mp3', MetadataType::FILE),
46  new Metadata('dir_1', MetadataType::DIRECTORY),
47  ];
48 
49  $fileSystem
50  ->expects($this->atLeast(1))
51  ->method('listContents')
52  ->willReturnCallback(function ($path) use ($metadata): array {
53  if ('/' === $path) {
54  return $metadata;
55  }
56 
57  return [];
58  });
59 
60  return $fileSystem;
61  }
62 
66  private function getNestedFileSystemStructure(): MockObject
67  {
68  $fileSystem = $this->getMockBuilder(Filesystem\Filesystem::class)->getMock();
69 
70  $rootMetadata = [
71  new Metadata('file_1.txt', MetadataType::FILE),
72  new Metadata('file_2.mp3', MetadataType::FILE),
73  new Metadata('dir_1', MetadataType::DIRECTORY),
74  ];
75 
76  $level1Metadata = [
77  new Metadata('dir_1/file_3.log', MetadataType::FILE),
78  new Metadata('dir_1/file_4.php', MetadataType::FILE),
79  new Metadata('dir_1/dir_1_1', MetadataType::DIRECTORY),
80  new Metadata('dir_1/dir_1_2', MetadataType::DIRECTORY),
81  ];
82 
83  $level11Metadata = [
84  new Metadata('dir_1/dir_1_1/file_5.cpp', MetadataType::FILE),
85  ];
86 
87  $level12Metadata = [
88  new Metadata('dir_1/dir_1_2/file_6.py', MetadataType::FILE),
89  new Metadata('dir_1/dir_1_2/file_7.cpp', MetadataType::FILE),
90  new Metadata('dir_1/dir_1_2/dir_1_2_1', MetadataType::DIRECTORY),
91  ];
92 
93  $fileSystem
94  ->expects($this->atLeast(1))
95  ->method('listContents')
96  ->willReturnCallback(function ($path) use (
97  $rootMetadata,
98  $level1Metadata,
99  $level11Metadata,
100  $level12Metadata
101  ): array {
102  if ('/' === $path) {
103  return $rootMetadata;
104  }
105  if ('dir_1' === $path) {
106  return $level1Metadata;
107  }
108  if ('dir_1/dir_1_1' === $path) {
109  return $level11Metadata;
110  }
111  if ('dir_1/dir_1_2' === $path) {
112  return $level12Metadata;
113  }
114 
115  return [];
116  });
117 
118  return $fileSystem;
119  }
120 
125  {
126  $fileSystem = $this->getMockBuilder(Filesystem\Filesystem::class)->getMock();
127 
128  $fileSystem
129  ->method('listContents')
130  ->willReturn([]);
131 
132  $finder = (new Finder($fileSystem))->in(['/']);
133 
134  $this->assertEmpty(iterator_count($finder));
135  }
136 
141  {
142  $finder = (new Finder($this->getFlatFileSystemStructure()))->in(['/']);
143 
144  $this->assertCount(3, $finder);
145  $this->assertCount(1, $finder->directories());
146  $this->assertCount(2, $finder->files());
147  }
148 
153  {
154  $finder = (new Finder($this->getNestedFileSystemStructure()))->in(['/']);
155 
156  $this->assertCount(11, $finder);
157  $this->assertCount(4, $finder->directories());
158  $this->assertCount(7, $finder->files());
159  }
160 
165  {
166  $finder = (new Finder($this->getNestedFileSystemStructure()))->in(['/']);
167 
168  $level0Finder = $finder->depth(0);
169  $this->assertCount(3, $level0Finder);
170  $this->assertCount(1, $level0Finder->directories());
171  $this->assertCount(2, $level0Finder->files());
172 
173  $greaterLevel0Finder = $finder->depth('> 0');
174  $this->assertCount(8, $greaterLevel0Finder);
175  $this->assertCount(3, $greaterLevel0Finder->directories());
176  $this->assertCount(5, $greaterLevel0Finder->files());
177 
178  $greaterOrEqualLevel0Finder = $finder->depth('>= 0');
179  $this->assertCount(11, $greaterOrEqualLevel0Finder);
180  $this->assertCount(4, $greaterOrEqualLevel0Finder->directories());
181  $this->assertCount(7, $greaterOrEqualLevel0Finder->files());
182 
183  $lowerOrEqualLevel1Finder = $finder->depth('<= 1');
184  $this->assertCount(7, $lowerOrEqualLevel1Finder);
185  $this->assertCount(3, $lowerOrEqualLevel1Finder->directories());
186  $this->assertCount(4, $lowerOrEqualLevel1Finder->files());
187 
188  $lowerLevel2Finder = $finder->depth('< 2');
189  $this->assertCount(7, $lowerLevel2Finder);
190  $this->assertCount(3, $lowerLevel2Finder->directories());
191  $this->assertCount(4, $lowerLevel2Finder->files());
192 
193  $exactlyLevel2Finder = $finder->depth(2);
194  $this->assertCount(4, $exactlyLevel2Finder);
195  $this->assertCount(1, $exactlyLevel2Finder->directories());
196  $this->assertCount(3, $exactlyLevel2Finder->files());
197  }
198 
203  {
204  $finder = (new Finder($this->getNestedFileSystemStructure()))->in(['/']);
205 
206  $finderWithExcludedDir = $finder->exclude(['dir_1/dir_1_1']);
207  $this->assertCount(9, $finderWithExcludedDir);
208  $this->assertCount(3, $finderWithExcludedDir->directories());
209  $this->assertCount(6, $finderWithExcludedDir->files());
210 
211  $finderWithMultipleExcludedDirs = $finder->exclude(['dir_1/dir_1_1', 'dir_1/dir_1_2/dir_1_2_1']);
212  $this->assertCount(8, $finderWithMultipleExcludedDirs);
213  $this->assertCount(2, $finderWithMultipleExcludedDirs->directories());
214  $this->assertCount(6, $finderWithMultipleExcludedDirs->files());
215  }
216 
221  {
222  $now = new \DateTimeImmutable('2019-03-30 13:00:00');
223 
224  $fs = $this->getNestedFileSystemStructure();
225  $fs->method('has')->willReturn(true);
226 
227  $fs
228  ->expects($this->atLeast(1))
229  ->method('getTimestamp')
230  ->willReturnCallback(fn($path): \DateTimeImmutable => match ($path) {
231  'file_1.txt' => $now,
232  'file_2.mp3' => $now->modify('+1 hour'),
233  'dir_1/file_3.log' => $now->modify('+2 hour'),
234  'dir_1/file_4.php' => $now->modify('+3 hour'),
235  'dir_1/dir_1_1/file_5.cpp' => $now->modify('+4 hour'),
236  'dir_1/dir_1_2/file_6.py' => $now->modify('+5 hour'),
237  'dir_1/dir_1_2/file_7.cpp' => $now->modify('+6 hour'),
238  default => new \DateTimeImmutable('now'),
239  });
240 
241  $finder = (new Finder($fs))->in(['/']);
242 
243  for ($i = 1; $i <= 7; $i++) {
244  $this->assertCount(8 - $i, $finder->date('>= 2019-03-30 1' . (2 + $i) . ':00')->files());
245  }
246  $this->assertCount(3, $finder->date('>= 2019-03-30 15:00 + 2hours')->files());
247  $this->assertCount(2, $finder->date('> 2019-03-30 15:00 + 2hours')->files());
248  $this->assertCount(1, $finder->date('2019-03-30 15:00 + 2hours')->files());
249  $this->assertCount(2, $finder->date('< 2019-03-30 15:00')->files());
250  $this->assertCount(3, $finder->date('<= 2019-03-30 15:00')->files());
251  $this->assertCount(2, $finder->date('<= 2019-03-30 15:00 - 1minute')->files());
252 
253  return $fs;
254  }
255 
259  public function testFinderWillFilterFilesBySize(): void
260  {
261  $fs = $this->getNestedFileSystemStructure();
262  $fs->method('has')->willReturn(true);
263 
264  $fs->expects($this->atLeast(1))
265  ->method('getSize')
266  ->willReturnCallback(fn($path): DataSize => match ($path) {
267  'file_1.txt' => new DataSize(PHP_INT_MAX, DataSize::Byte),
268  'file_2.mp3' => new DataSize(1024, DataSize::Byte),
269  'dir_1/file_3.log' => new DataSize(1024 * 1024 * 1024, DataSize::Byte),
270  'dir_1/file_4.php' => new DataSize(1024 * 1024 * 127, DataSize::Byte),
271  'dir_1/dir_1_1/file_5.cpp' => new DataSize(1024 * 7, DataSize::Byte),
272  'dir_1/dir_1_2/file_6.py' => new DataSize(1024 * 100, DataSize::Byte),
273  'dir_1/dir_1_2/file_7.cpp' => new DataSize(1, DataSize::Byte),
274  default => new DataSize(0, DataSize::Byte),
275  });
276 
277  $finder = (new Finder($fs))->in(['/']);
278 
279  $this->assertCount(1, $finder->size('< 1Ki')->files());
280  $this->assertCount(2, $finder->size('<= 1Ki')->files());
281  $this->assertCount(6, $finder->size('>= 1Ki')->files());
282  $this->assertCount(5, $finder->size('> 1Ki')->files());
283  $this->assertCount(1, $finder->size('1Ki')->files());
284 
285  $this->assertCount(3, $finder->size('> 1Mi')->files());
286  $this->assertCount(2, $finder->size('>= 1Gi')->files());
287  }
288 
289  #[Depends('testFinderWillFilterFilesAndFoldersByCreationTimestamp')]
291  {
292  $finder = (new Finder($fs))->in(['/']);
293 
294  $this->assertEquals('file_1.txt', $finder->files()->sortByTime()->getIterator()->current()->getPath());
295  $this->assertEquals(
296  'dir_1/dir_1_2/file_7.cpp',
297  $finder->files()->sortByTime()->reverseSorting()->getIterator()->current()->getPath()
298  );
299 
300  $this->assertEquals('dir_1', $finder->sortByName()->getIterator()->current()->getPath());
301  $this->assertEquals('file_2.mp3', $finder->sortByName()->reverseSorting()->getIterator()->current()->getPath());
302 
303  $this->assertEquals('dir_1', $finder->sortByType()->getIterator()->current()->getPath());
304  $this->assertEquals('file_2.mp3', $finder->sortByType()->reverseSorting()->getIterator()->current()->getPath());
305 
306  $customSortFinder = $finder->sort(function (Metadata $left, Metadata $right): int {
307  if ('dir_1/dir_1_1/file_5.cpp' === $left->getPath()) {
308  return -1;
309  }
310 
311  return 1;
312  });
313  $this->assertEquals('dir_1/dir_1_1/file_5.cpp', $customSortFinder->getIterator()->current()->getPath());
314  $all = array_values(iterator_to_array($customSortFinder->reverseSorting()->getIterator()));
315  $last = $all[iterator_count($customSortFinder) - 1];
316  $this->assertEquals('dir_1/dir_1_1/file_5.cpp', $last->getPath());
317  }
318 }
getFlatFileSystemStructure()
Definition: FinderTest.php:39
testFinderWillFindFilesAndFoldersInFlatStructure()
Definition: FinderTest.php:140
testSortingWorksAsExpected(Filesystem\Filesystem $fs)
Definition: FinderTest.php:290
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:30
getPath()
The path to the file or directory.
Definition: Metadata.php:62
getNestedFileSystemStructure()
Definition: FinderTest.php:66
$path
Definition: ltiservices.php:29
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testFinderWillFindNoFilesOrFoldersInAnEmptyDirectory()
Definition: FinderTest.php:124
testFinderWillFilterFilesAndFoldersByCreationTimestamp()
Definition: FinderTest.php:220
testFinderWillFindFilesAndFoldersInNestedStructure()
Definition: FinderTest.php:152
testFinderWillFindFilesAndFoldersForACertainDirectoryDepth()
Definition: FinderTest.php:164
testFinderWillFilterFilesBySize()
Definition: FinderTest.php:259
testFinderWillNotSearchInExcludedFolders()
Definition: FinderTest.php:202