ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
FlySystemFileStreamAccessTestTBD.php
Go to the documentation of this file.
1 <?php
2 
20 
21 \Hamcrest\Util::registerGlobalFunctions();
22 
36 
41 class FlySystemFileStreamAccessTest extends TestCase
42 {
44 
48  private \Mockery\LegacyMockInterface $filesystemMock;
49  private \ILIAS\Filesystem\Provider\FlySystem\FlySystemFileStreamAccess $subject;
50 
51 
56  protected function setUp(): void
57  {
58  parent::setUp();
59 
60  $this->filesystemMock = \Mockery::mock(FilesystemOperator::class);
61  $this->subject = new FlySystemFileStreamAccess($this->filesystemMock);
62  }
63 
68  public function testReadStreamWhichShouldSucceed(): void
69  {
70  $path = '/path/to/your/file';
71  $fileContent = 'Awesome file content';
72  $stream = fopen('data://text/plain,' . $fileContent, $fileContent, 'r');
73 
74  $this->filesystemMock->shouldReceive('readStream')
75  ->once()
76  ->with($path)
77  ->andReturn($stream);
78 
79  $wrappedStream = $this->subject->readStream($path);
80 
81  $this->assertSame($fileContent, $wrappedStream->getContents());
82  }
83 
89  {
90  $path = '/path/to/your/file';
91 
92  $this->filesystemMock->shouldReceive('readStream')
93  ->once()
94  ->with($path)
95  ->andThrow(UnableToReadFile::class);
96 
97  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
98  $this->expectExceptionMessage("File \"$path\" not found.");
99 
100  $this->subject->readStream($path);
101  }
102 
108  {
109  $path = '/path/to/your/file';
110 
111  $this->filesystemMock->shouldReceive('readStream')
112  ->once()
113  ->with($path)
114  ->andReturn(false);
115 
116  $this->expectException(IOException::class);
117  $this->expectExceptionMessage("Could not open stream for file \"$path\"");
118 
119  $this->subject->readStream($path);
120  }
121 
126  public function testWriteStreamWhichShouldSucceed(): void
127  {
128  $path = '/path/to/your/file';
129  $fileContent = 'Awesome file content';
130  $stream = Streams::ofString($fileContent);
131 
132  $this->filesystemMock
133  ->shouldReceive('fileExists')
134  ->once()
135  ->andReturn(false)
136  ->getMock()
137  ->shouldReceive('writeStream')
138  ->once()
139  ->withArgs([$path, \resourceValue()])
140  ->andReturn(true);
141 
142  $this->subject->writeStream($path, $stream);
143  }
144 
150  {
151  $path = '/path/to/your/file';
152  $fileContent = 'Awesome file content';
153  $stream = Streams::ofString($fileContent);
154  $stream->detach();
155 
156  $this->expectException(\InvalidArgumentException::class);
157  $this->expectExceptionMessage('The given stream must not be detached.');
158 
159  $this->subject->writeStream($path, $stream);
160  }
161 
167  {
168  $path = '/path/to/your/file';
169  $fileContent = 'Awesome file content';
170  $stream = Streams::ofString($fileContent);
171 
172  $this->filesystemMock
173  ->shouldReceive('fileExists')
174  ->once()
175  ->andReturn(true);
176 
177  $this->expectException(FileAlreadyExistsException::class);
178  $this->expectExceptionMessage("File \"$path\" already exists.");
179 
180  $this->subject->writeStream($path, $stream);
181  }
182 
188  {
189  $path = '/path/to/your/file';
190  $fileContent = 'Awesome file content';
191  $stream = Streams::ofString($fileContent);
192 
193  $this->filesystemMock
194  ->shouldReceive('fileExists')
195  ->once()
196  ->andReturn(false)
197  ->getMock()
198  ->shouldReceive('writeStream')
199  ->once()
200  ->withArgs([$path, \resourceValue()])
201  ->andThrow(UnableToWriteFile::class);
202 
203  $this->expectException(IOException::class);
204  $this->expectExceptionMessage("Could not write stream to file \"$path\"");
205 
206  $this->subject->writeStream($path, $stream);
207  }
208 
213  public function testPutStreamWhichShouldSucceed(): void
214  {
215  $path = '/path/to/your/file';
216  $fileContent = 'Awesome file content';
217  $stream = Streams::ofString($fileContent);
218 
219  $this->filesystemMock->shouldReceive('putStream')
220  ->once()
221  ->withArgs([$path, \resourceValue()])
222  ->andReturn(true);
223 
224  $this->subject->putStream($path, $stream);
225  }
226 
232  {
233  $path = '/path/to/your/file';
234  $fileContent = 'Awesome file content';
235  $stream = Streams::ofString($fileContent);
236 
237  $this->filesystemMock->shouldReceive('putStream')
238  ->once()
239  ->withArgs([$path, \resourceValue()])
240  ->andReturn(false);
241 
242  $this->expectException(IOException::class);
243  $this->expectExceptionMessage("Could not put stream content into \"$path\"");
244 
245  $this->subject->putStream($path, $stream);
246  }
247 
253  {
254  $path = '/path/to/your/file';
255  $fileContent = 'Awesome file content';
256  $stream = Streams::ofString($fileContent);
257  $stream->detach();
258 
259  $this->expectException(\InvalidArgumentException::class);
260  $this->expectExceptionMessage('The given stream must not be detached.');
261 
262  $this->subject->putStream($path, $stream);
263  }
264 
269  public function testUpdateStreamWhichShouldSucceed(): void
270  {
271  $path = '/path/to/your/file';
272  $fileContent = 'Awesome file content';
273  $stream = Streams::ofString($fileContent);
274 
275  $this->filesystemMock->shouldReceive('writeStream')
276  ->once()
277  ->withArgs([$path, \resourceValue()])
278  ->andReturn(true);
279 
280  $this->subject->updateStream($path, $stream);
281  }
282 
288  {
289  $path = '/path/to/your/file';
290  $fileContent = 'Awesome file content';
291  $stream = Streams::ofString($fileContent);
292  $stream->detach();
293 
294  $this->expectException(\InvalidArgumentException::class);
295  $this->expectExceptionMessage('The given stream must not be detached.');
296 
297  $this->subject->updateStream($path, $stream);
298  }
299 
305  {
306  $path = '/path/to/your/file';
307  $fileContent = 'Awesome file content';
308  $stream = Streams::ofString($fileContent);
309 
310  $this->filesystemMock
311  ->shouldReceive('writeStream')
312  ->once()
313  ->withArgs([$path, \resourceValue()])
314  ->andThrow(UnableToWriteFile::class);
315 
316  $this->expectException(IOException::class);
317  $this->expectExceptionMessage("Unable to update Stream in \"$path\".");
318 
319  $this->subject->updateStream($path, $stream);
320  }
321 
327  {
328  $path = '/path/to/your/file';
329  $fileContent = 'Awesome file content';
330  $stream = Streams::ofString($fileContent);
331 
332  $this->filesystemMock
333  ->shouldReceive('writeStream')
334  ->once()
335  ->withArgs([$path, \resourceValue()])
336  ->andThrow(UnableToWriteFile::class);
337 
338  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
339  $this->expectExceptionMessage("Unable to update Stream in \"$path\".");
340 
341  $this->subject->updateStream($path, $stream);
342  }
343 }
ILIAS Filesystem Provider FlySystem FlySystemFileStreamAccess $subject
Interface Observer Contains several chained tasks and infos about them.
$path
Definition: ltiservices.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
setUp()
Sets up the fixture, for example, open a network connection.