ILIAS  release_7 Revision v7.30-3-g800a261c036
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
28class FlySystemDirectoryAccessTest extends TestCase
29{
30 use MockeryPHPUnitIntegration;
31
35 private $subject;
44
48 protected function setUp() : void
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
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';
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';
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]',
334 [$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 $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]',
400 [$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 $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]',
466 [$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]',
495 [$this->filesystemMock, $this->fileAccessMock]);
496 $subjectMock
497 ->shouldReceive('hasDir')
498 ->with($srcPath)
499 ->once()
500 ->andReturn(false);
501
502 $this->expectException(DirectoryNotFoundException::class);
503 $this->expectExceptionMessage("Directory \"$srcPath\" not found");
504
505 $subjectMock->copyDir($srcPath, $destPath);
506 }
507
513 {
514 $path = '/directory/which/should/be/removed';
515
516 $this->filesystemMock
517 ->shouldReceive('deleteDir')
518 ->once()
519 ->with($path)
520 ->andReturn(true);
521
522 $this->subject->deleteDir($path);
523 }
524
530 {
531 $path = '';
532
533 $this->filesystemMock
534 ->shouldReceive('deleteDir')
535 ->once()
536 ->with($path)
537 ->andThrow(RootViolationException::class);
538
539 $this->expectException(IOException::class);
540 $this->expectExceptionMessage('The filesystem root must not be deleted.');
541
542 $this->subject->deleteDir($path);
543 }
544
550 {
551 $path = '/directory/which/should/be/removed';
552
553 $this->filesystemMock
554 ->shouldReceive('deleteDir')
555 ->once()
556 ->with($path)
557 ->andReturn(false);
558
559 $this->expectException(IOException::class);
560 $this->expectExceptionMessage("Could not delete directory \"$path\".");
561
562 $this->subject->deleteDir($path);
563 }
564}
An exception for terminatinating execution or to throw for unit testing.
Class Metadata This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:17
Class DirectoryNotFoundException Indicates that the directory is missing or not found.
Class IOException Indicates general problems with the input or output operations.
Definition: IOException.php:13
Class MetadataType The possible metadata types of the filesystem metadata.
const FILE
The subject is file.
const DIRECTORY
The subject is a directory.
Interface Visibility This interface provides the available options for the filesystem right managemen...
Definition: Visibility.php:16
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:27