1 <?php declare(strict_types=1);
17 $fileSystem = $this->getMockBuilder(
Filesystem\Filesystem::class)->getMock();
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),
26 ->expects($this->atLeast(1))
27 ->method(
'listContents')
28 ->will($this->returnCallback(
function ($path) use ($metadata) {
45 $fileSystem = $this->getMockBuilder(
Filesystem\Filesystem::class)->getMock();
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),
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),
61 new Filesystem\DTO\Metadata(
'dir_1/dir_1_1/file_5.cpp', MetadataType::FILE),
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),
71 ->expects($this->atLeast(1))
72 ->method(
'listContents')
73 ->will($this->returnCallback(
function ($path) use (
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;
100 $fileSystem = $this->getMockBuilder(
Filesystem\Filesystem::class)->getMock();
103 ->expects($this->any())
104 ->method(
'listContents')
107 $finder = (
new Finder($fileSystem))->in([
'/']);
109 $this->assertEmpty(iterator_count($finder));
119 $this->assertCount(3, $finder);
120 $this->assertCount(1, $finder->directories());
121 $this->assertCount(2, $finder->files());
131 $this->assertCount(11, $finder);
132 $this->assertCount(4, $finder->directories());
133 $this->assertCount(7, $finder->files());
143 $level0Finder = $finder->depth(0);
144 $this->assertCount(3, $level0Finder);
145 $this->assertCount(1, $level0Finder->directories());
146 $this->assertCount(2, $level0Finder->files());
148 $greaterLevel0Finder = $finder->depth(
'> 0');
149 $this->assertCount(8, $greaterLevel0Finder);
150 $this->assertCount(3, $greaterLevel0Finder->directories());
151 $this->assertCount(5, $greaterLevel0Finder->files());
153 $greaterOrEqualLevel0Finder = $finder->depth(
'>= 0');
154 $this->assertCount(11, $greaterOrEqualLevel0Finder);
155 $this->assertCount(4, $greaterOrEqualLevel0Finder->directories());
156 $this->assertCount(7, $greaterOrEqualLevel0Finder->files());
158 $lowerOrEqualLevel1Finder = $finder->depth(
'<= 1');
159 $this->assertCount(7, $lowerOrEqualLevel1Finder);
160 $this->assertCount(3, $lowerOrEqualLevel1Finder->directories());
161 $this->assertCount(4, $lowerOrEqualLevel1Finder->files());
163 $lowerLevel2Finder = $finder->depth(
'< 2');
164 $this->assertCount(7, $lowerLevel2Finder);
165 $this->assertCount(3, $lowerLevel2Finder->directories());
166 $this->assertCount(4, $lowerLevel2Finder->files());
168 $exactlyLevel2Finder = $finder->depth(2);
169 $this->assertCount(4, $exactlyLevel2Finder);
170 $this->assertCount(1, $exactlyLevel2Finder->directories());
171 $this->assertCount(3, $exactlyLevel2Finder->files());
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());
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());
198 $now = new \DateTimeImmutable(
'2019-03-30 13:00:00');
202 ->expects($this->any())->method(
'has')->willReturn(
true);
205 ->expects($this->atLeast(1))
206 ->method(
'getTimestamp')
207 ->will($this->returnCallback(
function ($path) use ($now) {
213 return $now->modify(
'+1 hour');
215 case 'dir_1/file_3.log':
216 return $now->modify(
'+2 hour');
218 case 'dir_1/file_4.php':
219 return $now->modify(
'+3 hour');
221 case 'dir_1/dir_1_1/file_5.cpp':
222 return $now->modify(
'+4 hour');
224 case 'dir_1/dir_1_2/file_6.py':
225 return $now->modify(
'+5 hour');
227 case 'dir_1/dir_1_2/file_7.cpp':
228 return $now->modify(
'+6 hour');
231 return new \DateTimeImmutable(
'now');
235 $finder = (
new Finder($fs))->in([
'/']);
237 for (
$i = 1;
$i <= 7;
$i++) {
238 $this->assertCount(8 -
$i, $finder->date(
'>= 2019-03-30 1' . (
string) (2 +
$i) .
':00')->files());
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());
256 $fs->expects($this->any())->method(
'has')->willReturn(
true);
258 $fs->expects($this->atLeast(1))
260 ->will($this->returnCallback(
function ($path) {
263 return new DataSize(PHP_INT_MAX, DataSize::Byte);
266 return new DataSize(1024, DataSize::Byte);
268 case 'dir_1/file_3.log':
269 return new DataSize(1024 * 1024 * 1024, DataSize::Byte);
271 case 'dir_1/file_4.php':
272 return new DataSize(1024 * 1024 * 127, DataSize::Byte);
274 case 'dir_1/dir_1_1/file_5.cpp':
275 return new DataSize(1024 * 7, DataSize::Byte);
277 case 'dir_1/dir_1_2/file_6.py':
278 return new DataSize(1024 * 100, DataSize::Byte);
280 case 'dir_1/dir_1_2/file_7.cpp':
281 return new DataSize(1, DataSize::Byte);
284 return new DataSize(0, DataSize::Byte);
288 $finder = (
new Finder($fs))->in([
'/']);
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());
296 $this->assertCount(3, $finder->size(
'> 1Mi')->files());
297 $this->assertCount(2, $finder->size(
'>= 1Gi')->files());
306 $finder = (
new Finder($fs))->in([
'/']);
308 $this->assertEquals(
'file_1.txt', $finder->files()->sortByTime()->getIterator()->current()->getPath());
310 'dir_1/dir_1_2/file_7.cpp',
311 $finder->files()->sortByTime()->reverseSorting()->getIterator()->current()->getPath()
314 $this->assertEquals(
'dir_1', $finder->sortByName()->getIterator()->current()->getPath());
315 $this->assertEquals(
'file_2.mp3', $finder->sortByName()->reverseSorting()->getIterator()->current()->getPath());
317 $this->assertEquals(
'dir_1', $finder->sortByType()->getIterator()->current()->getPath());
318 $this->assertEquals(
'file_2.mp3', $finder->sortByType()->reverseSorting()->getIterator()->current()->getPath());
321 if (
'dir_1/dir_1_1/file_5.cpp' === $left->getPath()) {
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());
getFlatFileSystemStructure()
testFinderWillFindFilesAndFoldersInFlatStructure()
testSortingWorksAsExpected(Filesystem\Filesystem $fs)
getNestedFileSystemStructure()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testFinderWillFindNoFilesOrFoldersInAnEmptyDirectory()
testFinderWillFilterFilesAndFoldersByCreationTimestamp()
testFinderWillFindFilesAndFoldersInNestedStructure()
testFinderWillFindFilesAndFoldersForACertainDirectoryDepth()
testFinderWillFilterFilesBySize()
Class FlySystemFileAccessTest disabled disabled disabled.
testFinderWillNotSearchInExcludedFolders()