ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
FlySystemFileStreamAccessTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 \Hamcrest\Util::registerGlobalFunctions();
6 
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  *****************************************************************************/
41 class FlySystemFileStreamAccessTest extends TestCase
42 {
44 
48  private $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(FilesystemInterface::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(FileNotFoundException::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->shouldReceive('writeStream')
133  ->once()
134  ->withArgs([$path, \resourceValue()])
135  ->andReturn(true);
136 
137  $this->subject->writeStream($path, $stream);
138  }
139 
145  {
146  $path = '/path/to/your/file';
147  $fileContent = 'Awesome file content';
148  $stream = Streams::ofString($fileContent);
149  $stream->detach();
150 
151  $this->expectException(\InvalidArgumentException::class);
152  $this->expectExceptionMessage('The given stream must not be detached.');
153 
154  $this->subject->writeStream($path, $stream);
155  }
156 
162  {
163  $path = '/path/to/your/file';
164  $fileContent = 'Awesome file content';
165  $stream = Streams::ofString($fileContent);
166 
167  $this->filesystemMock->shouldReceive('writeStream')
168  ->once()
169  ->withArgs([$path, \resourceValue()])
170  ->andThrow(FileExistsException::class);
171 
172  $this->expectException(FileAlreadyExistsException::class);
173  $this->expectExceptionMessage("File \"$path\" already exists.");
174 
175  $this->subject->writeStream($path, $stream);
176  }
177 
183  {
184  $path = '/path/to/your/file';
185  $fileContent = 'Awesome file content';
186  $stream = Streams::ofString($fileContent);
187 
188  $this->filesystemMock->shouldReceive('writeStream')
189  ->once()
190  ->withArgs([$path, \resourceValue()])
191  ->andReturn(false);
192 
193  $this->expectException(IOException::class);
194  $this->expectExceptionMessage("Could not write stream to file \"$path\"");
195 
196  $this->subject->writeStream($path, $stream);
197  }
198 
203  public function testPutStreamWhichShouldSucceed(): void
204  {
205  $path = '/path/to/your/file';
206  $fileContent = 'Awesome file content';
207  $stream = Streams::ofString($fileContent);
208 
209  $this->filesystemMock->shouldReceive('putStream')
210  ->once()
211  ->withArgs([$path, \resourceValue()])
212  ->andReturn(true);
213 
214  $this->subject->putStream($path, $stream);
215  }
216 
222  {
223  $path = '/path/to/your/file';
224  $fileContent = 'Awesome file content';
225  $stream = Streams::ofString($fileContent);
226 
227  $this->filesystemMock->shouldReceive('putStream')
228  ->once()
229  ->withArgs([$path, \resourceValue()])
230  ->andReturn(false);
231 
232  $this->expectException(IOException::class);
233  $this->expectExceptionMessage("Could not put stream content into \"$path\"");
234 
235  $this->subject->putStream($path, $stream);
236  }
237 
243  {
244  $path = '/path/to/your/file';
245  $fileContent = 'Awesome file content';
246  $stream = Streams::ofString($fileContent);
247  $stream->detach();
248 
249  $this->expectException(\InvalidArgumentException::class);
250  $this->expectExceptionMessage('The given stream must not be detached.');
251 
252  $this->subject->putStream($path, $stream);
253  }
254 
259  public function testUpdateStreamWhichShouldSucceed(): void
260  {
261  $path = '/path/to/your/file';
262  $fileContent = 'Awesome file content';
263  $stream = Streams::ofString($fileContent);
264 
265  $this->filesystemMock->shouldReceive('updateStream')
266  ->once()
267  ->withArgs([$path, \resourceValue()])
268  ->andReturn(true);
269 
270  $this->subject->updateStream($path, $stream);
271  }
272 
278  {
279  $path = '/path/to/your/file';
280  $fileContent = 'Awesome file content';
281  $stream = Streams::ofString($fileContent);
282  $stream->detach();
283 
284  $this->expectException(\InvalidArgumentException::class);
285  $this->expectExceptionMessage('The given stream must not be detached.');
286 
287  $this->subject->updateStream($path, $stream);
288  }
289 
295  {
296  $path = '/path/to/your/file';
297  $fileContent = 'Awesome file content';
298  $stream = Streams::ofString($fileContent);
299 
300  $this->filesystemMock->shouldReceive('updateStream')
301  ->once()
302  ->withArgs([$path, \resourceValue()])
303  ->andReturn(false);
304 
305  $this->expectException(IOException::class);
306  $this->expectExceptionMessage("Could not update file \"$path\"");
307 
308  $this->subject->updateStream($path, $stream);
309  }
310 
316  {
317  $path = '/path/to/your/file';
318  $fileContent = 'Awesome file content';
319  $stream = Streams::ofString($fileContent);
320 
321  $this->filesystemMock->shouldReceive('updateStream')
322  ->once()
323  ->withArgs([$path, \resourceValue()])
324  ->andThrow(FileNotFoundException::class);
325 
326  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
327  $this->expectExceptionMessage("File \"$path\" not found.");
328 
329  $this->subject->updateStream($path, $stream);
330  }
331 }
ILIAS Filesystem Provider FlySystem FlySystemFileStreamAccess $subject
Class ChatMainBarProvider .
$path
Definition: ltiservices.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:43
setUp()
Sets up the fixture, for example, open a network connection.
Class FlySystemFileAccessTest disabled disabled disabled.