ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FlySystemFileAccessTest.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 FlySystemFileAccessTest extends TestCase
30 {
32 
36  private $subject;
40  private $filesystemMock;
41 
42 
47  protected function setUp()
48  {
49  parent::setUp();
50  date_default_timezone_set('Africa/Lagos');
51  $this->filesystemMock = Mockery::mock(FilesystemInterface::class);
52  $this->subject = new FlySystemFileAccess($this->filesystemMock);
53  }
54 
55 
60  public function testReadWhichShouldSucceed()
61  {
62  $fileContent = 'Test file content.';
63  $this->filesystemMock->shouldReceive('read')
64  ->once()
65  ->andReturn($fileContent);
66 
67  $actualContent = $this->subject->read('/path/to/your/file');
68  $this->assertSame($fileContent, $actualContent);
69  }
70 
76  {
77  $path = '/path/to/your/file';
78  $this->filesystemMock->shouldReceive('read')
79  ->with($path)
80  ->once()
81  ->andReturn(false);
82 
83  $this->expectException(IOException::class);
84  $this->expectExceptionMessage("Could not access the file \"$path\".");
85 
86  $this->subject->read($path);
87  }
88 
94  {
95  $path = '/path/to/your/file';
96  $this->filesystemMock->shouldReceive('read')
97  ->once()
98  ->with($path)
99  ->andThrow(FileNotFoundException::class);
100 
101  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
102  $this->expectExceptionMessage("File \"$path\" not found.");
103 
104  $this->subject->read($path);
105  }
106 
112  {
113  $mimeType = 'image/jpeg';
114  $this->filesystemMock->shouldReceive('getMimetype')
115  ->once()
116  ->andReturn($mimeType);
117 
118  $actualMimeType = $this->subject->getMimeType('/path/to/your/file');
119  $this->assertSame($mimeType, $actualMimeType);
120  }
121 
127  {
128  $path = '/path/to/your/file';
129  $this->filesystemMock->shouldReceive('getMimetype')
130  ->with($path)
131  ->once()
132  ->andReturn(false);
133 
134  $this->expectException(IOException::class);
135  $this->expectExceptionMessage("Could not determine the MIME type of the file \"$path\".");
136 
137  $this->subject->getMimeType($path);
138  }
139 
145  {
146  $path = '/path/to/your/file';
147  $this->filesystemMock->shouldReceive('getMimetype')
148  ->once()
149  ->with($path)
150  ->andThrow(FileNotFoundException::class);
151 
152  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
153  $this->expectExceptionMessage("File \"$path\" not found.");
154 
155  $this->subject->getMimeType($path);
156  }
157 
163  {
164  $timestamp = '06.02.2012';
165  $this->filesystemMock->shouldReceive('getTimestamp')
166  ->once()
167  ->andReturn($timestamp);
168 
169  $actualTimestamp = $this->subject->getTimestamp('/path/to/your/file');
170 
171  /*
172  * needs to be equals instead of same because == checks if the object content is the same and === seems to check the reference too
173  * eg.
174  * $a == $b => true
175  * $a === $b => false
176  * $a === $a => true
177  * $b === $b => true
178  *
179  * Danger; this is only the observed behaviour and was not documented at least the part with the === operator.
180  * Tested with DateTime objects (PHP 7.1.6)
181  */
182  $this->assertEquals(new \DateTime($timestamp), $actualTimestamp);
183  }
184 
190  {
191  $path = '/path/to/your/file';
192  $this->filesystemMock->shouldReceive('getTimestamp')
193  ->with($path)
194  ->once()
195  ->andReturn(false);
196 
197  $this->expectException(IOException::class);
198  $this->expectExceptionMessage("Could not lookup timestamp of the file \"$path\".");
199 
200  $this->subject->getTimestamp($path);
201  }
202 
208  {
209  $path = '/path/to/your/file';
210  $this->filesystemMock->shouldReceive('getTimestamp')
211  ->once()
212  ->with($path)
213  ->andThrow(FileNotFoundException::class);
214 
215  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
216  $this->expectExceptionMessage("File \"$path\" not found.");
217 
218  $this->subject->getTimestamp($path);
219  }
220 
226  {
227  $rawSize = 1024;
228  $size = new DataSize($rawSize, DataSize::KiB);
229  $delta = 0.00001; //floating point is never that precise.
230 
231  $this->filesystemMock->shouldReceive('getSize')
232  ->once()
233  ->andReturn($rawSize);
234 
235  $actualSize = $this->subject->getSize('/path/to/your/file', DataSize::KiB);
236  $this->assertSame($size->getSize(), $actualSize->getSize(), '', $delta);
237  }
238 
244  {
245  $path = '/path/to/your/file';
246  $this->filesystemMock->shouldReceive('getSize')
247  ->with($path)
248  ->once()
249  ->andReturn(false);
250 
251  $this->expectException(IOException::class);
252  $this->expectExceptionMessage("Could not calculate the file size of the file \"$path\".");
253 
254  $this->subject->getSize($path, DataSize::MiB);
255  }
256 
262  {
263  $path = '/path/to/your/file';
264  $this->filesystemMock->shouldReceive('getSize')
265  ->once()
266  ->with($path)
267  ->andThrow(FileNotFoundException::class);
268 
269  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
270  $this->expectExceptionMessage("File \"$path\" not found.");
271 
272  $this->subject->getSize($path, DataSize::GiB);
273  }
274 
280  {
281  $path = '/path/to/your/file';
282  $visibility = "private";
283 
284  $this->filesystemMock->shouldReceive('has')
285  ->once()
286  ->with($path)
287  ->andReturn(true);
288 
289  $this->filesystemMock->shouldReceive('setVisibility')
290  ->once()
291  ->withArgs([$path, $visibility])
292  ->andReturn(true);
293 
294  $operationSuccessful = $this->subject->setVisibility($path, $visibility);
295  $this->assertTrue($operationSuccessful);
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(false);
316 
317  $operationSuccessful = $this->subject->setVisibility($path, $visibility);
318  $this->assertFalse($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(false);
334 
335  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
336  $this->expectExceptionMessage("Path \"$path\" not found.");
337 
338  $this->subject->setVisibility($path, $visibility);
339  }
340 
346  {
347  $path = '/path/to/your/file';
348  $visibility = "not valid";
349 
350  $this->filesystemMock->shouldReceive('has')
351  ->once()
352  ->with($path)
353  ->andReturn(true);
354 
355  $this->expectException(\InvalidArgumentException::class);
356  $this->expectExceptionMessage("The access must be 'public' or 'private' but '$visibility' was given.");
357 
358  $this->subject->setVisibility($path, $visibility);
359  }
360 
366  {
367  $path = '/path/to/your/file';
368  $visibility = "private";
369 
370  $this->filesystemMock->shouldReceive('has')
371  ->once()
372  ->with($path)
373  ->andReturn(true);
374 
375  $this->filesystemMock->shouldReceive('getVisibility')
376  ->once()
377  ->with($path)
378  ->andReturn($visibility);
379 
380  $actualVisibility = $this->subject->getVisibility($path);
381  $this->assertSame($visibility, $actualVisibility);
382  }
383 
389  {
390  $path = '/path/to/your/file';
391 
392  $this->filesystemMock->shouldReceive('has')
393  ->once()
394  ->with($path)
395  ->andReturn(false);
396 
397  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
398  $this->expectExceptionMessage("Path \"$path\" not found.");
399 
400  $this->subject->getVisibility($path);
401  }
402 
408  {
409  $path = '/path/to/your/file';
410 
411  $this->filesystemMock->shouldReceive('has')
412  ->once()
413  ->with($path)
414  ->andReturn(true);
415 
416  $this->filesystemMock->shouldReceive('getVisibility')
417  ->once()
418  ->with($path)
419  ->andReturn(false);
420 
421  $this->expectException(IOException::class);
422  $this->expectExceptionMessage("Could not determine visibility for path '$path'.");
423 
424  $this->subject->getVisibility($path);
425  }
426 
431  public function testWriteWhichShouldSucceed()
432  {
433  $path = '/path/to/your/file';
434  $content = "some awesome content";
435 
436  $this->filesystemMock->shouldReceive('write')
437  ->once()
438  ->withArgs([$path, $content])
439  ->andReturn(true);
440 
441  $this->subject->write($path, $content);
442  }
443 
449  {
450  $path = '/path/to/your/file';
451  $content = "some awesome content";
452 
453  $this->filesystemMock->shouldReceive('write')
454  ->once()
455  ->withArgs([$path, $content])
456  ->andThrow(FileExistsException::class);
457 
458  $this->expectException(FileAlreadyExistsException::class);
459  $this->expectExceptionMessage("File \"$path\" already exists.");
460 
461  $this->subject->write($path, $content);
462  }
463 
469  {
470  $path = '/path/to/your/file';
471  $content = "some awesome content";
472 
473  $this->filesystemMock->shouldReceive('write')
474  ->once()
475  ->withArgs([$path, $content])
476  ->andReturn(false);
477 
478  $this->expectException(IOException::class);
479  $this->expectExceptionMessage("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
480 
481  $this->subject->write($path, $content);
482  }
483 
489  {
490  $path = '/path/to/your/file';
491  $content = "some awesome content";
492 
493  $this->filesystemMock->shouldReceive('update')
494  ->once()
495  ->withArgs([$path, $content])
496  ->andReturn(true);
497 
498  $this->subject->update($path, $content);
499  }
500 
506  {
507  $path = '/path/to/your/file';
508  $content = "some awesome content";
509 
510  $this->filesystemMock->shouldReceive('update')
511  ->once()
512  ->withArgs([$path, $content])
513  ->andReturn(false);
514 
515  $this->expectException(IOException::class);
516  $this->expectExceptionMessage("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
517 
518  $this->subject->update($path, $content);
519  }
520 
526  {
527  $path = '/path/to/your/file';
528  $content = "some awesome content";
529 
530  $this->filesystemMock->shouldReceive('update')
531  ->once()
532  ->withArgs([$path, $content])
533  ->andThrow(FileNotFoundException::class);
534 
535  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
536  $this->expectExceptionMessage("File \"$path\" was not found update failed.");
537 
538  $this->subject->update($path, $content);
539  }
540 
545  public function testPutWhichShouldSucceed()
546  {
547  $path = '/path/to/your/file';
548  $content = "some awesome content";
549 
550  $this->filesystemMock->shouldReceive('put')
551  ->once()
552  ->withArgs([$path, $content])
553  ->andReturn(true);
554 
555  $this->subject->put($path, $content);
556  }
557 
563  {
564  $path = '/path/to/your/file';
565  $content = "some awesome content";
566 
567  $this->filesystemMock->shouldReceive('put')
568  ->once()
569  ->withArgs([$path, $content])
570  ->andReturn(false);
571 
572  $this->expectException(IOException::class);
573  $this->expectExceptionMessage("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
574 
575  $this->subject->put($path, $content);
576  }
577 
583  {
584  $path = '/path/to/your/file';
585 
586  $this->filesystemMock->shouldReceive('delete')
587  ->once()
588  ->with($path)
589  ->andReturn(true);
590 
591  $this->subject->delete($path);
592  }
593 
599  {
600  $path = '/path/to/your/file';
601 
602  $this->filesystemMock->shouldReceive('delete')
603  ->once()
604  ->with($path)
605  ->andReturn(false);
606 
607  $this->expectException(IOException::class);
608  $this->expectExceptionMessage("Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable.");
609 
610  $this->subject->delete($path);
611  }
612 
618  {
619  $path = '/path/to/your/file';
620 
621  $this->filesystemMock->shouldReceive('delete')
622  ->once()
623  ->with($path)
624  ->andThrow(FileNotFoundException::class);
625 
626  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
627  $this->expectExceptionMessage("File \"$path\" was not found delete operation failed.");
628 
629  $this->subject->delete($path);
630  }
631 
639  {
640  $path = '/path/to/your/file';
641  $content = "awesome content";
642 
643  //partially mock the subject to intercept the method calls of the own object.
644  $this->subject = Mockery::mock(FlySystemFileAccess::class, [$this->filesystemMock])->makePartial();
645 
646  $this->subject
647  ->shouldReceive('read')
648  ->once()
649  ->with($path)
650  ->andReturn($content)
651  ->getMock()
652  ->shouldReceive('delete')
653  ->once()
654  ->with($path);
655 
656  $this->subject->readAndDelete($path);
657  }
658 
659 
665  {
666  $source = '/source/path';
667  $destination = '/dest/path';
668 
669  $this->filesystemMock
670  ->shouldReceive('rename')
671  ->once()
672  ->withArgs([$source, $destination])
673  ->andReturn(true);
674 
675  $this->subject->rename($source, $destination);
676  }
677 
683  {
684  $source = '/source/path';
685  $destination = '/dest/path';
686 
687  $this->filesystemMock
688  ->shouldReceive('rename')
689  ->once()
690  ->withArgs([$source, $destination])
691  ->andThrow(FileNotFoundException::class);
692 
693  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
694  $this->expectExceptionMessage("File \"$source\" not found.");
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(FileExistsException::class);
713 
714  $this->expectException(FileAlreadyExistsException::class);
715  $this->expectExceptionMessage("File \"$destination\" already exists.");
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  ->andReturn(false);
734 
735  $this->expectException(IOException::class);
736  $this->expectExceptionMessage("Could not move file from \"$source\" to \"$destination\".");
737 
738  $this->subject->rename($source, $destination);
739  }
740 
745  public function testCopyWhichShouldSucceed()
746  {
747  $sourcePath = '/path/to/your/source/file';
748  $destinationPath = '/path/to/your/destination/file';
749 
750  $this->filesystemMock->shouldReceive('copy')
751  ->once()
752  ->withArgs([$sourcePath, $destinationPath])
753  ->andReturn(true);
754 
755  $this->subject->copy($sourcePath, $destinationPath);
756  }
757 
763  {
764  $sourcePath = '/path/to/your/source/file';
765  $destinationPath = '/path/to/your/destination/file';
766 
767  $this->filesystemMock->shouldReceive('copy')
768  ->once()
769  ->withArgs([$sourcePath, $destinationPath])
770  ->andReturn(false);
771 
772  $this->expectException(IOException::class);
773  $this->expectExceptionMessage("Could not copy file \"$sourcePath\" to destination \"$destinationPath\" because a general IO error occurred. Please check that your destination is writable.");
774 
775  $this->subject->copy($sourcePath, $destinationPath);
776  }
777 
783  {
784  $sourcePath = '/path/to/your/source/file';
785  $destinationPath = '/path/to/your/destination/file';
786 
787  $this->filesystemMock->shouldReceive('copy')
788  ->once()
789  ->withArgs([$sourcePath, $destinationPath])
790  ->andThrow(FileNotFoundException::class);
791 
792  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
793  $this->expectExceptionMessage("File source \"$sourcePath\" was not found copy failed.");
794 
795  $this->subject->copy($sourcePath, $destinationPath);
796  }
797 
803  {
804  $sourcePath = '/path/to/your/source/file';
805  $destinationPath = '/path/to/your/destination/file';
806 
807  $this->filesystemMock->shouldReceive('copy')
808  ->once()
809  ->withArgs([$sourcePath, $destinationPath])
810  ->andThrow(FileExistsException::class);
811 
812  $this->expectException(FileAlreadyExistsException::class);
813  $this->expectExceptionMessage("File destination \"$destinationPath\" already exists copy failed.");
814 
815  $this->subject->copy($sourcePath, $destinationPath);
816  }
817 }
$path
Definition: aliased.php:25
$size
Definition: RandomTest.php:84
Exercise XML Parser which completes/updates a given file by an xml string.
Class BaseForm.
Class DataSize.
Definition: DataSize.php:15
$destination
setUp()
Sets up the fixture, for example, open a network connection.
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
$source
Definition: linkback.php:22
Class FlySystemFileAccessTest.