26 Util::registerGlobalFunctions();
63 protected function setUp(): void
67 $this->filesystemMock = Mockery::mock(FilesystemOperator::class);
68 $this->fileAccessMock = Mockery::mock(FlySystemFileAccess::class);
77 $path =
'/path/to/dir';
80 ->shouldReceive(
'directoryExists')
84 $exists = $this->subject->hasDir(
$path);
85 $this->assertTrue($exists);
92 $path =
'/path/to/file';
95 ->shouldReceive(
'directoryExists')
99 $exists = $this->subject->hasDir(
$path);
100 $this->assertFalse($exists);
107 $path =
'/path/to/dir';
109 $this->filesystemMock
110 ->shouldReceive(
'directoryExists')
114 $exists = $this->subject->hasDir(
$path);
115 $this->assertFalse($exists);
120 public function testListContentsWhichShouldSucceed():
void 122 $path =
'/path/to/dir';
123 $file = [
'type' =>
'file',
'path' =>
$path];
124 $dir = [
'type' =>
'dir',
'path' =>
$path];
127 FileAttributes::fromArray($file),
128 DirectoryAttributes::fromArray($dir)
130 $contentListContainer =
new DirectoryListing($content_list);
134 $this->filesystemMock
135 ->shouldReceive(
'directoryExists')
140 ->shouldReceive(
'listContents')
142 ->withArgs([
$path,
false])
143 ->andReturn($contentListContainer);
149 $content = $this->subject->listContents(
$path);
151 $this->assertSame($content_list[0][
'type'], $content[0]->getType());
152 $this->assertSame($content_list[0][
'path'], $content[0]->getPath());
153 $this->assertTrue($content[0]->isFile());
154 $this->assertFalse($content[0]->isDir());
155 $this->assertSame($content_list[1][
'type'], $content[1]->getType());
156 $this->assertSame($content_list[1][
'path'], $content[1]->getPath());
157 $this->assertTrue($content[1]->isDir());
158 $this->assertFalse($content[1]->isFile());
165 $path =
'/path/to/dir';
167 $this->filesystemMock
168 ->shouldReceive(
'directoryExists')
172 $this->expectException(DirectoryNotFoundException::class);
173 $this->expectExceptionMessage(
"Directory \"$path\" not found.");
175 $this->subject->listContents(
$path);
182 $path =
'/path/to/dir';
183 $file = [
'type' =>
'file',
'path' =>
$path];
184 $dir = [
'type' =>
'dir'];
192 $contentListContainer =
new DirectoryListing($contentList);
194 $this->filesystemMock
195 ->shouldReceive(
'directoryExists')
198 ->shouldReceive(
'listContents')
200 ->andReturn($contentListContainer)
202 ->shouldReceive(
'getMetadata')
206 $this->expectException(IOException::class);
207 $this->expectExceptionMessage(
"Directory \"$path\" not found.");
209 $this->subject->listContents(
$path);
216 $path =
'/path/to/dir';
219 $this->filesystemMock
220 ->shouldReceive(
'createDirectory')
222 ->withArgs([
$path, [
'visibility' => $access]])
225 $this->subject->createDir(
$path, $access);
232 $path =
'/path/to/dir';
235 $this->filesystemMock
236 ->shouldReceive(
'createDirectory')
238 ->withArgs([
$path, [
'visibility' => $access]])
239 ->andThrow(UnableToCreateDirectory::class);
241 $this->expectException(IOException::class);
242 $this->expectExceptionMessage(
"Could not create directory \"$path\"");
244 $this->subject->createDir(
$path, $access);
251 $path =
'/path/to/dir';
254 $this->expectException(\InvalidArgumentException::class);
255 $this->expectExceptionMessage(
"Invalid visibility expected public or private but got \"$access\".");
257 $this->subject->createDir(
$path, $access);
262 public function testCopyDirWhichShouldSucceed():
void 264 $srcPath =
'/source/path/to/dir';
265 $destPath =
'/dest/path/to/dir';
280 $fileDestinationList = [];
282 $subjectMock = Mockery::mock(
283 FlySystemDirectoryAccess::class .
'[listContents, hasDir]',
284 [$this->filesystemMock, $this->fileAccessMock]
287 ->shouldReceive(
'listContents')
288 ->withArgs([$srcPath,
true])
290 ->andReturn($fileSourceList)
292 ->shouldReceive(
'listContents')
293 ->withArgs([$destPath,
true])
295 ->andReturn($fileDestinationList);
298 ->shouldReceive(
'hasDir')
303 $this->fileAccessMock
304 ->shouldReceive(
'copy')
305 ->withArgs([$fileSourceList[0]->getPath(),
"$destPath/hello1"])
308 ->shouldReceive(
'copy')
309 ->withArgs([$fileSourceList[2]->getPath(),
"$destPath/hello2"])
312 ->shouldReceive(
'copy')
313 ->withArgs([$fileSourceList[3]->getPath(),
"$destPath/hello/subhello1"])
316 ->shouldReceive(
'copy')
317 ->withArgs([$fileSourceList[5]->getPath(),
"$destPath/hello3"])
320 ->shouldReceive(
'copy')
321 ->withArgs([$fileSourceList[6]->getPath(),
"$destPath/hello/subhello2"])
325 $subjectMock->copyDir($srcPath, $destPath);
330 public function testCopyDirWithDestinationListContentErrorWhichShouldSucceed():
void 332 $srcPath =
'/source/path/to/dir';
333 $destPath =
'/dest/path/to/dir';
348 $subjectMock = Mockery::mock(
349 FlySystemDirectoryAccess::class .
'[listContents, hasDir]',
350 [$this->filesystemMock, $this->fileAccessMock]
353 ->shouldReceive(
'listContents')
354 ->withArgs([$srcPath,
true])
356 ->andReturn($fileSourceList)
358 ->shouldReceive(
'listContents')
359 ->withArgs([$destPath,
true])
361 ->andThrow(UnableToRetrieveMetadata::class);
364 ->shouldReceive(
'hasDir')
369 $this->fileAccessMock
370 ->shouldReceive(
'copy')
371 ->withArgs([$fileSourceList[0]->getPath(),
"$destPath/hello1"])
374 ->shouldReceive(
'copy')
375 ->withArgs([$fileSourceList[2]->getPath(),
"$destPath/hello2"])
378 ->shouldReceive(
'copy')
379 ->withArgs([$fileSourceList[3]->getPath(),
"$destPath/hello/subhello1"])
382 ->shouldReceive(
'copy')
383 ->withArgs([$fileSourceList[5]->getPath(),
"$destPath/hello3"])
386 ->shouldReceive(
'copy')
387 ->withArgs([$fileSourceList[6]->getPath(),
"$destPath/hello/subhello2"])
391 $subjectMock->copyDir($srcPath, $destPath);
396 public function testCopyDirWithFullDestinationDirWhichShouldFail():
void 398 $srcPath =
'/source/path/to/dir';
399 $destPath =
'/dest/path/to/dir';
404 $fileDestinationList = [
414 $subjectMock = Mockery::mock(
415 FlySystemDirectoryAccess::class .
'[listContents, hasDir]',
416 [$this->filesystemMock, $this->fileAccessMock]
419 ->shouldReceive(
'listContents')
420 ->withArgs([$destPath,
true])
422 ->andReturn($fileDestinationList);
425 ->shouldReceive(
'hasDir')
430 $this->expectException(IOException::class);
431 $this->expectExceptionMessage(
"Destination \"$destPath\" is not empty can not copy files.");
433 $subjectMock->copyDir($srcPath, $destPath);
440 $srcPath =
'/source/path/to/dir';
441 $destPath =
'/dest/path/to/dir';
443 $subjectMock = Mockery::mock(
444 FlySystemDirectoryAccess::class .
'[hasDir]',
445 [$this->filesystemMock, $this->fileAccessMock]
448 ->shouldReceive(
'hasDir')
453 $this->expectException(DirectoryNotFoundException::class);
454 $this->expectExceptionMessage(
"Directory \"$srcPath\" not found");
456 $subjectMock->copyDir($srcPath, $destPath);
463 $path =
'/directory/which/should/be/removed';
465 $this->filesystemMock
466 ->shouldReceive(
'deleteDirectory')
470 ->shouldReceive(
'has')
475 $this->subject->deleteDir(
$path);
484 $this->filesystemMock
485 ->shouldReceive(
'deleteDirectory')
488 ->andThrow(\Exception::class);
490 $this->expectException(IOException::class);
491 $this->expectExceptionMessage(
'Could not delete directory "".');
493 $this->subject->deleteDir(
$path);
500 $path =
'/directory/which/should/be/removed';
502 $this->filesystemMock
503 ->shouldReceive(
'deleteDirectory')
506 ->andThrow(\Exception::class);
508 $this->expectException(IOException::class);
509 $this->expectExceptionMessage(
"Could not delete directory \"$path\".");
511 $this->subject->deleteDir(
$path);
LegacyMockInterface $filesystemMock
testListContentsWithInvalidMetadataWhichShouldFail()
testListContentsWithMissingRootDirectoryWhichShouldFail()
Fly system file access implementation.
testHasDirWhichShouldSucceed()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testHasDirWithMissingDirWhichShouldSucceed()
FlySystemFileAccess MockInterface $fileAccessMock
FlySystemDirectoryAccess MockInterface $subject
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const PRIVATE_ACCESS
Private file visibility.
testCreateDirWithGeneralErrorWhichShouldFail()
testCreateDirWithInvalidVisibilityWhichShouldFail()
testCreateDirWhichShouldSucceed()
testDeleteDirWithRootViolationWhichShouldFail()
testDeleteDirWithGeneralErrorWhichShouldFail()
testCopyDirWithMissingSourceDirWhichShouldFail()
testHasDirWithFileTargetWhichShouldFail()
testDeleteDirWhichShouldSucceed()