ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FlySystemDirectoryAccessTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 require_once('./libs/composer/vendor/autoload.php');
6 
14 use Mockery;
18 
29 class FlySystemDirectoryAccessTest extends TestCase
30 {
32 
36  private $subject;
40  private $filesystemMock;
44  private $fileAccessMock;
48  protected function setUp()
49  {
50  parent::setUp();
51 
52  $this->filesystemMock = Mockery::mock(FilesystemInterface::class);
53  $this->fileAccessMock = Mockery::mock(FlySystemFileAccess::class);
54 
55  $this->subject = new FlySystemDirectoryAccess($this->filesystemMock, $this->fileAccessMock);
56  }
57 
62  public function testHasDirWhichShouldSucceed()
63  {
64  $path = '/path/to/dir';
65  $metadata = [
66  'type' => 'dir',
67  'path' => $path,
68  'timestamp' => 10000000
69  ];
70 
71  $this->filesystemMock
72  ->shouldReceive('has')
73  ->with($path)
74  ->andReturn(true)
75  ->getMock()
76  ->shouldReceive('getMetadata')
77  ->once()
78  ->andReturn($metadata);
79 
80  $exists = $this->subject->hasDir($path);
81  $this->assertTrue($exists);
82  }
83 
89  {
90  $path = '/path/to/file';
91  $metadata = [
92  'type' => 'file',
93  'path' => $path,
94  'timestamp' => 10000000
95  ];
96 
97  $this->filesystemMock
98  ->shouldReceive('has')
99  ->with($path)
100  ->andReturn(true)
101  ->getMock()
102  ->shouldReceive('getMetadata')
103  ->once()
104  ->andReturn($metadata);
105 
106  $exists = $this->subject->hasDir($path);
107  $this->assertFalse($exists);
108  }
109 
115  {
116  $path = '/path/to/file';
117  $metadata = [
118  'path' => $path,
119  'timestamp' => 10000000
120  ];
121 
122  $this->filesystemMock
123  ->shouldReceive('has')
124  ->with($path)
125  ->andReturn(true)
126  ->getMock()
127  ->shouldReceive('getMetadata')
128  ->once()
129  ->andReturn($metadata);
130 
131  $this->expectException(IOException::class);
132  $this->expectExceptionMessage("Could not evaluate path type: \"$path\"");
133 
134  $exists = $this->subject->hasDir($path);
135  $this->assertFalse($exists);
136  }
137 
143  {
144  $path = '/path/to/dir';
145 
146  $this->filesystemMock
147  ->shouldReceive('has')
148  ->with($path)
149  ->andReturn(false);
150 
151  $exists = $this->subject->hasDir($path);
152  $this->assertFalse($exists);
153  }
154 
159  public function testListContentsWhichShouldSucceed()
160  {
161  $path = '/path/to/dir';
162  $file = [ 'type' => 'file', 'path' => $path ];
163  $dir = [ 'type' => 'dir', 'path' => $path ];
164 
165  $contentList = [
166  $file,
167  $dir
168  ];
169 
170  $this->filesystemMock
171  ->shouldReceive('listContents')
172  ->once()
173  ->withArgs([$path, boolValue()])
174  ->andReturn($contentList)
175  ->getMock()
176  ->shouldReceive('has')
177  ->once()
178  ->with($path)
179  ->andReturn(true)
180  ->getMock()
181  ->shouldReceive('getMetadata')
182  ->once()
183  ->with($path)
184  ->andReturn($dir);
185 
189  $content = $this->subject->listContents($path);
190 
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());
199  }
200 
206  {
207  $path = '/path/to/dir';
208 
209  $this->filesystemMock
210  ->shouldReceive('has')
211  ->once()
212  ->andReturn(false);
213 
214  $this->expectException(DirectoryNotFoundException::class);
215  $this->expectExceptionMessage("Directory \"$path\" not found.");
216 
217  $this->subject->listContents($path);
218  }
219 
225  {
226  $path = '/path/to/dir';
227  $file = [ 'type' => 'file', 'path' => $path ];
228  $dir = [ 'type' => 'dir'];
229 
230  $contentList = [
231  $file,
232  $dir,
233  $file
234  ];
235 
236  $this->filesystemMock
237  ->shouldReceive('listContents')
238  ->once()
239  ->andReturn($contentList)
240  ->getMock()
241  ->shouldReceive('has')
242  ->once()
243  ->andReturn(true)
244  ->getMock()
245  ->shouldReceive('getMetadata')
246  ->once()
247  ->andReturn($dir);
248 
249  $this->expectException(IOException::class);
250  $this->expectExceptionMessage("Invalid metadata received for path \"$path\"");
251 
252  $this->subject->listContents($path);
253  }
254 
260  {
261  $path = '/path/to/dir';
262  $access = Visibility::PRIVATE_ACCESS;
263 
264  $this->filesystemMock
265  ->shouldReceive('createDir')
266  ->once()
267  ->withArgs([$path, ['visibility' => $access]])
268  ->andReturn(true);
269 
270  $this->subject->createDir($path, $access);
271  }
272 
278  {
279  $path = '/path/to/dir';
280  $access = Visibility::PRIVATE_ACCESS;
281 
282  $this->filesystemMock
283  ->shouldReceive('createDir')
284  ->once()
285  ->withArgs([$path, ['visibility' => $access]])
286  ->andReturn(false);
287 
288  $this->expectException(IOException::class);
289  $this->expectExceptionMessage("Could not create directory \"$path\"");
290 
291  $this->subject->createDir($path, $access);
292  }
293 
299  {
300  $path = '/path/to/dir';
301  $access = 'invalid';
302 
303  $this->expectException(\InvalidArgumentException::class);
304  $this->expectExceptionMessage("Invalid visibility expected public or private but got \"$access\".");
305 
306  $this->subject->createDir($path, $access);
307  }
308 
313  public function testCopyDirWhichShouldSucceed()
314  {
315  $srcPath = '/source/path/to/dir';
316  $destPath = '/dest/path/to/dir';
317 
321  $fileSourceList = [
322  new Metadata("$srcPath/hello1", MetadataType::FILE),
323  new Metadata("$srcPath/helloDir", MetadataType::DIRECTORY),
324  new Metadata("$srcPath/hello2", MetadataType::FILE),
325  new Metadata("$srcPath/hello/subhello1", MetadataType::FILE),
326  new Metadata("$srcPath/helloDir2", MetadataType::DIRECTORY),
327  new Metadata("$srcPath/hello3", MetadataType::FILE),
328  new Metadata("$srcPath/hello/subhello2", MetadataType::FILE),
329  ];
330 
331  $fileDestinationList = [];
332 
333  $subjectMock = Mockery::mock(FlySystemDirectoryAccess::class . '[listContents, hasDir]', [$this->filesystemMock, $this->fileAccessMock]);
334  $subjectMock
335  ->shouldReceive('listContents')
336  ->withArgs([$srcPath, true])
337  ->once()
338  ->andReturn($fileSourceList)
339  ->getMock()
340  ->shouldReceive('listContents')
341  ->withArgs([$destPath, true])
342  ->once()
343  ->andReturn($fileDestinationList);
344 
345  $subjectMock
346  ->shouldReceive('hasDir')
347  ->with($srcPath)
348  ->once()
349  ->andReturn(true);
350 
351  $this->fileAccessMock
352  ->shouldReceive('copy')
353  ->withArgs([$fileSourceList[0]->getPath(), "$destPath/hello1"])
354  ->once()
355  ->getMock()
356  ->shouldReceive('copy')
357  ->withArgs([$fileSourceList[2]->getPath(), "$destPath/hello2"])
358  ->once()
359  ->getMock()
360  ->shouldReceive('copy')
361  ->withArgs([$fileSourceList[3]->getPath(), "$destPath/hello/subhello1"])
362  ->once()
363  ->getMock()
364  ->shouldReceive('copy')
365  ->withArgs([$fileSourceList[5]->getPath(), "$destPath/hello3"])
366  ->once()
367  ->getMock()
368  ->shouldReceive('copy')
369  ->withArgs([$fileSourceList[6]->getPath(), "$destPath/hello/subhello2"])
370  ->once()
371  ->getMock();
372 
373 
374  $subjectMock->copyDir($srcPath, $destPath);
375  }
376 
381  public function testCopyDirWithDestinationListContentErrorWhichShouldSucceed()
382  {
383  $srcPath = '/source/path/to/dir';
384  $destPath = '/dest/path/to/dir';
385 
389  $fileSourceList = [
390  new Metadata("$srcPath/hello1", MetadataType::FILE),
391  new Metadata("$srcPath/helloDir", MetadataType::DIRECTORY),
392  new Metadata("$srcPath/hello2", MetadataType::FILE),
393  new Metadata("$srcPath/hello/subhello1", MetadataType::FILE),
394  new Metadata("$srcPath/helloDir2", MetadataType::DIRECTORY),
395  new Metadata("$srcPath/hello3", MetadataType::FILE),
396  new Metadata("$srcPath/hello/subhello2", MetadataType::FILE),
397  ];
398 
399  $subjectMock = Mockery::mock(FlySystemDirectoryAccess::class . '[listContents, hasDir]', [$this->filesystemMock, $this->fileAccessMock]);
400  $subjectMock
401  ->shouldReceive('listContents')
402  ->withArgs([$srcPath, true])
403  ->once()
404  ->andReturn($fileSourceList)
405  ->getMock()
406  ->shouldReceive('listContents')
407  ->withArgs([$destPath, true])
408  ->once()
409  ->andThrow(DirectoryNotFoundException::class);
410 
411  $subjectMock
412  ->shouldReceive('hasDir')
413  ->with($srcPath)
414  ->once()
415  ->andReturn(true);
416 
417  $this->fileAccessMock
418  ->shouldReceive('copy')
419  ->withArgs([$fileSourceList[0]->getPath(), "$destPath/hello1"])
420  ->once()
421  ->getMock()
422  ->shouldReceive('copy')
423  ->withArgs([$fileSourceList[2]->getPath(), "$destPath/hello2"])
424  ->once()
425  ->getMock()
426  ->shouldReceive('copy')
427  ->withArgs([$fileSourceList[3]->getPath(), "$destPath/hello/subhello1"])
428  ->once()
429  ->getMock()
430  ->shouldReceive('copy')
431  ->withArgs([$fileSourceList[5]->getPath(), "$destPath/hello3"])
432  ->once()
433  ->getMock()
434  ->shouldReceive('copy')
435  ->withArgs([$fileSourceList[6]->getPath(), "$destPath/hello/subhello2"])
436  ->once()
437  ->getMock();
438 
439 
440  $subjectMock->copyDir($srcPath, $destPath);
441  }
442 
447  public function testCopyDirWithFullDestinationDirWhichShouldFail()
448  {
449  $srcPath = '/source/path/to/dir';
450  $destPath = '/dest/path/to/dir';
451 
455  $fileDestinationList = [
456  new Metadata("$srcPath/hello1", MetadataType::FILE),
457  new Metadata("$srcPath/helloDir", MetadataType::DIRECTORY),
458  new Metadata("$srcPath/hello2", MetadataType::FILE),
459  new Metadata("$srcPath/hello/subhello1", MetadataType::FILE),
460  new Metadata("$srcPath/helloDir2", MetadataType::DIRECTORY),
461  new Metadata("$srcPath/hello3", MetadataType::FILE),
462  new Metadata("$srcPath/hello/subhello2", MetadataType::FILE),
463  ];
464 
465  $subjectMock = Mockery::mock(FlySystemDirectoryAccess::class . '[listContents, hasDir]', [$this->filesystemMock, $this->fileAccessMock]);
466  $subjectMock
467  ->shouldReceive('listContents')
468  ->withArgs([$destPath, true])
469  ->once()
470  ->andReturn($fileDestinationList);
471 
472  $subjectMock
473  ->shouldReceive('hasDir')
474  ->with($srcPath)
475  ->once()
476  ->andReturn(true);
477 
478  $this->expectException(IOException::class);
479  $this->expectExceptionMessage("Destination \"$destPath\" is not empty can not copy files.");
480 
481  $subjectMock->copyDir($srcPath, $destPath);
482  }
483 
489  {
490  $srcPath = '/source/path/to/dir';
491  $destPath = '/dest/path/to/dir';
492 
493  $subjectMock = Mockery::mock(FlySystemDirectoryAccess::class . '[hasDir]', [$this->filesystemMock, $this->fileAccessMock]);
494  $subjectMock
495  ->shouldReceive('hasDir')
496  ->with($srcPath)
497  ->once()
498  ->andReturn(false);
499 
500  $this->expectException(DirectoryNotFoundException::class);
501  $this->expectExceptionMessage("Directory \"$srcPath\" not found");
502 
503  $subjectMock->copyDir($srcPath, $destPath);
504  }
505 
511  {
512  $path = '/directory/which/should/be/removed';
513 
514  $this->filesystemMock
515  ->shouldReceive('deleteDir')
516  ->once()
517  ->with($path)
518  ->andReturn(true);
519 
520  $this->subject->deleteDir($path);
521  }
522 
528  {
529  $path = '';
530 
531  $this->filesystemMock
532  ->shouldReceive('deleteDir')
533  ->once()
534  ->with($path)
535  ->andThrow(RootViolationException::class);
536 
537  $this->expectException(IOException::class);
538  $this->expectExceptionMessage('The filesystem root must not be deleted.');
539 
540  $this->subject->deleteDir($path);
541  }
542 
548  {
549  $path = '/directory/which/should/be/removed';
550 
551  $this->filesystemMock
552  ->shouldReceive('deleteDir')
553  ->once()
554  ->with($path)
555  ->andReturn(false);
556 
557  $this->expectException(IOException::class);
558  $this->expectExceptionMessage("Could not delete directory \"$path\".");
559 
560  $this->subject->deleteDir($path);
561  }
562 }
$path
Definition: aliased.php:25
$metadata['__DYNAMIC:1__']
const FILE
The subject is file.
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:30
const DIRECTORY
The subject is a directory.