ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
FinderTest.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
8 
9 class FinderTest extends TestCase
10 {
16  {
17  $fileSystem = $this->getMockBuilder(Filesystem\Filesystem::class)->getMock();
18 
19  $metadata = [
20  new Filesystem\DTO\Metadata('file_1.txt', MetadataType::FILE),
21  new Filesystem\DTO\Metadata('file_2.mp3', MetadataType::FILE),
22  new Filesystem\DTO\Metadata('dir_1', MetadataType::DIRECTORY),
23  ];
24 
25  $fileSystem
26  ->expects($this->atLeast(1))
27  ->method('listContents')
28  ->will($this->returnCallback(function ($path) use ($metadata) {
29  if ('/' === $path) {
30  return $metadata;
31  }
32 
33  return [];
34  }));
35 
36  return $fileSystem;
37  }
38 
44  {
45  $fileSystem = $this->getMockBuilder(Filesystem\Filesystem::class)->getMock();
46 
47  $rootMetadata = [
48  new Filesystem\DTO\Metadata('file_1.txt', MetadataType::FILE),
49  new Filesystem\DTO\Metadata('file_2.mp3', MetadataType::FILE),
50  new Filesystem\DTO\Metadata('dir_1', MetadataType::DIRECTORY),
51  ];
52 
53  $level1Metadata = [
54  new Filesystem\DTO\Metadata('dir_1/file_3.log', MetadataType::FILE),
55  new Filesystem\DTO\Metadata('dir_1/file_4.php', MetadataType::FILE),
56  new Filesystem\DTO\Metadata('dir_1/dir_1_1', MetadataType::DIRECTORY),
57  new Filesystem\DTO\Metadata('dir_1/dir_1_2', MetadataType::DIRECTORY),
58  ];
59 
60  $level11Metadata = [
61  new Filesystem\DTO\Metadata('dir_1/dir_1_1/file_5.cpp', MetadataType::FILE),
62  ];
63 
64  $level12Metadata = [
65  new Filesystem\DTO\Metadata('dir_1/dir_1_2/file_6.py', MetadataType::FILE),
66  new Filesystem\DTO\Metadata('dir_1/dir_1_2/file_7.cpp', MetadataType::FILE),
67  new Filesystem\DTO\Metadata('dir_1/dir_1_2/dir_1_2_1', MetadataType::DIRECTORY),
68  ];
69 
70  $fileSystem
71  ->expects($this->atLeast(1))
72  ->method('listContents')
73  ->will($this->returnCallback(function ($path) use (
74  $rootMetadata,
75  $level1Metadata,
76  $level11Metadata,
77  $level12Metadata
78  ) {
79  if ('/' === $path) {
80  return $rootMetadata;
81  } elseif ('dir_1' === $path) {
82  return $level1Metadata;
83  } elseif ('dir_1/dir_1_1' === $path) {
84  return $level11Metadata;
85  } elseif ('dir_1/dir_1_2' === $path) {
86  return $level12Metadata;
87  }
88 
89  return [];
90  }));
91 
92  return $fileSystem;
93  }
94 
99  {
100  $fileSystem = $this->getMockBuilder(Filesystem\Filesystem::class)->getMock();
101 
102  $fileSystem
103  ->expects($this->any())
104  ->method('listContents')
105  ->willReturn([]);
106 
107  $finder = (new Finder($fileSystem))->in(['/']);
108 
109  $this->assertEmpty(iterator_count($finder));
110  }
111 
116  {
117  $finder = (new Finder($this->getFlatFileSystemStructure()))->in(['/']);
118 
119  $this->assertCount(3, $finder);
120  $this->assertCount(1, $finder->directories());
121  $this->assertCount(2, $finder->files());
122  }
123 
128  {
129  $finder = (new Finder($this->getNestedFileSystemStructure()))->in(['/']);
130 
131  $this->assertCount(11, $finder);
132  $this->assertCount(4, $finder->directories());
133  $this->assertCount(7, $finder->files());
134  }
135 
140  {
141  $finder = (new Finder($this->getNestedFileSystemStructure()))->in(['/']);
142 
143  $level0Finder = $finder->depth(0);
144  $this->assertCount(3, $level0Finder);
145  $this->assertCount(1, $level0Finder->directories());
146  $this->assertCount(2, $level0Finder->files());
147 
148  $greaterLevel0Finder = $finder->depth('> 0');
149  $this->assertCount(8, $greaterLevel0Finder);
150  $this->assertCount(3, $greaterLevel0Finder->directories());
151  $this->assertCount(5, $greaterLevel0Finder->files());
152 
153  $greaterOrEqualLevel0Finder = $finder->depth('>= 0');
154  $this->assertCount(11, $greaterOrEqualLevel0Finder);
155  $this->assertCount(4, $greaterOrEqualLevel0Finder->directories());
156  $this->assertCount(7, $greaterOrEqualLevel0Finder->files());
157 
158  $lowerOrEqualLevel1Finder = $finder->depth('<= 1');
159  $this->assertCount(7, $lowerOrEqualLevel1Finder);
160  $this->assertCount(3, $lowerOrEqualLevel1Finder->directories());
161  $this->assertCount(4, $lowerOrEqualLevel1Finder->files());
162 
163  $lowerLevel2Finder = $finder->depth('< 2');
164  $this->assertCount(7, $lowerLevel2Finder);
165  $this->assertCount(3, $lowerLevel2Finder->directories());
166  $this->assertCount(4, $lowerLevel2Finder->files());
167 
168  $exactlyLevel2Finder = $finder->depth(2);
169  $this->assertCount(4, $exactlyLevel2Finder);
170  $this->assertCount(1, $exactlyLevel2Finder->directories());
171  $this->assertCount(3, $exactlyLevel2Finder->files());
172  }
173 
178  {
179  $finder = (new Finder($this->getNestedFileSystemStructure()))->in(['/']);
180 
181  $finderWithExcludedDir = $finder->exclude(['dir_1/dir_1_1']);
182  $this->assertCount(9, $finderWithExcludedDir);
183  $this->assertCount(3, $finderWithExcludedDir->directories());
184  $this->assertCount(6, $finderWithExcludedDir->files());
185 
186  $finderWithMultipleExcludedDirs = $finder->exclude(['dir_1/dir_1_1', 'dir_1/dir_1_2/dir_1_2_1']);
187  $this->assertCount(8, $finderWithMultipleExcludedDirs);
188  $this->assertCount(2, $finderWithMultipleExcludedDirs->directories());
189  $this->assertCount(6, $finderWithMultipleExcludedDirs->files());
190  }
191 
197  {
198  $now = new \DateTimeImmutable('2019-03-30 13:00:00');
199 
200  $fs = $this->getNestedFileSystemStructure();
201  $fs
202  ->expects($this->any())->method('has')->willReturn(true);
203 
204  $fs
205  ->expects($this->atLeast(1))
206  ->method('getTimestamp')
207  ->will($this->returnCallback(function ($path) use ($now) {
208  switch ($path) {
209  case'file_1.txt':
210  return $now;
211 
212  case 'file_2.mp3':
213  return $now->modify('+1 hour');
214 
215  case 'dir_1/file_3.log':
216  return $now->modify('+2 hour');
217 
218  case 'dir_1/file_4.php':
219  return $now->modify('+3 hour');
220 
221  case 'dir_1/dir_1_1/file_5.cpp':
222  return $now->modify('+4 hour');
223 
224  case 'dir_1/dir_1_2/file_6.py':
225  return $now->modify('+5 hour');
226 
227  case 'dir_1/dir_1_2/file_7.cpp':
228  return $now->modify('+6 hour');
229 
230  default:
231  return new \DateTimeImmutable('now');
232  }
233  }));
234 
235  $finder = (new Finder($fs))->in(['/']);
236 
237  for ($i = 1; $i <= 7; $i++) {
238  $this->assertCount(8 - $i, $finder->date('>= 2019-03-30 1' . (string) (2 + $i) . ':00')->files());
239  }
240  $this->assertCount(3, $finder->date('>= 2019-03-30 15:00 + 2hours')->files());
241  $this->assertCount(2, $finder->date('> 2019-03-30 15:00 + 2hours')->files());
242  $this->assertCount(1, $finder->date('2019-03-30 15:00 + 2hours')->files());
243  $this->assertCount(2, $finder->date('< 2019-03-30 15:00')->files());
244  $this->assertCount(3, $finder->date('<= 2019-03-30 15:00')->files());
245  $this->assertCount(2, $finder->date('<= 2019-03-30 15:00 - 1minute')->files());
246 
247  return $fs;
248  }
249 
253  public function testFinderWillFilterFilesBySize() : void
254  {
255  $fs = $this->getNestedFileSystemStructure();
256  $fs->expects($this->any())->method('has')->willReturn(true);
257 
258  $fs->expects($this->atLeast(1))
259  ->method('getSize')
260  ->will($this->returnCallback(function ($path) {
261  switch ($path) {
262  case'file_1.txt':
263  return new DataSize(PHP_INT_MAX, DataSize::Byte);
264 
265  case 'file_2.mp3':
266  return new DataSize(1024, DataSize::Byte);
267 
268  case 'dir_1/file_3.log':
269  return new DataSize(1024 * 1024 * 1024, DataSize::Byte);
270 
271  case 'dir_1/file_4.php':
272  return new DataSize(1024 * 1024 * 127, DataSize::Byte);
273 
274  case 'dir_1/dir_1_1/file_5.cpp':
275  return new DataSize(1024 * 7, DataSize::Byte);
276 
277  case 'dir_1/dir_1_2/file_6.py':
278  return new DataSize(1024 * 100, DataSize::Byte);
279 
280  case 'dir_1/dir_1_2/file_7.cpp':
281  return new DataSize(1, DataSize::Byte);
282 
283  default:
284  return new DataSize(0, DataSize::Byte);
285  }
286  }));
287 
288  $finder = (new Finder($fs))->in(['/']);
289 
290  $this->assertCount(1, $finder->size('< 1Ki')->files());
291  $this->assertCount(2, $finder->size('<= 1Ki')->files());
292  $this->assertCount(6, $finder->size('>= 1Ki')->files());
293  $this->assertCount(5, $finder->size('> 1Ki')->files());
294  $this->assertCount(1, $finder->size('1Ki')->files());
295 
296  $this->assertCount(3, $finder->size('> 1Mi')->files());
297  $this->assertCount(2, $finder->size('>= 1Gi')->files());
298  }
299 
305  {
306  $finder = (new Finder($fs))->in(['/']);
307 
308  $this->assertEquals('file_1.txt', $finder->files()->sortByTime()->getIterator()->current()->getPath());
309  $this->assertEquals(
310  'dir_1/dir_1_2/file_7.cpp',
311  $finder->files()->sortByTime()->reverseSorting()->getIterator()->current()->getPath()
312  );
313 
314  $this->assertEquals('dir_1', $finder->sortByName()->getIterator()->current()->getPath());
315  $this->assertEquals('file_2.mp3', $finder->sortByName()->reverseSorting()->getIterator()->current()->getPath());
316 
317  $this->assertEquals('dir_1', $finder->sortByType()->getIterator()->current()->getPath());
318  $this->assertEquals('file_2.mp3', $finder->sortByType()->reverseSorting()->getIterator()->current()->getPath());
319 
320  $customSortFinder = $finder->sort(function (Filesystem\DTO\Metadata $left, Filesystem\DTO\Metadata $right) {
321  if ('dir_1/dir_1_1/file_5.cpp' === $left->getPath()) {
322  return -1;
323  }
324 
325  return 1;
326  });
327  $this->assertEquals('dir_1/dir_1_1/file_5.cpp', $customSortFinder->getIterator()->current()->getPath());
328  $all = array_values(iterator_to_array($customSortFinder->reverseSorting()->getIterator()));
329  $last = $all[iterator_count($customSortFinder) - 1];
330  $this->assertEquals('dir_1/dir_1_1/file_5.cpp', $last->getPath());
331  }
332 }
getFlatFileSystemStructure()
Definition: FinderTest.php:15
testFinderWillFindFilesAndFoldersInFlatStructure()
Definition: FinderTest.php:115
testSortingWorksAsExpected(Filesystem\Filesystem $fs)
Definition: FinderTest.php:304
Class DataSize.
Definition: DataSize.php:15
getNestedFileSystemStructure()
Definition: FinderTest.php:43
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class Metadata This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:16
testFinderWillFindNoFilesOrFoldersInAnEmptyDirectory()
Definition: FinderTest.php:98
testFinderWillFilterFilesAndFoldersByCreationTimestamp()
Definition: FinderTest.php:196
testFinderWillFindFilesAndFoldersInNestedStructure()
Definition: FinderTest.php:127
testFinderWillFindFilesAndFoldersForACertainDirectoryDepth()
Definition: FinderTest.php:139
testFinderWillFilterFilesBySize()
Definition: FinderTest.php:253
Class FlySystemFileAccessTest disabled disabled disabled.
testFinderWillNotSearchInExcludedFolders()
Definition: FinderTest.php:177
$i
Definition: metadata.php:24