ILIAS  release_7 Revision v7.30-3-g800a261c036
FlySystemFileAccessTest.php
Go to the documentation of this file.
1<?php
2
4
5require_once('./libs/composer/vendor/autoload.php');
6
10use League\Flysystem\FileExistsException;
11use League\Flysystem\FileNotFoundException;
12use League\Flysystem\Filesystem;
13use League\Flysystem\FilesystemInterface;
14use Mockery;
15use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
16use Mockery\MockInterface;
17use PHPUnit\Framework\TestCase;
18use League\Flysystem\AdapterInterface;
19
28class FlySystemFileAccessTest extends TestCase
29{
30 use MockeryPHPUnitIntegration;
31
35 private $subject;
43 private $adapterMock;
44
49 protected function setUp() : void
50 {
51 parent::setUp();
52
53 $this->filesystemMock = Mockery::mock(FilesystemInterface::class);
54 $this->adapterMock = Mockery::mock(AdapterInterface::class);
55 $this->subject = new FlySystemFileAccess($this->filesystemMock);
56 }
57
63 {
64 $fileContent = 'Test file content.';
65
66 $this->adapterMock->shouldReceive('read')
67 ->once()
68 ->andReturn(['contents' => $fileContent]);
69
70 $this->adapterMock->shouldReceive('has')
71 ->once()
72 ->andReturn(true);
73
74 $this->filesystemMock->shouldReceive('getAdapter')
75 ->once()
76 ->andReturn($this->adapterMock);
77
78 $actualContent = $this->subject->read('/path/to/your/file');
79 $this->assertSame($fileContent, $actualContent);
80 }
81
87 {
88 $path = 'path/to/your/file';
89
90 $this->adapterMock->shouldReceive('has')
91 ->once()
92 ->andReturn(true);
93
94 $this->adapterMock->shouldReceive('read')
95 ->once()
96 ->andReturn(['contents' => false]);
97
98 $this->filesystemMock->shouldReceive('getAdapter')
99 ->once()
100 ->andReturn($this->adapterMock);
101
102 $this->expectException(IOException::class);
103 $this->expectExceptionMessage("Could not access the file \"$path\".");
104
105 $this->subject->read($path);
106 }
107
113 {
114 $path = 'path/to/your/file';
115
116 $this->adapterMock->shouldReceive('has')
117 ->once()
118 ->andReturn(false);
119
120 $this->filesystemMock->shouldReceive('getAdapter')
121 ->once()
122 ->andReturn($this->adapterMock);
123
124 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
125 $this->expectExceptionMessage("File \"$path\" not found.");
126
127 $this->subject->read('/' . $path);
128 }
129
135 {
136 $mimeType = 'image/jpeg';
137 $this->filesystemMock->shouldReceive('getMimetype')
138 ->once()
139 ->andReturn($mimeType);
140
141 $actualMimeType = $this->subject->getMimeType('/path/to/your/file');
142 $this->assertSame($mimeType, $actualMimeType);
143 }
144
150 {
151 $path = '/path/to/your/file';
152 $this->filesystemMock->shouldReceive('getMimetype')
153 ->with($path)
154 ->once()
155 ->andReturn(false);
156
157 $this->expectException(IOException::class);
158 $this->expectExceptionMessage("Could not determine the MIME type of the file \"$path\".");
159
160 $this->subject->getMimeType($path);
161 }
162
168 {
169 $path = '/path/to/your/file';
170 $this->filesystemMock->shouldReceive('getMimetype')
171 ->once()
172 ->with($path)
173 ->andThrow(FileNotFoundException::class);
174
175 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
176 $this->expectExceptionMessage("File \"$path\" not found.");
177
178 $this->subject->getMimeType($path);
179 }
180
186 {
187 $timestamp = '06.02.2012';
188 $this->filesystemMock->shouldReceive('getTimestamp')
189 ->once()
190 ->andReturn($timestamp);
191
192 $actualTimestamp = $this->subject->getTimestamp('/path/to/your/file');
193
194 /*
195 * needs to be equals instead of same because == checks if the object content is the same and === seems to check the reference too
196 * eg.
197 * $a == $b => true
198 * $a === $b => false
199 * $a === $a => true
200 * $b === $b => true
201 *
202 * Danger; this is only the observed behaviour and was not documented at least the part with the === operator.
203 * Tested with DateTime objects (PHP 7.1.6)
204 */
205 $this->assertEquals(new \DateTime($timestamp), $actualTimestamp);
206 }
207
213 {
214 $path = '/path/to/your/file';
215 $this->filesystemMock->shouldReceive('getTimestamp')
216 ->with($path)
217 ->once()
218 ->andReturn(false);
219
220 $this->expectException(IOException::class);
221 $this->expectExceptionMessage("Could not lookup timestamp of the file \"$path\".");
222
223 $this->subject->getTimestamp($path);
224 }
225
231 {
232 $path = '/path/to/your/file';
233 $this->filesystemMock->shouldReceive('getTimestamp')
234 ->once()
235 ->with($path)
236 ->andThrow(FileNotFoundException::class);
237
238 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
239 $this->expectExceptionMessage("File \"$path\" not found.");
240
241 $this->subject->getTimestamp($path);
242 }
243
249 {
250 $rawSize = 1024;
251 $size = new DataSize($rawSize, DataSize::KiB);
252 $delta = 0.00001; //floating point is never that precise.
253
254 $this->filesystemMock->shouldReceive('getSize')
255 ->once()
256 ->andReturn($rawSize);
257
258 $actualSize = $this->subject->getSize('/path/to/your/file', DataSize::KiB);
259 $this->assertSame($size->getSize(), $actualSize->getSize(), '', $delta);
260 }
261
267 {
268 $path = '/path/to/your/file';
269 $this->filesystemMock->shouldReceive('getSize')
270 ->with($path)
271 ->once()
272 ->andReturn(false);
273
274 $this->expectException(IOException::class);
275 $this->expectExceptionMessage("Could not calculate the file size of the file \"$path\".");
276
277 $this->subject->getSize($path, DataSize::MiB);
278 }
279
285 {
286 $path = '/path/to/your/file';
287 $this->filesystemMock->shouldReceive('getSize')
288 ->once()
289 ->with($path)
290 ->andThrow(FileNotFoundException::class);
291
292 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
293 $this->expectExceptionMessage("File \"$path\" not found.");
294
295 $this->subject->getSize($path, DataSize::GiB);
296 }
297
303 {
304 $path = '/path/to/your/file';
305 $visibility = "private";
306
307 $this->filesystemMock->shouldReceive('has')
308 ->once()
309 ->with($path)
310 ->andReturn(true);
311
312 $this->filesystemMock->shouldReceive('setVisibility')
313 ->once()
314 ->withArgs([$path, $visibility])
315 ->andReturn(true);
316
317 $operationSuccessful = $this->subject->setVisibility($path, $visibility);
318 $this->assertTrue($operationSuccessful);
319 }
320
326 {
327 $path = '/path/to/your/file';
328 $visibility = "private";
329
330 $this->filesystemMock->shouldReceive('has')
331 ->once()
332 ->with($path)
333 ->andReturn(true);
334
335 $this->filesystemMock->shouldReceive('setVisibility')
336 ->once()
337 ->withArgs([$path, $visibility])
338 ->andReturn(false);
339
340 $operationSuccessful = $this->subject->setVisibility($path, $visibility);
341 $this->assertFalse($operationSuccessful);
342 }
343
349 {
350 $path = '/path/to/your/file';
351 $visibility = "private";
352
353 $this->filesystemMock->shouldReceive('has')
354 ->once()
355 ->with($path)
356 ->andReturn(false);
357
358 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
359 $this->expectExceptionMessage("Path \"$path\" not found.");
360
361 $this->subject->setVisibility($path, $visibility);
362 }
363
369 {
370 $path = '/path/to/your/file';
371 $visibility = "not valid";
372
373 $this->filesystemMock->shouldReceive('has')
374 ->once()
375 ->with($path)
376 ->andReturn(true);
377
378 $this->expectException(\InvalidArgumentException::class);
379 $this->expectExceptionMessage("The access must be 'public' or 'private' but '$visibility' was given.");
380
381 $this->subject->setVisibility($path, $visibility);
382 }
383
389 {
390 $path = '/path/to/your/file';
391 $visibility = "private";
392
393 $this->filesystemMock->shouldReceive('has')
394 ->once()
395 ->with($path)
396 ->andReturn(true);
397
398 $this->filesystemMock->shouldReceive('getVisibility')
399 ->once()
400 ->with($path)
401 ->andReturn($visibility);
402
403 $actualVisibility = $this->subject->getVisibility($path);
404 $this->assertSame($visibility, $actualVisibility);
405 }
406
412 {
413 $path = '/path/to/your/file';
414
415 $this->filesystemMock->shouldReceive('has')
416 ->once()
417 ->with($path)
418 ->andReturn(false);
419
420 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
421 $this->expectExceptionMessage("Path \"$path\" not found.");
422
423 $this->subject->getVisibility($path);
424 }
425
431 {
432 $path = '/path/to/your/file';
433
434 $this->filesystemMock->shouldReceive('has')
435 ->once()
436 ->with($path)
437 ->andReturn(true);
438
439 $this->filesystemMock->shouldReceive('getVisibility')
440 ->once()
441 ->with($path)
442 ->andReturn(false);
443
444 $this->expectException(IOException::class);
445 $this->expectExceptionMessage("Could not determine visibility for path '$path'.");
446
447 $this->subject->getVisibility($path);
448 }
449
455 {
456 $path = '/path/to/your/file';
457 $content = "some awesome content";
458
459 $this->filesystemMock->shouldReceive('write')
460 ->once()
461 ->withArgs([$path, $content])
462 ->andReturn(true);
463
464 $this->subject->write($path, $content);
465 }
466
472 {
473 $path = '/path/to/your/file';
474 $content = "some awesome content";
475
476 $this->filesystemMock->shouldReceive('write')
477 ->once()
478 ->withArgs([$path, $content])
479 ->andThrow(FileExistsException::class);
480
481 $this->expectException(FileAlreadyExistsException::class);
482 $this->expectExceptionMessage("File \"$path\" already exists.");
483
484 $this->subject->write($path, $content);
485 }
486
492 {
493 $path = '/path/to/your/file';
494 $content = "some awesome content";
495
496 $this->filesystemMock->shouldReceive('write')
497 ->once()
498 ->withArgs([$path, $content])
499 ->andReturn(false);
500
501 $this->expectException(IOException::class);
502 $this->expectExceptionMessage("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
503
504 $this->subject->write($path, $content);
505 }
506
512 {
513 $path = '/path/to/your/file';
514 $content = "some awesome content";
515
516 $this->filesystemMock->shouldReceive('update')
517 ->once()
518 ->withArgs([$path, $content])
519 ->andReturn(true);
520
521 $this->subject->update($path, $content);
522 }
523
529 {
530 $path = '/path/to/your/file';
531 $content = "some awesome content";
532
533 $this->filesystemMock->shouldReceive('update')
534 ->once()
535 ->withArgs([$path, $content])
536 ->andReturn(false);
537
538 $this->expectException(IOException::class);
539 $this->expectExceptionMessage("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
540
541 $this->subject->update($path, $content);
542 }
543
549 {
550 $path = '/path/to/your/file';
551 $content = "some awesome content";
552
553 $this->filesystemMock->shouldReceive('update')
554 ->once()
555 ->withArgs([$path, $content])
556 ->andThrow(FileNotFoundException::class);
557
558 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
559 $this->expectExceptionMessage("File \"$path\" was not found update failed.");
560
561 $this->subject->update($path, $content);
562 }
563
569 {
570 $path = '/path/to/your/file';
571 $content = "some awesome content";
572
573 $this->filesystemMock->shouldReceive('put')
574 ->once()
575 ->withArgs([$path, $content])
576 ->andReturn(true);
577
578 $this->subject->put($path, $content);
579 }
580
586 {
587 $path = '/path/to/your/file';
588 $content = "some awesome content";
589
590 $this->filesystemMock->shouldReceive('put')
591 ->once()
592 ->withArgs([$path, $content])
593 ->andReturn(false);
594
595 $this->expectException(IOException::class);
596 $this->expectExceptionMessage("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
597
598 $this->subject->put($path, $content);
599 }
600
606 {
607 $path = '/path/to/your/file';
608
609 $this->filesystemMock->shouldReceive('delete')
610 ->once()
611 ->with($path)
612 ->andReturn(true);
613
614 $this->subject->delete($path);
615 }
616
622 {
623 $path = '/path/to/your/file';
624
625 $this->filesystemMock->shouldReceive('delete')
626 ->once()
627 ->with($path)
628 ->andReturn(false);
629
630 $this->expectException(IOException::class);
631 $this->expectExceptionMessage("Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable.");
632
633 $this->subject->delete($path);
634 }
635
641 {
642 $path = '/path/to/your/file';
643
644 $this->filesystemMock->shouldReceive('delete')
645 ->once()
646 ->with($path)
647 ->andThrow(FileNotFoundException::class);
648
649 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
650 $this->expectExceptionMessage("File \"$path\" was not found delete operation failed.");
651
652 $this->subject->delete($path);
653 }
654
661 {
662 $path = '/path/to/your/file';
663 $content = "awesome content";
664
665 //partially mock the subject to intercept the method calls of the own object.
666 $this->subject = Mockery::mock(FlySystemFileAccess::class, [$this->filesystemMock])->makePartial();
667
668 $this->subject
669 ->shouldReceive('read')
670 ->once()
671 ->with($path)
672 ->andReturn($content)
673 ->getMock()
674 ->shouldReceive('delete')
675 ->once()
676 ->with($path);
677
678 $this->subject->readAndDelete($path);
679 }
680
686 {
687 $source = '/source/path';
688 $destination = '/dest/path';
689
690 $this->filesystemMock
691 ->shouldReceive('rename')
692 ->once()
693 ->withArgs([$source, $destination])
694 ->andReturn(true);
695
696 $this->subject->rename($source, $destination);
697 }
698
704 {
705 $source = '/source/path';
706 $destination = '/dest/path';
707
708 $this->filesystemMock
709 ->shouldReceive('rename')
710 ->once()
711 ->withArgs([$source, $destination])
712 ->andThrow(FileNotFoundException::class);
713
714 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
715 $this->expectExceptionMessage("File \"$source\" not found.");
716
717 $this->subject->rename($source, $destination);
718 }
719
725 {
726 $source = '/source/path';
727 $destination = '/dest/path';
728
729 $this->filesystemMock
730 ->shouldReceive('rename')
731 ->once()
732 ->withArgs([$source, $destination])
733 ->andThrow(FileExistsException::class);
734
735 $this->expectException(FileAlreadyExistsException::class);
736 $this->expectExceptionMessage("File \"$destination\" already exists.");
737
738 $this->subject->rename($source, $destination);
739 }
740
746 {
747 $source = '/source/path';
748 $destination = '/dest/path';
749
750 $this->filesystemMock
751 ->shouldReceive('rename')
752 ->once()
753 ->withArgs([$source, $destination])
754 ->andReturn(false);
755
756 $this->expectException(IOException::class);
757 $this->expectExceptionMessage("Could not move file from \"$source\" to \"$destination\".");
758
759 $this->subject->rename($source, $destination);
760 }
761
767 {
768 $sourcePath = '/path/to/your/source/file';
769 $destinationPath = '/path/to/your/destination/file';
770
771 $this->filesystemMock->shouldReceive('copy')
772 ->once()
773 ->withArgs([$sourcePath, $destinationPath])
774 ->andReturn(true);
775
776 $this->subject->copy($sourcePath, $destinationPath);
777 }
778
784 {
785 $sourcePath = '/path/to/your/source/file';
786 $destinationPath = '/path/to/your/destination/file';
787
788 $this->filesystemMock->shouldReceive('copy')
789 ->once()
790 ->withArgs([$sourcePath, $destinationPath])
791 ->andReturn(false);
792
793 $this->expectException(IOException::class);
794 $this->expectExceptionMessage("Could not copy file \"$sourcePath\" to destination \"$destinationPath\" because a general IO error occurred. Please check that your destination is writable.");
795
796 $this->subject->copy($sourcePath, $destinationPath);
797 }
798
804 {
805 $sourcePath = '/path/to/your/source/file';
806 $destinationPath = '/path/to/your/destination/file';
807
808 $this->filesystemMock->shouldReceive('copy')
809 ->once()
810 ->withArgs([$sourcePath, $destinationPath])
811 ->andThrow(FileNotFoundException::class);
812
813 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
814 $this->expectExceptionMessage("File source \"$sourcePath\" was not found copy failed.");
815
816 $this->subject->copy($sourcePath, $destinationPath);
817 }
818
824 {
825 $sourcePath = '/path/to/your/source/file';
826 $destinationPath = '/path/to/your/destination/file';
827
828 $this->filesystemMock->shouldReceive('copy')
829 ->once()
830 ->withArgs([$sourcePath, $destinationPath])
831 ->andThrow(FileExistsException::class);
832
833 $this->expectException(FileAlreadyExistsException::class);
834 $this->expectExceptionMessage("File destination \"$destinationPath\" already exists copy failed.");
835
836 $this->subject->copy($sourcePath, $destinationPath);
837 }
838}
$size
Definition: RandomTest.php:84
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
An exception for terminatinating execution or to throw for unit testing.
Class DataSize.
Definition: DataSize.php:16
Class IOException Indicates general problems with the input or output operations.
Definition: IOException.php:13
setUp()
Sets up the fixture, for example, open a network connection.
Class FlySystemFileAccess Fly system file access implementation.
$source
Definition: metadata.php:76
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
Class ChatMainBarProvider \MainMenu\Provider.