19 declare(strict_types=1);
41 $fileSystem = $this->getMockBuilder(
Filesystem\Filesystem::class)->getMock();
44 new Metadata(
'file_1.txt', MetadataType::FILE),
45 new Metadata(
'file_2.mp3', MetadataType::FILE),
46 new Metadata(
'dir_1', MetadataType::DIRECTORY),
50 ->expects($this->atLeast(1))
51 ->method(
'listContents')
52 ->willReturnCallback(
function (
$path) use ($metadata): array {
68 $fileSystem = $this->getMockBuilder(
Filesystem\Filesystem::class)->getMock();
71 new Metadata(
'file_1.txt', MetadataType::FILE),
72 new Metadata(
'file_2.mp3', MetadataType::FILE),
73 new Metadata(
'dir_1', MetadataType::DIRECTORY),
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),
84 new Metadata(
'dir_1/dir_1_1/file_5.cpp', MetadataType::FILE),
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),
94 ->expects($this->atLeast(1))
95 ->method(
'listContents')
96 ->willReturnCallback(
function (
$path) use (
103 return $rootMetadata;
105 if (
'dir_1' ===
$path) {
106 return $level1Metadata;
108 if (
'dir_1/dir_1_1' ===
$path) {
109 return $level11Metadata;
111 if (
'dir_1/dir_1_2' ===
$path) {
112 return $level12Metadata;
126 $fileSystem = $this->getMockBuilder(
Filesystem\Filesystem::class)->getMock();
129 ->method(
'listContents')
132 $finder = (
new Finder($fileSystem))->in([
'/']);
134 $this->assertEmpty(iterator_count($finder));
144 $this->assertCount(3, $finder);
145 $this->assertCount(1, $finder->directories());
146 $this->assertCount(2, $finder->files());
156 $this->assertCount(11, $finder);
157 $this->assertCount(4, $finder->directories());
158 $this->assertCount(7, $finder->files());
168 $level0Finder = $finder->depth(0);
169 $this->assertCount(3, $level0Finder);
170 $this->assertCount(1, $level0Finder->directories());
171 $this->assertCount(2, $level0Finder->files());
173 $greaterLevel0Finder = $finder->depth(
'> 0');
174 $this->assertCount(8, $greaterLevel0Finder);
175 $this->assertCount(3, $greaterLevel0Finder->directories());
176 $this->assertCount(5, $greaterLevel0Finder->files());
178 $greaterOrEqualLevel0Finder = $finder->depth(
'>= 0');
179 $this->assertCount(11, $greaterOrEqualLevel0Finder);
180 $this->assertCount(4, $greaterOrEqualLevel0Finder->directories());
181 $this->assertCount(7, $greaterOrEqualLevel0Finder->files());
183 $lowerOrEqualLevel1Finder = $finder->depth(
'<= 1');
184 $this->assertCount(7, $lowerOrEqualLevel1Finder);
185 $this->assertCount(3, $lowerOrEqualLevel1Finder->directories());
186 $this->assertCount(4, $lowerOrEqualLevel1Finder->files());
188 $lowerLevel2Finder = $finder->depth(
'< 2');
189 $this->assertCount(7, $lowerLevel2Finder);
190 $this->assertCount(3, $lowerLevel2Finder->directories());
191 $this->assertCount(4, $lowerLevel2Finder->files());
193 $exactlyLevel2Finder = $finder->depth(2);
194 $this->assertCount(4, $exactlyLevel2Finder);
195 $this->assertCount(1, $exactlyLevel2Finder->directories());
196 $this->assertCount(3, $exactlyLevel2Finder->files());
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());
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());
222 $now = new \DateTimeImmutable(
'2019-03-30 13:00:00');
225 $fs->method(
'has')->willReturn(
true);
228 ->expects($this->atLeast(1))
229 ->method(
'getTimestamp')
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'),
241 $finder = (
new Finder($fs))->in([
'/']);
243 for ($i = 1; $i <= 7; $i++) {
244 $this->assertCount(8 - $i, $finder->date(
'>= 2019-03-30 1' . (2 + $i) .
':00')->files());
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());
262 $fs->method(
'has')->willReturn(
true);
264 $fs->expects($this->atLeast(1))
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),
277 $finder = (
new Finder($fs))->in([
'/']);
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());
285 $this->assertCount(3, $finder->size(
'> 1Mi')->files());
286 $this->assertCount(2, $finder->size(
'>= 1Gi')->files());
289 #[Depends('testFinderWillFilterFilesAndFoldersByCreationTimestamp')] 292 $finder = (
new Finder($fs))->in([
'/']);
294 $this->assertEquals(
'file_1.txt', $finder->files()->sortByTime()->getIterator()->current()->getPath());
296 'dir_1/dir_1_2/file_7.cpp',
297 $finder->files()->sortByTime()->reverseSorting()->getIterator()->current()->getPath()
300 $this->assertEquals(
'dir_1', $finder->sortByName()->getIterator()->current()->getPath());
301 $this->assertEquals(
'file_2.mp3', $finder->sortByName()->reverseSorting()->getIterator()->current()->getPath());
303 $this->assertEquals(
'dir_1', $finder->sortByType()->getIterator()->current()->getPath());
304 $this->assertEquals(
'file_2.mp3', $finder->sortByType()->reverseSorting()->getIterator()->current()->getPath());
306 $customSortFinder = $finder->sort(
function (
Metadata $left,
Metadata $right):
int {
307 if (
'dir_1/dir_1_1/file_5.cpp' === $left->
getPath()) {
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());
getFlatFileSystemStructure()
testFinderWillFindFilesAndFoldersInFlatStructure()
testSortingWorksAsExpected(Filesystem\Filesystem $fs)
This class provides the data size with additional information to remove the work to calculate the siz...
getNestedFileSystemStructure()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testFinderWillFindNoFilesOrFoldersInAnEmptyDirectory()
testFinderWillFilterFilesAndFoldersByCreationTimestamp()
testFinderWillFindFilesAndFoldersInNestedStructure()
testFinderWillFindFilesAndFoldersForACertainDirectoryDepth()
testFinderWillFilterFilesBySize()
testFinderWillNotSearchInExcludedFolders()