ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
FlySystemDirectoryAccessTest.php
Go to the documentation of this file.
1 <?php
2 
20 
21 \Hamcrest\Util::registerGlobalFunctions();
22 
33 use Mockery;
41 
46 class FlySystemDirectoryAccessTest extends TestCase
47 {
49 
50  private \ILIAS\Filesystem\Provider\FlySystem\FlySystemDirectoryAccess|\Mockery\MockInterface $subject;
54  private \Mockery\LegacyMockInterface $filesystemMock;
55  private \ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess|\Mockery\MockInterface $fileAccessMock;
56 
60  protected function setUp(): void
61  {
62  parent::setUp();
63 
64  $this->filesystemMock = Mockery::mock(FilesystemOperator::class);
65  $this->fileAccessMock = Mockery::mock(FlySystemFileAccess::class);
66 
67  $this->subject = new FlySystemDirectoryAccess($this->filesystemMock, $this->fileAccessMock);
68  }
69 
74  public function testHasDirWhichShouldSucceed(): void
75  {
76  $path = '/path/to/dir';
77 
78  $this->filesystemMock
79  ->shouldReceive('directoryExists')
80  ->with($path)
81  ->andReturn(true);
82 
83  $exists = $this->subject->hasDir($path);
84  $this->assertTrue($exists);
85  }
86 
92  {
93  $path = '/path/to/file';
94 
95  $this->filesystemMock
96  ->shouldReceive('directoryExists')
97  ->with($path)
98  ->andReturn(false);
99 
100  $exists = $this->subject->hasDir($path);
101  $this->assertFalse($exists);
102  }
103 
109  {
110  $path = '/path/to/dir';
111 
112  $this->filesystemMock
113  ->shouldReceive('directoryExists')
114  ->with($path)
115  ->andReturn(false);
116 
117  $exists = $this->subject->hasDir($path);
118  $this->assertFalse($exists);
119  }
120 
125  public function testListContentsWhichShouldSucceed(): void
126  {
127  $path = '/path/to/dir';
128  $file = ['type' => 'file', 'path' => $path];
129  $dir = ['type' => 'dir', 'path' => $path];
130 
131  $content_list = [
132  FileAttributes::fromArray($file),
133  DirectoryAttributes::fromArray($dir)
134  ];
135  $contentListContainer = new DirectoryListing($content_list);
136 
137 
138 
139  $this->filesystemMock
140  ->shouldReceive('directoryExists')
141  ->once()
142  ->with($path)
143  ->andReturn(true)
144  ->getMock()
145  ->shouldReceive('listContents')
146  ->once()
147  ->withArgs([$path, false])
148  ->andReturn($contentListContainer);
149 
154  $content = $this->subject->listContents($path);
155 
156  $this->assertSame($content_list[0]['type'], $content[0]->getType());
157  $this->assertSame($content_list[0]['path'], $content[0]->getPath());
158  $this->assertTrue($content[0]->isFile());
159  $this->assertFalse($content[0]->isDir());
160  $this->assertSame($content_list[1]['type'], $content[1]->getType());
161  $this->assertSame($content_list[1]['path'], $content[1]->getPath());
162  $this->assertTrue($content[1]->isDir());
163  $this->assertFalse($content[1]->isFile());
164  }
165 
171  {
172  $path = '/path/to/dir';
173 
174  $this->filesystemMock
175  ->shouldReceive('directoryExists')
176  ->once()
177  ->andReturn(false);
178 
179  $this->expectException(DirectoryNotFoundException::class);
180  $this->expectExceptionMessage("Directory \"$path\" not found.");
181 
182  $this->subject->listContents($path);
183  }
184 
190  {
191  $path = '/path/to/dir';
192  $file = ['type' => 'file', 'path' => $path];
193  $dir = ['type' => 'dir'];
194 
195  $contentList = [
196  $file,
197  $dir,
198  $file
199  ];
200 
201  $contentListContainer = new DirectoryListing($contentList);
202 
203  $this->filesystemMock
204  ->shouldReceive('directoryExists')
205  ->once()
206  ->andReturn(false)
207  ->shouldReceive('listContents')
208  ->never()
209  ->andReturn($contentListContainer)
210  ->getMock()
211  ->shouldReceive('getMetadata')
212  ->never()
213  ->andReturn($dir);
214 
215  $this->expectException(IOException::class);
216  $this->expectExceptionMessage("Directory \"$path\" not found.");
217 
218  $this->subject->listContents($path);
219  }
220 
225  public function testCreateDirWhichShouldSucceed(): void
226  {
227  $path = '/path/to/dir';
228  $access = Visibility::PRIVATE_ACCESS;
229 
230  $this->filesystemMock
231  ->shouldReceive('createDirectory')
232  ->once()
233  ->withArgs([$path, ['visibility' => $access]])
234  ->andReturn(true);
235 
236  $this->subject->createDir($path, $access);
237  }
238 
244  {
245  $path = '/path/to/dir';
246  $access = Visibility::PRIVATE_ACCESS;
247 
248  $this->filesystemMock
249  ->shouldReceive('createDirectory')
250  ->once()
251  ->withArgs([$path, ['visibility' => $access]])
252  ->andThrow(UnableToCreateDirectory::class);
253 
254  $this->expectException(IOException::class);
255  $this->expectExceptionMessage("Could not create directory \"$path\"");
256 
257  $this->subject->createDir($path, $access);
258  }
259 
265  {
266  $path = '/path/to/dir';
267  $access = 'invalid';
268 
269  $this->expectException(\InvalidArgumentException::class);
270  $this->expectExceptionMessage("Invalid visibility expected public or private but got \"$access\".");
271 
272  $this->subject->createDir($path, $access);
273  }
274 
279  public function testCopyDirWhichShouldSucceed(): void
280  {
281  $srcPath = '/source/path/to/dir';
282  $destPath = '/dest/path/to/dir';
283 
287  $fileSourceList = [
288  new Metadata("$srcPath/hello1", MetadataType::FILE),
289  new Metadata("$srcPath/helloDir", MetadataType::DIRECTORY),
290  new Metadata("$srcPath/hello2", MetadataType::FILE),
291  new Metadata("$srcPath/hello/subhello1", MetadataType::FILE),
292  new Metadata("$srcPath/helloDir2", MetadataType::DIRECTORY),
293  new Metadata("$srcPath/hello3", MetadataType::FILE),
294  new Metadata("$srcPath/hello/subhello2", MetadataType::FILE),
295  ];
296 
297  $fileDestinationList = [];
298 
299  $subjectMock = Mockery::mock(
300  FlySystemDirectoryAccess::class . '[listContents, hasDir]',
301  [$this->filesystemMock, $this->fileAccessMock]
302  );
303  $subjectMock
304  ->shouldReceive('listContents')
305  ->withArgs([$srcPath, true])
306  ->once()
307  ->andReturn($fileSourceList)
308  ->getMock()
309  ->shouldReceive('listContents')
310  ->withArgs([$destPath, true])
311  ->once()
312  ->andReturn($fileDestinationList);
313 
314  $subjectMock
315  ->shouldReceive('hasDir')
316  ->with($srcPath)
317  ->once()
318  ->andReturn(true);
319 
320  $this->fileAccessMock
321  ->shouldReceive('copy')
322  ->withArgs([$fileSourceList[0]->getPath(), "$destPath/hello1"])
323  ->once()
324  ->getMock()
325  ->shouldReceive('copy')
326  ->withArgs([$fileSourceList[2]->getPath(), "$destPath/hello2"])
327  ->once()
328  ->getMock()
329  ->shouldReceive('copy')
330  ->withArgs([$fileSourceList[3]->getPath(), "$destPath/hello/subhello1"])
331  ->once()
332  ->getMock()
333  ->shouldReceive('copy')
334  ->withArgs([$fileSourceList[5]->getPath(), "$destPath/hello3"])
335  ->once()
336  ->getMock()
337  ->shouldReceive('copy')
338  ->withArgs([$fileSourceList[6]->getPath(), "$destPath/hello/subhello2"])
339  ->once()
340  ->getMock();
341 
342  $subjectMock->copyDir($srcPath, $destPath);
343  }
344 
349  public function testCopyDirWithDestinationListContentErrorWhichShouldSucceed(): void
350  {
351  $srcPath = '/source/path/to/dir';
352  $destPath = '/dest/path/to/dir';
353 
357  $fileSourceList = [
358  new Metadata("$srcPath/hello1", MetadataType::FILE),
359  new Metadata("$srcPath/helloDir", MetadataType::DIRECTORY),
360  new Metadata("$srcPath/hello2", MetadataType::FILE),
361  new Metadata("$srcPath/hello/subhello1", MetadataType::FILE),
362  new Metadata("$srcPath/helloDir2", MetadataType::DIRECTORY),
363  new Metadata("$srcPath/hello3", MetadataType::FILE),
364  new Metadata("$srcPath/hello/subhello2", MetadataType::FILE),
365  ];
366 
367  $subjectMock = Mockery::mock(
368  FlySystemDirectoryAccess::class . '[listContents, hasDir]',
369  [$this->filesystemMock, $this->fileAccessMock]
370  );
371  $subjectMock
372  ->shouldReceive('listContents')
373  ->withArgs([$srcPath, true])
374  ->once()
375  ->andReturn($fileSourceList)
376  ->getMock()
377  ->shouldReceive('listContents')
378  ->withArgs([$destPath, true])
379  ->once()
380  ->andThrow(UnableToRetrieveMetadata::class);
381 
382  $subjectMock
383  ->shouldReceive('hasDir')
384  ->with($srcPath)
385  ->once()
386  ->andReturn(true);
387 
388  $this->fileAccessMock
389  ->shouldReceive('copy')
390  ->withArgs([$fileSourceList[0]->getPath(), "$destPath/hello1"])
391  ->once()
392  ->getMock()
393  ->shouldReceive('copy')
394  ->withArgs([$fileSourceList[2]->getPath(), "$destPath/hello2"])
395  ->once()
396  ->getMock()
397  ->shouldReceive('copy')
398  ->withArgs([$fileSourceList[3]->getPath(), "$destPath/hello/subhello1"])
399  ->once()
400  ->getMock()
401  ->shouldReceive('copy')
402  ->withArgs([$fileSourceList[5]->getPath(), "$destPath/hello3"])
403  ->once()
404  ->getMock()
405  ->shouldReceive('copy')
406  ->withArgs([$fileSourceList[6]->getPath(), "$destPath/hello/subhello2"])
407  ->once()
408  ->getMock();
409 
410  $subjectMock->copyDir($srcPath, $destPath);
411  }
412 
417  public function testCopyDirWithFullDestinationDirWhichShouldFail(): void
418  {
419  $srcPath = '/source/path/to/dir';
420  $destPath = '/dest/path/to/dir';
421 
425  $fileDestinationList = [
426  new Metadata("$srcPath/hello1", MetadataType::FILE),
427  new Metadata("$srcPath/helloDir", MetadataType::DIRECTORY),
428  new Metadata("$srcPath/hello2", MetadataType::FILE),
429  new Metadata("$srcPath/hello/subhello1", MetadataType::FILE),
430  new Metadata("$srcPath/helloDir2", MetadataType::DIRECTORY),
431  new Metadata("$srcPath/hello3", MetadataType::FILE),
432  new Metadata("$srcPath/hello/subhello2", MetadataType::FILE),
433  ];
434 
435  $subjectMock = Mockery::mock(
436  FlySystemDirectoryAccess::class . '[listContents, hasDir]',
437  [$this->filesystemMock, $this->fileAccessMock]
438  );
439  $subjectMock
440  ->shouldReceive('listContents')
441  ->withArgs([$destPath, true])
442  ->once()
443  ->andReturn($fileDestinationList);
444 
445  $subjectMock
446  ->shouldReceive('hasDir')
447  ->with($srcPath)
448  ->once()
449  ->andReturn(true);
450 
451  $this->expectException(IOException::class);
452  $this->expectExceptionMessage("Destination \"$destPath\" is not empty can not copy files.");
453 
454  $subjectMock->copyDir($srcPath, $destPath);
455  }
456 
462  {
463  $srcPath = '/source/path/to/dir';
464  $destPath = '/dest/path/to/dir';
465 
466  $subjectMock = Mockery::mock(
467  FlySystemDirectoryAccess::class . '[hasDir]',
468  [$this->filesystemMock, $this->fileAccessMock]
469  );
470  $subjectMock
471  ->shouldReceive('hasDir')
472  ->with($srcPath)
473  ->once()
474  ->andReturn(false);
475 
476  $this->expectException(DirectoryNotFoundException::class);
477  $this->expectExceptionMessage("Directory \"$srcPath\" not found");
478 
479  $subjectMock->copyDir($srcPath, $destPath);
480  }
481 
486  public function testDeleteDirWhichShouldSucceed(): void
487  {
488  $path = '/directory/which/should/be/removed';
489 
490  $this->filesystemMock
491  ->shouldReceive('deleteDirectory')
492  ->once()
493  ->with($path)
494  ->getMock()
495  ->shouldReceive('has')
496  ->once()
497  ->with($path)
498  ->andReturn(false);
499 
500  $this->subject->deleteDir($path);
501  }
502 
508  {
509  $path = '';
510 
511  $this->filesystemMock
512  ->shouldReceive('deleteDirectory')
513  ->once()
514  ->with($path)
515  ->andThrow(\Exception::class);
516 
517  $this->expectException(IOException::class);
518  $this->expectExceptionMessage('Could not delete directory "".');
519 
520  $this->subject->deleteDir($path);
521  }
522 
528  {
529  $path = '/directory/which/should/be/removed';
530 
531  $this->filesystemMock
532  ->shouldReceive('deleteDirectory')
533  ->once()
534  ->with($path)
535  ->andThrow(\Exception::class);
536 
537  $this->expectException(IOException::class);
538  $this->expectExceptionMessage("Could not delete directory \"$path\".");
539 
540  $this->subject->deleteDir($path);
541  }
542 }
ILIAS Filesystem Provider FlySystem FlySystemDirectoryAccess Mockery MockInterface $subject
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const FILE
The subject is file.
$path
Definition: ltiservices.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ILIAS Filesystem Provider FlySystem FlySystemFileAccess Mockery MockInterface $fileAccessMock
This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:31
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:38
const DIRECTORY
The subject is a directory.