ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
FlySystemDirectoryAccessTestTBD.php
Go to the documentation of this file.
1<?php
2
20
21use Hamcrest\Util;
22use Mockery\LegacyMockInterface;
23use PHPUnit\Framework\Attributes\Test;
24use PHPUnit\Framework\Attributes\Small;
25
26Util::registerGlobalFunctions();
27
33use League\Flysystem\DirectoryListing;
34use League\Flysystem\FilesystemInterface;
35use League\Flysystem\FilesystemOperator;
36use Mockery;
37use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
38use Mockery\MockInterface;
39use PHPUnit\Framework\TestCase;
40use League\Flysystem\UnableToCreateDirectory;
41use League\Flysystem\UnableToRetrieveMetadata;
42use League\Flysystem\FileAttributes;
43use League\Flysystem\DirectoryAttributes;
44
49class FlySystemDirectoryAccessTest extends TestCase
50{
51 use MockeryPHPUnitIntegration;
52
53 private FlySystemDirectoryAccess|MockInterface $subject;
57 private LegacyMockInterface $filesystemMock;
58 private FlySystemFileAccess|MockInterface $fileAccessMock;
59
63 protected function setUp(): void
64 {
65 parent::setUp();
66
67 $this->filesystemMock = Mockery::mock(FilesystemOperator::class);
68 $this->fileAccessMock = Mockery::mock(FlySystemFileAccess::class);
69
70 $this->subject = new FlySystemDirectoryAccess($this->filesystemMock, $this->fileAccessMock);
71 }
72
73 #[Test]
74
75 public function testHasDirWhichShouldSucceed(): void
76 {
77 $path = '/path/to/dir';
78
79 $this->filesystemMock
80 ->shouldReceive('directoryExists')
81 ->with($path)
82 ->andReturn(true);
83
84 $exists = $this->subject->hasDir($path);
85 $this->assertTrue($exists);
86 }
87
88 #[Test]
89
91 {
92 $path = '/path/to/file';
93
94 $this->filesystemMock
95 ->shouldReceive('directoryExists')
96 ->with($path)
97 ->andReturn(false);
98
99 $exists = $this->subject->hasDir($path);
100 $this->assertFalse($exists);
101 }
102
103 #[Test]
104
106 {
107 $path = '/path/to/dir';
108
109 $this->filesystemMock
110 ->shouldReceive('directoryExists')
111 ->with($path)
112 ->andReturn(false);
113
114 $exists = $this->subject->hasDir($path);
115 $this->assertFalse($exists);
116 }
117
118 #[Test]
119
120 public function testListContentsWhichShouldSucceed(): void
121 {
122 $path = '/path/to/dir';
123 $file = ['type' => 'file', 'path' => $path];
124 $dir = ['type' => 'dir', 'path' => $path];
125
126 $content_list = [
127 FileAttributes::fromArray($file),
128 DirectoryAttributes::fromArray($dir)
129 ];
130 $contentListContainer = new DirectoryListing($content_list);
131
132
133
134 $this->filesystemMock
135 ->shouldReceive('directoryExists')
136 ->once()
137 ->with($path)
138 ->andReturn(true)
139 ->getMock()
140 ->shouldReceive('listContents')
141 ->once()
142 ->withArgs([$path, false])
143 ->andReturn($contentListContainer);
144
149 $content = $this->subject->listContents($path);
150
151 $this->assertSame($content_list[0]['type'], $content[0]->getType());
152 $this->assertSame($content_list[0]['path'], $content[0]->getPath());
153 $this->assertTrue($content[0]->isFile());
154 $this->assertFalse($content[0]->isDir());
155 $this->assertSame($content_list[1]['type'], $content[1]->getType());
156 $this->assertSame($content_list[1]['path'], $content[1]->getPath());
157 $this->assertTrue($content[1]->isDir());
158 $this->assertFalse($content[1]->isFile());
159 }
160
161 #[Test]
162
164 {
165 $path = '/path/to/dir';
166
167 $this->filesystemMock
168 ->shouldReceive('directoryExists')
169 ->once()
170 ->andReturn(false);
171
172 $this->expectException(DirectoryNotFoundException::class);
173 $this->expectExceptionMessage("Directory \"$path\" not found.");
174
175 $this->subject->listContents($path);
176 }
177
178 #[Test]
179
181 {
182 $path = '/path/to/dir';
183 $file = ['type' => 'file', 'path' => $path];
184 $dir = ['type' => 'dir'];
185
186 $contentList = [
187 $file,
188 $dir,
189 $file
190 ];
191
192 $contentListContainer = new DirectoryListing($contentList);
193
194 $this->filesystemMock
195 ->shouldReceive('directoryExists')
196 ->once()
197 ->andReturn(false)
198 ->shouldReceive('listContents')
199 ->never()
200 ->andReturn($contentListContainer)
201 ->getMock()
202 ->shouldReceive('getMetadata')
203 ->never()
204 ->andReturn($dir);
205
206 $this->expectException(IOException::class);
207 $this->expectExceptionMessage("Directory \"$path\" not found.");
208
209 $this->subject->listContents($path);
210 }
211
212 #[Test]
213
214 public function testCreateDirWhichShouldSucceed(): void
215 {
216 $path = '/path/to/dir';
218
219 $this->filesystemMock
220 ->shouldReceive('createDirectory')
221 ->once()
222 ->withArgs([$path, ['visibility' => $access]])
223 ->andReturn(true);
224
225 $this->subject->createDir($path, $access);
226 }
227
228 #[Test]
229
231 {
232 $path = '/path/to/dir';
234
235 $this->filesystemMock
236 ->shouldReceive('createDirectory')
237 ->once()
238 ->withArgs([$path, ['visibility' => $access]])
239 ->andThrow(UnableToCreateDirectory::class);
240
241 $this->expectException(IOException::class);
242 $this->expectExceptionMessage("Could not create directory \"$path\"");
243
244 $this->subject->createDir($path, $access);
245 }
246
247 #[Test]
248
250 {
251 $path = '/path/to/dir';
252 $access = 'invalid';
253
254 $this->expectException(\InvalidArgumentException::class);
255 $this->expectExceptionMessage("Invalid visibility expected public or private but got \"$access\".");
256
257 $this->subject->createDir($path, $access);
258 }
259
260 #[Test]
261
262 public function testCopyDirWhichShouldSucceed(): void
263 {
264 $srcPath = '/source/path/to/dir';
265 $destPath = '/dest/path/to/dir';
266
270 $fileSourceList = [
271 new Metadata("$srcPath/hello1", MetadataType::FILE),
272 new Metadata("$srcPath/helloDir", MetadataType::DIRECTORY),
273 new Metadata("$srcPath/hello2", MetadataType::FILE),
274 new Metadata("$srcPath/hello/subhello1", MetadataType::FILE),
275 new Metadata("$srcPath/helloDir2", MetadataType::DIRECTORY),
276 new Metadata("$srcPath/hello3", MetadataType::FILE),
277 new Metadata("$srcPath/hello/subhello2", MetadataType::FILE),
278 ];
279
280 $fileDestinationList = [];
281
282 $subjectMock = Mockery::mock(
283 FlySystemDirectoryAccess::class . '[listContents, hasDir]',
284 [$this->filesystemMock, $this->fileAccessMock]
285 );
286 $subjectMock
287 ->shouldReceive('listContents')
288 ->withArgs([$srcPath, true])
289 ->once()
290 ->andReturn($fileSourceList)
291 ->getMock()
292 ->shouldReceive('listContents')
293 ->withArgs([$destPath, true])
294 ->once()
295 ->andReturn($fileDestinationList);
296
297 $subjectMock
298 ->shouldReceive('hasDir')
299 ->with($srcPath)
300 ->once()
301 ->andReturn(true);
302
303 $this->fileAccessMock
304 ->shouldReceive('copy')
305 ->withArgs([$fileSourceList[0]->getPath(), "$destPath/hello1"])
306 ->once()
307 ->getMock()
308 ->shouldReceive('copy')
309 ->withArgs([$fileSourceList[2]->getPath(), "$destPath/hello2"])
310 ->once()
311 ->getMock()
312 ->shouldReceive('copy')
313 ->withArgs([$fileSourceList[3]->getPath(), "$destPath/hello/subhello1"])
314 ->once()
315 ->getMock()
316 ->shouldReceive('copy')
317 ->withArgs([$fileSourceList[5]->getPath(), "$destPath/hello3"])
318 ->once()
319 ->getMock()
320 ->shouldReceive('copy')
321 ->withArgs([$fileSourceList[6]->getPath(), "$destPath/hello/subhello2"])
322 ->once()
323 ->getMock();
324
325 $subjectMock->copyDir($srcPath, $destPath);
326 }
327
328 #[Test]
329
330 public function testCopyDirWithDestinationListContentErrorWhichShouldSucceed(): void
331 {
332 $srcPath = '/source/path/to/dir';
333 $destPath = '/dest/path/to/dir';
334
338 $fileSourceList = [
339 new Metadata("$srcPath/hello1", MetadataType::FILE),
340 new Metadata("$srcPath/helloDir", MetadataType::DIRECTORY),
341 new Metadata("$srcPath/hello2", MetadataType::FILE),
342 new Metadata("$srcPath/hello/subhello1", MetadataType::FILE),
343 new Metadata("$srcPath/helloDir2", MetadataType::DIRECTORY),
344 new Metadata("$srcPath/hello3", MetadataType::FILE),
345 new Metadata("$srcPath/hello/subhello2", MetadataType::FILE),
346 ];
347
348 $subjectMock = Mockery::mock(
349 FlySystemDirectoryAccess::class . '[listContents, hasDir]',
350 [$this->filesystemMock, $this->fileAccessMock]
351 );
352 $subjectMock
353 ->shouldReceive('listContents')
354 ->withArgs([$srcPath, true])
355 ->once()
356 ->andReturn($fileSourceList)
357 ->getMock()
358 ->shouldReceive('listContents')
359 ->withArgs([$destPath, true])
360 ->once()
361 ->andThrow(UnableToRetrieveMetadata::class);
362
363 $subjectMock
364 ->shouldReceive('hasDir')
365 ->with($srcPath)
366 ->once()
367 ->andReturn(true);
368
369 $this->fileAccessMock
370 ->shouldReceive('copy')
371 ->withArgs([$fileSourceList[0]->getPath(), "$destPath/hello1"])
372 ->once()
373 ->getMock()
374 ->shouldReceive('copy')
375 ->withArgs([$fileSourceList[2]->getPath(), "$destPath/hello2"])
376 ->once()
377 ->getMock()
378 ->shouldReceive('copy')
379 ->withArgs([$fileSourceList[3]->getPath(), "$destPath/hello/subhello1"])
380 ->once()
381 ->getMock()
382 ->shouldReceive('copy')
383 ->withArgs([$fileSourceList[5]->getPath(), "$destPath/hello3"])
384 ->once()
385 ->getMock()
386 ->shouldReceive('copy')
387 ->withArgs([$fileSourceList[6]->getPath(), "$destPath/hello/subhello2"])
388 ->once()
389 ->getMock();
390
391 $subjectMock->copyDir($srcPath, $destPath);
392 }
393
394 #[Test]
395
396 public function testCopyDirWithFullDestinationDirWhichShouldFail(): void
397 {
398 $srcPath = '/source/path/to/dir';
399 $destPath = '/dest/path/to/dir';
400
404 $fileDestinationList = [
405 new Metadata("$srcPath/hello1", MetadataType::FILE),
406 new Metadata("$srcPath/helloDir", MetadataType::DIRECTORY),
407 new Metadata("$srcPath/hello2", MetadataType::FILE),
408 new Metadata("$srcPath/hello/subhello1", MetadataType::FILE),
409 new Metadata("$srcPath/helloDir2", MetadataType::DIRECTORY),
410 new Metadata("$srcPath/hello3", MetadataType::FILE),
411 new Metadata("$srcPath/hello/subhello2", MetadataType::FILE),
412 ];
413
414 $subjectMock = Mockery::mock(
415 FlySystemDirectoryAccess::class . '[listContents, hasDir]',
416 [$this->filesystemMock, $this->fileAccessMock]
417 );
418 $subjectMock
419 ->shouldReceive('listContents')
420 ->withArgs([$destPath, true])
421 ->once()
422 ->andReturn($fileDestinationList);
423
424 $subjectMock
425 ->shouldReceive('hasDir')
426 ->with($srcPath)
427 ->once()
428 ->andReturn(true);
429
430 $this->expectException(IOException::class);
431 $this->expectExceptionMessage("Destination \"$destPath\" is not empty can not copy files.");
432
433 $subjectMock->copyDir($srcPath, $destPath);
434 }
435
436 #[Test]
437
439 {
440 $srcPath = '/source/path/to/dir';
441 $destPath = '/dest/path/to/dir';
442
443 $subjectMock = Mockery::mock(
444 FlySystemDirectoryAccess::class . '[hasDir]',
445 [$this->filesystemMock, $this->fileAccessMock]
446 );
447 $subjectMock
448 ->shouldReceive('hasDir')
449 ->with($srcPath)
450 ->once()
451 ->andReturn(false);
452
453 $this->expectException(DirectoryNotFoundException::class);
454 $this->expectExceptionMessage("Directory \"$srcPath\" not found");
455
456 $subjectMock->copyDir($srcPath, $destPath);
457 }
458
459 #[Test]
460
461 public function testDeleteDirWhichShouldSucceed(): void
462 {
463 $path = '/directory/which/should/be/removed';
464
465 $this->filesystemMock
466 ->shouldReceive('deleteDirectory')
467 ->once()
468 ->with($path)
469 ->getMock()
470 ->shouldReceive('has')
471 ->once()
472 ->with($path)
473 ->andReturn(false);
474
475 $this->subject->deleteDir($path);
476 }
477
478 #[Test]
479
481 {
482 $path = '';
483
484 $this->filesystemMock
485 ->shouldReceive('deleteDirectory')
486 ->once()
487 ->with($path)
488 ->andThrow(\Exception::class);
489
490 $this->expectException(IOException::class);
491 $this->expectExceptionMessage('Could not delete directory "".');
492
493 $this->subject->deleteDir($path);
494 }
495
496 #[Test]
497
499 {
500 $path = '/directory/which/should/be/removed';
501
502 $this->filesystemMock
503 ->shouldReceive('deleteDirectory')
504 ->once()
505 ->with($path)
506 ->andThrow(\Exception::class);
507
508 $this->expectException(IOException::class);
509 $this->expectExceptionMessage("Could not delete directory \"$path\".");
510
511 $this->subject->deleteDir($path);
512 }
513}
This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:33
Indicates that the directory is missing or not found.
Indicates general problems with the input or output operations.
Definition: IOException.php:28
The possible metadata types of the filesystem metadata.
const FILE
The subject is file.
const DIRECTORY
The subject is a directory.
This interface provides the available options for the filesystem right management of the filesystem s...
Definition: Visibility.php:30
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:38
$path
Definition: ltiservices.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...