ILIAS  release_8 Revision v8.24
FlySystemFileAccessTest.php
Go to the documentation of this file.
1<?php
2
4
8use League\Flysystem\FileExistsException;
9use League\Flysystem\FileNotFoundException;
10use League\Flysystem\Filesystem;
11use League\Flysystem\FilesystemInterface;
12use Mockery;
13use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
14use Mockery\MockInterface;
15use PHPUnit\Framework\TestCase;
16use League\Flysystem\AdapterInterface;
17
18/******************************************************************************
19 *
20 * This file is part of ILIAS, a powerful learning management system.
21 *
22 * ILIAS is licensed with the GPL-3.0, you should have received a copy
23 * of said license along with the source code.
24 *
25 * If this is not the case or you just want to try ILIAS, you'll find
26 * us at:
27 * https://www.ilias.de
28 * https://github.com/ILIAS-eLearning
29 *
30 *****************************************************************************/
39class FlySystemFileAccessTest extends TestCase
40{
41 use MockeryPHPUnitIntegration;
42
43 private \ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess $subject;
51 private $adapterMock;
52
57 protected function setUp(): void
58 {
59 parent::setUp();
60
61 $this->filesystemMock = Mockery::mock(FilesystemInterface::class);
62 $this->adapterMock = Mockery::mock(AdapterInterface::class);
63 $this->subject = new FlySystemFileAccess($this->filesystemMock);
64 }
65
70 public function testReadWhichShouldSucceed(): void
71 {
72 $fileContent = 'Test file content.';
73
74 $this->adapterMock->shouldReceive('read')
75 ->once()
76 ->andReturn(['contents' => $fileContent]);
77
78 $this->adapterMock->shouldReceive('has')
79 ->once()
80 ->andReturn(true);
81
82 $this->filesystemMock->shouldReceive('getAdapter')
83 ->once()
84 ->andReturn($this->adapterMock);
85
86 $actualContent = $this->subject->read('/path/to/your/file');
87 $this->assertSame($fileContent, $actualContent);
88 }
89
95 {
96 $path = 'path/to/your/file';
97
98 $this->adapterMock->shouldReceive('has')
99 ->once()
100 ->andReturn(true);
101
102 $this->adapterMock->shouldReceive('read')
103 ->once()
104 ->andReturn(['contents' => false]);
105
106 $this->filesystemMock->shouldReceive('getAdapter')
107 ->once()
108 ->andReturn($this->adapterMock);
109
110 $this->expectException(IOException::class);
111 $this->expectExceptionMessage("Could not access the file \"$path\".");
112
113 $this->subject->read($path);
114 }
115
121 {
122 $path = 'path/to/your/file';
123
124 $this->adapterMock->shouldReceive('has')
125 ->once()
126 ->andReturn(false);
127
128 $this->filesystemMock->shouldReceive('getAdapter')
129 ->once()
130 ->andReturn($this->adapterMock);
131
132 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
133 $this->expectExceptionMessage("File \"$path\" not found.");
134
135 $this->subject->read('/' . $path);
136 }
137
142 public function testGetMimeTypeWhichShouldSucceed(): void
143 {
144 $mimeType = 'image/jpeg';
145 $this->filesystemMock->shouldReceive('getMimetype')
146 ->once()
147 ->andReturn($mimeType);
148
149 $actualMimeType = $this->subject->getMimeType('/path/to/your/file');
150 $this->assertSame($mimeType, $actualMimeType);
151 }
152
158 {
159 $path = '/path/to/your/file';
160 $this->filesystemMock->shouldReceive('getMimetype')
161 ->with($path)
162 ->once()
163 ->andReturn(false);
164
165 $this->expectException(IOException::class);
166 $this->expectExceptionMessage("Could not determine the MIME type of the file \"$path\".");
167
168 $this->subject->getMimeType($path);
169 }
170
176 {
177 $path = '/path/to/your/file';
178 $this->filesystemMock->shouldReceive('getMimetype')
179 ->once()
180 ->with($path)
181 ->andThrow(FileNotFoundException::class);
182
183 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
184 $this->expectExceptionMessage("File \"$path\" not found.");
185
186 $this->subject->getMimeType($path);
187 }
188
194 {
195 $timestamp = '06.02.2012';
196 $this->filesystemMock->shouldReceive('getTimestamp')
197 ->once()
198 ->andReturn($timestamp);
199
200 $actualTimestamp = $this->subject->getTimestamp('/path/to/your/file');
201
202 /*
203 * needs to be equals instead of same because == checks if the object content is the same and === seems to check the reference too
204 * eg.
205 * $a == $b => true
206 * $a === $b => false
207 * $a === $a => true
208 * $b === $b => true
209 *
210 * Danger; this is only the observed behaviour and was not documented at least the part with the === operator.
211 * Tested with DateTime objects (PHP 7.1.6)
212 */
213 $this->assertEquals(new \DateTime($timestamp), $actualTimestamp);
214 }
215
221 {
222 $path = '/path/to/your/file';
223 $this->filesystemMock->shouldReceive('getTimestamp')
224 ->with($path)
225 ->once()
226 ->andReturn(false);
227
228 $this->expectException(IOException::class);
229 $this->expectExceptionMessage("Could not lookup timestamp of the file \"$path\".");
230
231 $this->subject->getTimestamp($path);
232 }
233
239 {
240 $path = '/path/to/your/file';
241 $this->filesystemMock->shouldReceive('getTimestamp')
242 ->once()
243 ->with($path)
244 ->andThrow(FileNotFoundException::class);
245
246 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
247 $this->expectExceptionMessage("File \"$path\" not found.");
248
249 $this->subject->getTimestamp($path);
250 }
251
256 public function testGetSizeWhichShouldSucceed(): void
257 {
258 $rawSize = 1024;
259 $size = new DataSize($rawSize, DataSize::KiB);
260 $delta = 0.00001; //floating point is never that precise.
261
262 $this->filesystemMock->shouldReceive('getSize')
263 ->once()
264 ->andReturn($rawSize);
265
266 $actualSize = $this->subject->getSize('/path/to/your/file', DataSize::KiB);
267 $this->assertSame($size->getSize(), $actualSize->getSize(), '', $delta);
268 }
269
275 {
276 $path = '/path/to/your/file';
277 $this->filesystemMock->shouldReceive('getSize')
278 ->with($path)
279 ->once()
280 ->andReturn(false);
281
282 $this->expectException(IOException::class);
283 $this->expectExceptionMessage("Could not calculate the file size of the file \"$path\".");
284
285 $this->subject->getSize($path, DataSize::MiB);
286 }
287
293 {
294 $path = '/path/to/your/file';
295 $this->filesystemMock->shouldReceive('getSize')
296 ->once()
297 ->with($path)
298 ->andThrow(FileNotFoundException::class);
299
300 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
301 $this->expectExceptionMessage("File \"$path\" not found.");
302
303 $this->subject->getSize($path, DataSize::GiB);
304 }
305
311 {
312 $path = '/path/to/your/file';
313 $visibility = "private";
314
315 $this->filesystemMock->shouldReceive('has')
316 ->once()
317 ->with($path)
318 ->andReturn(true);
319
320 $this->filesystemMock->shouldReceive('setVisibility')
321 ->once()
322 ->withArgs([$path, $visibility])
323 ->andReturn(true);
324
325 $operationSuccessful = $this->subject->setVisibility($path, $visibility);
326 $this->assertTrue($operationSuccessful);
327 }
328
334 {
335 $path = '/path/to/your/file';
336 $visibility = "private";
337
338 $this->filesystemMock->shouldReceive('has')
339 ->once()
340 ->with($path)
341 ->andReturn(true);
342
343 $this->filesystemMock->shouldReceive('setVisibility')
344 ->once()
345 ->withArgs([$path, $visibility])
346 ->andReturn(false);
347
348 $operationSuccessful = $this->subject->setVisibility($path, $visibility);
349 $this->assertFalse($operationSuccessful);
350 }
351
357 {
358 $path = '/path/to/your/file';
359 $visibility = "private";
360
361 $this->filesystemMock->shouldReceive('has')
362 ->once()
363 ->with($path)
364 ->andReturn(false);
365
366 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
367 $this->expectExceptionMessage("Path \"$path\" not found.");
368
369 $this->subject->setVisibility($path, $visibility);
370 }
371
377 {
378 $path = '/path/to/your/file';
379 $visibility = "not valid";
380
381 $this->filesystemMock->shouldReceive('has')
382 ->once()
383 ->with($path)
384 ->andReturn(true);
385
386 $this->expectException(\InvalidArgumentException::class);
387 $this->expectExceptionMessage("The access must be 'public' or 'private' but '$visibility' was given.");
388
389 $this->subject->setVisibility($path, $visibility);
390 }
391
397 {
398 $path = '/path/to/your/file';
399 $visibility = "private";
400
401 $this->filesystemMock->shouldReceive('has')
402 ->once()
403 ->with($path)
404 ->andReturn(true);
405
406 $this->filesystemMock->shouldReceive('getVisibility')
407 ->once()
408 ->with($path)
409 ->andReturn($visibility);
410
411 $actualVisibility = $this->subject->getVisibility($path);
412 $this->assertSame($visibility, $actualVisibility);
413 }
414
420 {
421 $path = '/path/to/your/file';
422
423 $this->filesystemMock->shouldReceive('has')
424 ->once()
425 ->with($path)
426 ->andReturn(false);
427
428 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
429 $this->expectExceptionMessage("Path \"$path\" not found.");
430
431 $this->subject->getVisibility($path);
432 }
433
439 {
440 $path = '/path/to/your/file';
441
442 $this->filesystemMock->shouldReceive('has')
443 ->once()
444 ->with($path)
445 ->andReturn(true);
446
447 $this->filesystemMock->shouldReceive('getVisibility')
448 ->once()
449 ->with($path)
450 ->andReturn(false);
451
452 $this->expectException(IOException::class);
453 $this->expectExceptionMessage("Could not determine visibility for path '$path'.");
454
455 $this->subject->getVisibility($path);
456 }
457
462 public function testWriteWhichShouldSucceed(): void
463 {
464 $path = '/path/to/your/file';
465 $content = "some awesome content";
466
467 $this->filesystemMock->shouldReceive('write')
468 ->once()
469 ->withArgs([$path, $content])
470 ->andReturn(true);
471
472 $this->subject->write($path, $content);
473 }
474
480 {
481 $path = '/path/to/your/file';
482 $content = "some awesome content";
483
484 $this->filesystemMock->shouldReceive('write')
485 ->once()
486 ->withArgs([$path, $content])
487 ->andThrow(FileExistsException::class);
488
489 $this->expectException(FileAlreadyExistsException::class);
490 $this->expectExceptionMessage("File \"$path\" already exists.");
491
492 $this->subject->write($path, $content);
493 }
494
500 {
501 $path = '/path/to/your/file';
502 $content = "some awesome content";
503
504 $this->filesystemMock->shouldReceive('write')
505 ->once()
506 ->withArgs([$path, $content])
507 ->andReturn(false);
508
509 $this->expectException(IOException::class);
510 $this->expectExceptionMessage("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
511
512 $this->subject->write($path, $content);
513 }
514
519 public function testUpdateWhichShouldSucceed(): void
520 {
521 $path = '/path/to/your/file';
522 $content = "some awesome content";
523
524 $this->filesystemMock->shouldReceive('update')
525 ->once()
526 ->withArgs([$path, $content])
527 ->andReturn(true);
528
529 $this->subject->update($path, $content);
530 }
531
537 {
538 $path = '/path/to/your/file';
539 $content = "some awesome content";
540
541 $this->filesystemMock->shouldReceive('update')
542 ->once()
543 ->withArgs([$path, $content])
544 ->andReturn(false);
545
546 $this->expectException(IOException::class);
547 $this->expectExceptionMessage("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
548
549 $this->subject->update($path, $content);
550 }
551
557 {
558 $path = '/path/to/your/file';
559 $content = "some awesome content";
560
561 $this->filesystemMock->shouldReceive('update')
562 ->once()
563 ->withArgs([$path, $content])
564 ->andThrow(FileNotFoundException::class);
565
566 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
567 $this->expectExceptionMessage("File \"$path\" was not found update failed.");
568
569 $this->subject->update($path, $content);
570 }
571
576 public function testPutWhichShouldSucceed(): void
577 {
578 $path = '/path/to/your/file';
579 $content = "some awesome content";
580
581 $this->filesystemMock->shouldReceive('put')
582 ->once()
583 ->withArgs([$path, $content])
584 ->andReturn(true);
585
586 $this->subject->put($path, $content);
587 }
588
594 {
595 $path = '/path/to/your/file';
596 $content = "some awesome content";
597
598 $this->filesystemMock->shouldReceive('put')
599 ->once()
600 ->withArgs([$path, $content])
601 ->andReturn(false);
602
603 $this->expectException(IOException::class);
604 $this->expectExceptionMessage("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
605
606 $this->subject->put($path, $content);
607 }
608
613 public function testDeleteWhichShouldSucceed(): void
614 {
615 $path = '/path/to/your/file';
616
617 $this->filesystemMock->shouldReceive('delete')
618 ->once()
619 ->with($path)
620 ->andReturn(true);
621
622 $this->subject->delete($path);
623 }
624
630 {
631 $path = '/path/to/your/file';
632
633 $this->filesystemMock->shouldReceive('delete')
634 ->once()
635 ->with($path)
636 ->andReturn(false);
637
638 $this->expectException(IOException::class);
639 $this->expectExceptionMessage("Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable.");
640
641 $this->subject->delete($path);
642 }
643
649 {
650 $path = '/path/to/your/file';
651
652 $this->filesystemMock->shouldReceive('delete')
653 ->once()
654 ->with($path)
655 ->andThrow(FileNotFoundException::class);
656
657 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
658 $this->expectExceptionMessage("File \"$path\" was not found delete operation failed.");
659
660 $this->subject->delete($path);
661 }
662
669 {
670 $path = '/path/to/your/file';
671 $content = "awesome content";
672
673 //partially mock the subject to intercept the method calls of the own object.
674 $this->subject = Mockery::mock(FlySystemFileAccess::class, [$this->filesystemMock])->makePartial();
675
676 $this->subject
677 ->shouldReceive('read')
678 ->once()
679 ->with($path)
680 ->andReturn($content)
681 ->getMock()
682 ->shouldReceive('delete')
683 ->once()
684 ->with($path);
685
686 $this->subject->readAndDelete($path);
687 }
688
693 public function testRenameWhichShouldSucceed(): void
694 {
695 $source = '/source/path';
696 $destination = '/dest/path';
697
698 $this->filesystemMock
699 ->shouldReceive('rename')
700 ->once()
701 ->withArgs([$source, $destination])
702 ->andReturn(true);
703
704 $this->subject->rename($source, $destination);
705 }
706
712 {
713 $source = '/source/path';
714 $destination = '/dest/path';
715
716 $this->filesystemMock
717 ->shouldReceive('rename')
718 ->once()
719 ->withArgs([$source, $destination])
720 ->andThrow(FileNotFoundException::class);
721
722 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
723 $this->expectExceptionMessage("File \"$source\" not found.");
724
725 $this->subject->rename($source, $destination);
726 }
727
733 {
734 $source = '/source/path';
735 $destination = '/dest/path';
736
737 $this->filesystemMock
738 ->shouldReceive('rename')
739 ->once()
740 ->withArgs([$source, $destination])
741 ->andThrow(FileExistsException::class);
742
743 $this->expectException(FileAlreadyExistsException::class);
744 $this->expectExceptionMessage("File \"$destination\" already exists.");
745
746 $this->subject->rename($source, $destination);
747 }
748
754 {
755 $source = '/source/path';
756 $destination = '/dest/path';
757
758 $this->filesystemMock
759 ->shouldReceive('rename')
760 ->once()
761 ->withArgs([$source, $destination])
762 ->andReturn(false);
763
764 $this->expectException(IOException::class);
765 $this->expectExceptionMessage("Could not move file from \"$source\" to \"$destination\".");
766
767 $this->subject->rename($source, $destination);
768 }
769
774 public function testCopyWhichShouldSucceed(): void
775 {
776 $sourcePath = '/path/to/your/source/file';
777 $destinationPath = '/path/to/your/destination/file';
778
779 $this->filesystemMock->shouldReceive('copy')
780 ->once()
781 ->withArgs([$sourcePath, $destinationPath])
782 ->andReturn(true);
783
784 $this->subject->copy($sourcePath, $destinationPath);
785 }
786
792 {
793 $sourcePath = '/path/to/your/source/file';
794 $destinationPath = '/path/to/your/destination/file';
795
796 $this->filesystemMock->shouldReceive('copy')
797 ->once()
798 ->withArgs([$sourcePath, $destinationPath])
799 ->andReturn(false);
800
801 $this->expectException(IOException::class);
802 $this->expectExceptionMessage("Could not copy file \"$sourcePath\" to destination \"$destinationPath\" because a general IO error occurred. Please check that your destination is writable.");
803
804 $this->subject->copy($sourcePath, $destinationPath);
805 }
806
812 {
813 $sourcePath = '/path/to/your/source/file';
814 $destinationPath = '/path/to/your/destination/file';
815
816 $this->filesystemMock->shouldReceive('copy')
817 ->once()
818 ->withArgs([$sourcePath, $destinationPath])
819 ->andThrow(FileNotFoundException::class);
820
821 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
822 $this->expectExceptionMessage("File source \"$sourcePath\" was not found copy failed.");
823
824 $this->subject->copy($sourcePath, $destinationPath);
825 }
826
832 {
833 $sourcePath = '/path/to/your/source/file';
834 $destinationPath = '/path/to/your/destination/file';
835
836 $this->filesystemMock->shouldReceive('copy')
837 ->once()
838 ->withArgs([$sourcePath, $destinationPath])
839 ->andThrow(FileExistsException::class);
840
841 $this->expectException(FileAlreadyExistsException::class);
842 $this->expectExceptionMessage("File destination \"$destinationPath\" already exists copy failed.");
843
844 $this->subject->copy($sourcePath, $destinationPath);
845 }
846}
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:31
setUp()
Sets up the fixture, for example, open a network connection.
ILIAS Filesystem Provider FlySystem FlySystemFileAccess $subject
$path
Definition: ltiservices.php:32
$source
Definition: metadata.php:93
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
Class ChatMainBarProvider \MainMenu\Provider.