ILIAS  release_7 Revision v7.30-3-g800a261c036
FlySystemFileStreamAccessTest.php
Go to the documentation of this file.
1<?php
2
4
5require_once('./libs/composer/vendor/autoload.php');
6\Hamcrest\Util::registerGlobalFunctions();
7
12use League\Flysystem\FileExistsException;
13use League\Flysystem\FileNotFoundException;
14use League\Flysystem\FilesystemInterface;
15use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
16use Mockery\MockInterface;
17use PHPUnit\Framework\TestCase;
18
29class FlySystemFileStreamAccessTest extends TestCase
30{
31 use MockeryPHPUnitIntegration;
32
40 private $subject;
41
42
47 protected function setUp() : void
48 {
49 parent::setUp();
50
51 $this->filesystemMock = \Mockery::mock(FilesystemInterface::class);
52 $this->subject = new FlySystemFileStreamAccess($this->filesystemMock);
53 }
54
60 {
61 $path = '/path/to/your/file';
62 $fileContent = 'Awesome file content';
63 $stream = fopen('data://text/plain,' . $fileContent, $fileContent, 'r');
64
65 $this->filesystemMock->shouldReceive('readStream')
66 ->once()
67 ->with($path)
68 ->andReturn($stream);
69
70 $wrappedStream = $this->subject->readStream($path);
71
72 $this->assertSame($fileContent, $wrappedStream->getContents());
73 }
74
80 {
81 $path = '/path/to/your/file';
82
83 $this->filesystemMock->shouldReceive('readStream')
84 ->once()
85 ->with($path)
86 ->andThrow(FileNotFoundException::class);
87
88 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
89 $this->expectExceptionMessage("File \"$path\" not found.");
90
91 $this->subject->readStream($path);
92 }
93
99 {
100 $path = '/path/to/your/file';
101
102 $this->filesystemMock->shouldReceive('readStream')
103 ->once()
104 ->with($path)
105 ->andReturn(false);
106
107 $this->expectException(IOException::class);
108 $this->expectExceptionMessage("Could not open stream for file \"$path\"");
109
110 $this->subject->readStream($path);
111 }
112
118 {
119 $path = '/path/to/your/file';
120 $fileContent = 'Awesome file content';
121 $stream = Streams::ofString($fileContent);
122
123 $this->filesystemMock->shouldReceive('writeStream')
124 ->once()
125 ->withArgs([$path, \resourceValue()])
126 ->andReturn(true);
127
128 $this->subject->writeStream($path, $stream);
129 }
130
136 {
137 $path = '/path/to/your/file';
138 $fileContent = 'Awesome file content';
139 $stream = Streams::ofString($fileContent);
140 $stream->detach();
141
142 $this->expectException(\InvalidArgumentException::class);
143 $this->expectExceptionMessage('The given stream must not be detached.');
144
145 $this->subject->writeStream($path, $stream);
146 }
147
153 {
154 $path = '/path/to/your/file';
155 $fileContent = 'Awesome file content';
156 $stream = Streams::ofString($fileContent);
157
158 $this->filesystemMock->shouldReceive('writeStream')
159 ->once()
160 ->withArgs([$path, \resourceValue()])
161 ->andThrow(FileExistsException::class);
162
163 $this->expectException(FileAlreadyExistsException::class);
164 $this->expectExceptionMessage("File \"$path\" already exists.");
165
166 $this->subject->writeStream($path, $stream);
167 }
168
174 {
175 $path = '/path/to/your/file';
176 $fileContent = 'Awesome file content';
177 $stream = Streams::ofString($fileContent);
178
179 $this->filesystemMock->shouldReceive('writeStream')
180 ->once()
181 ->withArgs([$path, \resourceValue()])
182 ->andReturn(false);
183
184 $this->expectException(IOException::class);
185 $this->expectExceptionMessage("Could not write stream to file \"$path\"");
186
187 $this->subject->writeStream($path, $stream);
188 }
189
195 {
196 $path = '/path/to/your/file';
197 $fileContent = 'Awesome file content';
198 $stream = Streams::ofString($fileContent);
199
200 $this->filesystemMock->shouldReceive('putStream')
201 ->once()
202 ->withArgs([$path, \resourceValue()])
203 ->andReturn(true);
204
205 $this->subject->putStream($path, $stream);
206 }
207
213 {
214 $path = '/path/to/your/file';
215 $fileContent = 'Awesome file content';
216 $stream = Streams::ofString($fileContent);
217
218 $this->filesystemMock->shouldReceive('putStream')
219 ->once()
220 ->withArgs([$path, \resourceValue()])
221 ->andReturn(false);
222
223 $this->expectException(IOException::class);
224 $this->expectExceptionMessage("Could not put stream content into \"$path\"");
225
226 $this->subject->putStream($path, $stream);
227 }
228
234 {
235 $path = '/path/to/your/file';
236 $fileContent = 'Awesome file content';
237 $stream = Streams::ofString($fileContent);
238 $stream->detach();
239
240 $this->expectException(\InvalidArgumentException::class);
241 $this->expectExceptionMessage('The given stream must not be detached.');
242
243 $this->subject->putStream($path, $stream);
244 }
245
251 {
252 $path = '/path/to/your/file';
253 $fileContent = 'Awesome file content';
254 $stream = Streams::ofString($fileContent);
255
256 $this->filesystemMock->shouldReceive('updateStream')
257 ->once()
258 ->withArgs([$path, \resourceValue()])
259 ->andReturn(true);
260
261 $this->subject->updateStream($path, $stream);
262 }
263
269 {
270 $path = '/path/to/your/file';
271 $fileContent = 'Awesome file content';
272 $stream = Streams::ofString($fileContent);
273 $stream->detach();
274
275 $this->expectException(\InvalidArgumentException::class);
276 $this->expectExceptionMessage('The given stream must not be detached.');
277
278 $this->subject->updateStream($path, $stream);
279 }
280
286 {
287 $path = '/path/to/your/file';
288 $fileContent = 'Awesome file content';
289 $stream = Streams::ofString($fileContent);
290
291 $this->filesystemMock->shouldReceive('updateStream')
292 ->once()
293 ->withArgs([$path, \resourceValue()])
294 ->andReturn(false);
295
296 $this->expectException(IOException::class);
297 $this->expectExceptionMessage("Could not update file \"$path\"");
298
299 $this->subject->updateStream($path, $stream);
300 }
301
307 {
308 $path = '/path/to/your/file';
309 $fileContent = 'Awesome file content';
310 $stream = Streams::ofString($fileContent);
311
312 $this->filesystemMock->shouldReceive('updateStream')
313 ->once()
314 ->withArgs([$path, \resourceValue()])
315 ->andThrow(FileNotFoundException::class);
316
317 $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
318 $this->expectExceptionMessage("File \"$path\" not found.");
319
320 $this->subject->updateStream($path, $stream);
321 }
322}
An exception for terminatinating execution or to throw for unit testing.
setUp()
Sets up the fixture, for example, open a network connection.
Class IOException Indicates general problems with the input or output operations.
Definition: IOException.php:13
Class FlySystemFileStreamAccess Streaming access implementation of the fly system library.
Class Streams Stream factory which enables the user to create streams without the knowledge of the co...
Definition: Streams.php:17
static ofString($string)
Creates a new stream with an initial value.
Definition: Streams.php:25
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
Class ChatMainBarProvider \MainMenu\Provider.