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