5require_once(
'./libs/composer/vendor/autoload.php');
6\Hamcrest\Util::registerGlobalFunctions();
13use League\Flysystem\FilesystemInterface;
14use League\Flysystem\RootViolationException;
16use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
17use Mockery\MockInterface;
18use PHPUnit\Framework\TestCase;
30 use MockeryPHPUnitIntegration;
48 protected function setUp() : void
52 $this->filesystemMock = Mockery::mock(FilesystemInterface::class);
53 $this->fileAccessMock = Mockery::mock(FlySystemFileAccess::class);
64 $path =
'/path/to/dir';
68 'timestamp' => 10000000
72 ->shouldReceive(
'has')
76 ->shouldReceive(
'getMetadata')
78 ->andReturn($metadata);
80 $exists = $this->subject->hasDir($path);
81 $this->assertTrue($exists);
90 $path =
'/path/to/file';
94 'timestamp' => 10000000
98 ->shouldReceive(
'has')
102 ->shouldReceive(
'getMetadata')
104 ->andReturn($metadata);
106 $exists = $this->subject->hasDir($path);
107 $this->assertFalse($exists);
116 $path =
'/path/to/file';
119 'timestamp' => 10000000
122 $this->filesystemMock
123 ->shouldReceive(
'has')
127 ->shouldReceive(
'getMetadata')
129 ->andReturn($metadata);
131 $this->expectException(IOException::class);
132 $this->expectExceptionMessage(
"Could not evaluate path type: \"$path\"");
134 $exists = $this->subject->hasDir($path);
135 $this->assertFalse($exists);
144 $path =
'/path/to/dir';
146 $this->filesystemMock
147 ->shouldReceive(
'has')
151 $exists = $this->subject->hasDir($path);
152 $this->assertFalse($exists);
159 public function testListContentsWhichShouldSucceed()
161 $path =
'/path/to/dir';
162 $file = [
'type' =>
'file',
'path' => $path];
163 $dir = [
'type' =>
'dir',
'path' => $path];
170 $this->filesystemMock
171 ->shouldReceive(
'listContents')
173 ->withArgs([$path, \boolValue()])
174 ->andReturn($contentList)
176 ->shouldReceive(
'has')
181 ->shouldReceive(
'getMetadata')
189 $content = $this->subject->listContents($path);
191 $this->assertSame($contentList[0][
'type'], $content[0]->getType());
192 $this->assertSame($contentList[0][
'path'], $content[0]->getPath());
193 $this->assertTrue($content[0]->isFile());
194 $this->assertFalse($content[0]->isDir());
195 $this->assertSame($contentList[1][
'type'], $content[1]->getType());
196 $this->assertSame($contentList[1][
'path'], $content[1]->getPath());
197 $this->assertTrue($content[1]->isDir());
198 $this->assertFalse($content[1]->isFile());
207 $path =
'/path/to/dir';
209 $this->filesystemMock
210 ->shouldReceive(
'has')
214 $this->expectException(DirectoryNotFoundException::class);
215 $this->expectExceptionMessage(
"Directory \"$path\" not found.");
217 $this->subject->listContents($path);
226 $path =
'/path/to/dir';
227 $file = [
'type' =>
'file',
'path' => $path];
228 $dir = [
'type' =>
'dir'];
236 $this->filesystemMock
237 ->shouldReceive(
'listContents')
239 ->andReturn($contentList)
241 ->shouldReceive(
'has')
245 ->shouldReceive(
'getMetadata')
249 $this->expectException(IOException::class);
250 $this->expectExceptionMessage(
"Invalid metadata received for path \"$path\"");
252 $this->subject->listContents($path);
261 $path =
'/path/to/dir';
264 $this->filesystemMock
265 ->shouldReceive(
'createDir')
267 ->withArgs([$path, [
'visibility' => $access]])
270 $this->subject->createDir($path, $access);
279 $path =
'/path/to/dir';
282 $this->filesystemMock
283 ->shouldReceive(
'createDir')
285 ->withArgs([$path, [
'visibility' => $access]])
288 $this->expectException(IOException::class);
289 $this->expectExceptionMessage(
"Could not create directory \"$path\"");
291 $this->subject->createDir($path, $access);
300 $path =
'/path/to/dir';
303 $this->expectException(\InvalidArgumentException::class);
304 $this->expectExceptionMessage(
"Invalid visibility expected public or private but got \"$access\".");
306 $this->subject->createDir($path, $access);
313 public function testCopyDirWhichShouldSucceed()
315 $srcPath =
'/source/path/to/dir';
316 $destPath =
'/dest/path/to/dir';
331 $fileDestinationList = [];
333 $subjectMock = Mockery::mock(FlySystemDirectoryAccess::class .
'[listContents, hasDir]',
334 [$this->filesystemMock, $this->fileAccessMock]);
336 ->shouldReceive(
'listContents')
337 ->withArgs([$srcPath,
true])
339 ->andReturn($fileSourceList)
341 ->shouldReceive(
'listContents')
342 ->withArgs([$destPath,
true])
344 ->andReturn($fileDestinationList);
347 ->shouldReceive(
'hasDir')
352 $this->fileAccessMock
353 ->shouldReceive(
'copy')
354 ->withArgs([$fileSourceList[0]->getPath(),
"$destPath/hello1"])
357 ->shouldReceive(
'copy')
358 ->withArgs([$fileSourceList[2]->getPath(),
"$destPath/hello2"])
361 ->shouldReceive(
'copy')
362 ->withArgs([$fileSourceList[3]->getPath(),
"$destPath/hello/subhello1"])
365 ->shouldReceive(
'copy')
366 ->withArgs([$fileSourceList[5]->getPath(),
"$destPath/hello3"])
369 ->shouldReceive(
'copy')
370 ->withArgs([$fileSourceList[6]->getPath(),
"$destPath/hello/subhello2"])
374 $subjectMock->copyDir($srcPath, $destPath);
381 public function testCopyDirWithDestinationListContentErrorWhichShouldSucceed()
383 $srcPath =
'/source/path/to/dir';
384 $destPath =
'/dest/path/to/dir';
399 $subjectMock = Mockery::mock(FlySystemDirectoryAccess::class .
'[listContents, hasDir]',
400 [$this->filesystemMock, $this->fileAccessMock]);
402 ->shouldReceive(
'listContents')
403 ->withArgs([$srcPath,
true])
405 ->andReturn($fileSourceList)
407 ->shouldReceive(
'listContents')
408 ->withArgs([$destPath,
true])
410 ->andThrow(DirectoryNotFoundException::class);
413 ->shouldReceive(
'hasDir')
418 $this->fileAccessMock
419 ->shouldReceive(
'copy')
420 ->withArgs([$fileSourceList[0]->getPath(),
"$destPath/hello1"])
423 ->shouldReceive(
'copy')
424 ->withArgs([$fileSourceList[2]->getPath(),
"$destPath/hello2"])
427 ->shouldReceive(
'copy')
428 ->withArgs([$fileSourceList[3]->getPath(),
"$destPath/hello/subhello1"])
431 ->shouldReceive(
'copy')
432 ->withArgs([$fileSourceList[5]->getPath(),
"$destPath/hello3"])
435 ->shouldReceive(
'copy')
436 ->withArgs([$fileSourceList[6]->getPath(),
"$destPath/hello/subhello2"])
440 $subjectMock->copyDir($srcPath, $destPath);
447 public function testCopyDirWithFullDestinationDirWhichShouldFail()
449 $srcPath =
'/source/path/to/dir';
450 $destPath =
'/dest/path/to/dir';
455 $fileDestinationList = [
465 $subjectMock = Mockery::mock(FlySystemDirectoryAccess::class .
'[listContents, hasDir]',
466 [$this->filesystemMock, $this->fileAccessMock]);
468 ->shouldReceive(
'listContents')
469 ->withArgs([$destPath,
true])
471 ->andReturn($fileDestinationList);
474 ->shouldReceive(
'hasDir')
479 $this->expectException(IOException::class);
480 $this->expectExceptionMessage(
"Destination \"$destPath\" is not empty can not copy files.");
482 $subjectMock->copyDir($srcPath, $destPath);
491 $srcPath =
'/source/path/to/dir';
492 $destPath =
'/dest/path/to/dir';
494 $subjectMock = Mockery::mock(FlySystemDirectoryAccess::class .
'[hasDir]',
495 [$this->filesystemMock, $this->fileAccessMock]);
497 ->shouldReceive(
'hasDir')
502 $this->expectException(DirectoryNotFoundException::class);
503 $this->expectExceptionMessage(
"Directory \"$srcPath\" not found");
505 $subjectMock->copyDir($srcPath, $destPath);
514 $path =
'/directory/which/should/be/removed';
516 $this->filesystemMock
517 ->shouldReceive(
'deleteDir')
522 $this->subject->deleteDir($path);
533 $this->filesystemMock
534 ->shouldReceive(
'deleteDir')
537 ->andThrow(RootViolationException::class);
539 $this->expectException(IOException::class);
540 $this->expectExceptionMessage(
'The filesystem root must not be deleted.');
542 $this->subject->deleteDir($path);
551 $path =
'/directory/which/should/be/removed';
553 $this->filesystemMock
554 ->shouldReceive(
'deleteDir')
559 $this->expectException(IOException::class);
560 $this->expectExceptionMessage(
"Could not delete directory \"$path\".");
562 $this->subject->deleteDir($path);
An exception for terminatinating execution or to throw for unit testing.
Class DirectoryNotFoundException Indicates that the directory is missing or not found.
Class IOException Indicates general problems with the input or output operations.
Class FlySystemDirectoryAccessTest.
testCreateDirWithGeneralErrorWhichShouldFail()
@Test @small
testHasDirWhichShouldSucceed()
@Test @small
testCreateDirWhichShouldSucceed()
@Test @small
testHasDirWithoutTypeInformationWhichShouldFail()
@Test @small
testCreateDirWithInvalidVisibilityWhichShouldFail()
@Test @small
testCopyDirWithMissingSourceDirWhichShouldFail()
@Test @small
testHasDirWithMissingDirWhichShouldSucceed()
@Test @small
testDeleteDirWithRootViolationWhichShouldFail()
@Test @small
testListContentsWithInvalidMetadataWhichShouldFail()
@Test @small
testHasDirWithFileTargetWhichShouldFail()
@Test @small
testDeleteDirWhichShouldSucceed()
@Test @small
testDeleteDirWithGeneralErrorWhichShouldFail()
@Test @small
testListContentsWithMissingRootDirectoryWhichShouldFail()
@Test @small
Class FlySystemDirectoryAccess.
Interface Visibility This interface provides the available options for the filesystem right managemen...
const PRIVATE_ACCESS
Private file visibility.