ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
FinderTest Class Reference
+ Inheritance diagram for FinderTest:
+ Collaboration diagram for FinderTest:

Public Member Functions

 testFinderWillFindNoFilesOrFoldersInAnEmptyDirectory ()
 
 testFinderWillFindFilesAndFoldersInFlatStructure ()
 
 testFinderWillFindFilesAndFoldersInNestedStructure ()
 
 testFinderWillFindFilesAndFoldersForACertainDirectoryDepth ()
 
 testFinderWillNotSearchInExcludedFolders ()
 
 testFinderWillFilterFilesAndFoldersByCreationTimestamp ()
 
 testFinderWillFilterFilesBySize ()
 
 testSortingWorksAsExpected (Filesystem\Filesystem $fs)
 

Private Member Functions

 getFlatFileSystemStructure ()
 
 getNestedFileSystemStructure ()
 

Detailed Description

Member Function Documentation

◆ getFlatFileSystemStructure()

FinderTest::getFlatFileSystemStructure ( )
private
Exceptions
ReflectionException

Definition at line 39 of file FinderTest.php.

39 : 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 }
This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:33
$path
Definition: ltiservices.php:30

References $path.

Referenced by testFinderWillFindFilesAndFoldersInFlatStructure().

+ Here is the caller graph for this function:

◆ getNestedFileSystemStructure()

FinderTest::getNestedFileSystemStructure ( )
private
Exceptions
ReflectionException

Definition at line 66 of file FinderTest.php.

66 : 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 }

References $path.

Referenced by testFinderWillFilterFilesAndFoldersByCreationTimestamp(), testFinderWillFilterFilesBySize(), testFinderWillFindFilesAndFoldersForACertainDirectoryDepth(), testFinderWillFindFilesAndFoldersInNestedStructure(), and testFinderWillNotSearchInExcludedFolders().

+ Here is the caller graph for this function:

◆ testFinderWillFilterFilesAndFoldersByCreationTimestamp()

FinderTest::testFinderWillFilterFilesAndFoldersByCreationTimestamp ( )
Exceptions
ReflectionException

Definition at line 220 of file FinderTest.php.

220 : Filesystem\Filesystem
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 }
getNestedFileSystemStructure()
Definition: FinderTest.php:66

References $path, and getNestedFileSystemStructure().

+ Here is the call graph for this function:

◆ testFinderWillFilterFilesBySize()

FinderTest::testFinderWillFilterFilesBySize ( )
Exceptions
ReflectionException

Definition at line 259 of file FinderTest.php.

259 : 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 }
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:31

References $path, and getNestedFileSystemStructure().

+ Here is the call graph for this function:

◆ testFinderWillFindFilesAndFoldersForACertainDirectoryDepth()

FinderTest::testFinderWillFindFilesAndFoldersForACertainDirectoryDepth ( )
Exceptions
ReflectionException

Definition at line 164 of file FinderTest.php.

164 : void
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 }

References getNestedFileSystemStructure().

+ Here is the call graph for this function:

◆ testFinderWillFindFilesAndFoldersInFlatStructure()

FinderTest::testFinderWillFindFilesAndFoldersInFlatStructure ( )
Exceptions
ReflectionException

Definition at line 140 of file FinderTest.php.

140 : void
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 }
getFlatFileSystemStructure()
Definition: FinderTest.php:39

References getFlatFileSystemStructure().

+ Here is the call graph for this function:

◆ testFinderWillFindFilesAndFoldersInNestedStructure()

FinderTest::testFinderWillFindFilesAndFoldersInNestedStructure ( )
Exceptions
ReflectionException

Definition at line 152 of file FinderTest.php.

152 : void
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 }

References getNestedFileSystemStructure().

+ Here is the call graph for this function:

◆ testFinderWillFindNoFilesOrFoldersInAnEmptyDirectory()

FinderTest::testFinderWillFindNoFilesOrFoldersInAnEmptyDirectory ( )
Exceptions
ReflectionException

Definition at line 124 of file FinderTest.php.

124 : void
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 }

◆ testFinderWillNotSearchInExcludedFolders()

FinderTest::testFinderWillNotSearchInExcludedFolders ( )
Exceptions
ReflectionException

Definition at line 202 of file FinderTest.php.

202 : void
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 }

References getNestedFileSystemStructure().

+ Here is the call graph for this function:

◆ testSortingWorksAsExpected()

FinderTest::testSortingWorksAsExpected ( Filesystem\Filesystem  $fs)

Definition at line 290 of file FinderTest.php.

290 : void
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 }
getPath()
The path to the file or directory.
Definition: Metadata.php:63

References ILIAS\Filesystem\DTO\Metadata\getPath().

+ Here is the call graph for this function:

The documentation for this class was generated from the following file: