ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FlySystemFileStreamAccessTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 require_once('./libs/composer/vendor/autoload.php');
6 
17 
28 class FlySystemFileStreamAccessTest extends TestCase
29 {
31 
35  private $filesystemMock;
39  private $subject;
40 
41 
46  protected function setUp()
47  {
48  parent::setUp();
49 
50  $this->filesystemMock = \Mockery::mock(FilesystemInterface::class);
51  $this->subject = new FlySystemFileStreamAccess($this->filesystemMock);
52  }
53 
59  {
60  $path = '/path/to/your/file';
61  $fileContent = 'Awesome file content';
62  $stream = fopen('data://text/plain,' . $fileContent, $fileContent, 'r');
63 
64  $this->filesystemMock->shouldReceive('readStream')
65  ->once()
66  ->with($path)
67  ->andReturn($stream);
68 
69  $wrappedStream = $this->subject->readStream($path);
70 
71  $this->assertSame($fileContent, $wrappedStream->getContents());
72  }
73 
79  {
80  $path = '/path/to/your/file';
81 
82  $this->filesystemMock->shouldReceive('readStream')
83  ->once()
84  ->with($path)
85  ->andThrow(FileNotFoundException::class);
86 
87  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
88  $this->expectExceptionMessage("File \"$path\" not found.");
89 
90  $this->subject->readStream($path);
91  }
92 
98  {
99  $path = '/path/to/your/file';
100 
101  $this->filesystemMock->shouldReceive('readStream')
102  ->once()
103  ->with($path)
104  ->andReturn(false);
105 
106  $this->expectException(IOException::class);
107  $this->expectExceptionMessage("Could not open stream for file \"$path\"");
108 
109  $this->subject->readStream($path);
110  }
111 
117  {
118  $path = '/path/to/your/file';
119  $fileContent = 'Awesome file content';
120  $stream = Streams::ofString($fileContent);
121 
122  $this->filesystemMock->shouldReceive('writeStream')
123  ->once()
124  ->withArgs([$path, resourceValue()])
125  ->andReturn(true);
126 
127  $this->subject->writeStream($path, $stream);
128  }
129 
135  {
136  $path = '/path/to/your/file';
137  $fileContent = 'Awesome file content';
138  $stream = Streams::ofString($fileContent);
139  $stream->detach();
140 
141  $this->expectException(\InvalidArgumentException::class);
142  $this->expectExceptionMessage('The given stream must not be detached.');
143 
144  $this->subject->writeStream($path, $stream);
145  }
146 
152  {
153  $path = '/path/to/your/file';
154  $fileContent = 'Awesome file content';
155  $stream = Streams::ofString($fileContent);
156 
157  $this->filesystemMock->shouldReceive('writeStream')
158  ->once()
159  ->withArgs([$path, resourceValue()])
160  ->andThrow(FileExistsException::class);
161 
162  $this->expectException(FileAlreadyExistsException::class);
163  $this->expectExceptionMessage("File \"$path\" already exists.");
164 
165  $this->subject->writeStream($path, $stream);
166  }
167 
173  {
174  $path = '/path/to/your/file';
175  $fileContent = 'Awesome file content';
176  $stream = Streams::ofString($fileContent);
177 
178  $this->filesystemMock->shouldReceive('writeStream')
179  ->once()
180  ->withArgs([$path, resourceValue()])
181  ->andReturn(false);
182 
183  $this->expectException(IOException::class);
184  $this->expectExceptionMessage("Could not write stream to file \"$path\"");
185 
186  $this->subject->writeStream($path, $stream);
187  }
188 
194  {
195  $path = '/path/to/your/file';
196  $fileContent = 'Awesome file content';
197  $stream = Streams::ofString($fileContent);
198 
199  $this->filesystemMock->shouldReceive('putStream')
200  ->once()
201  ->withArgs([$path, resourceValue()])
202  ->andReturn(true);
203 
204  $this->subject->putStream($path, $stream);
205  }
206 
212  {
213  $path = '/path/to/your/file';
214  $fileContent = 'Awesome file content';
215  $stream = Streams::ofString($fileContent);
216 
217  $this->filesystemMock->shouldReceive('putStream')
218  ->once()
219  ->withArgs([$path, resourceValue()])
220  ->andReturn(false);
221 
222  $this->expectException(IOException::class);
223  $this->expectExceptionMessage("Could not put stream content into \"$path\"");
224 
225  $this->subject->putStream($path, $stream);
226  }
227 
233  {
234  $path = '/path/to/your/file';
235  $fileContent = 'Awesome file content';
236  $stream = Streams::ofString($fileContent);
237  $stream->detach();
238 
239  $this->expectException(\InvalidArgumentException::class);
240  $this->expectExceptionMessage('The given stream must not be detached.');
241 
242  $this->subject->putStream($path, $stream);
243  }
244 
250  {
251  $path = '/path/to/your/file';
252  $fileContent = 'Awesome file content';
253  $stream = Streams::ofString($fileContent);
254 
255  $this->filesystemMock->shouldReceive('updateStream')
256  ->once()
257  ->withArgs([$path, resourceValue()])
258  ->andReturn(true);
259 
260  $this->subject->updateStream($path, $stream);
261  }
262 
268  {
269  $path = '/path/to/your/file';
270  $fileContent = 'Awesome file content';
271  $stream = Streams::ofString($fileContent);
272  $stream->detach();
273 
274  $this->expectException(\InvalidArgumentException::class);
275  $this->expectExceptionMessage('The given stream must not be detached.');
276 
277  $this->subject->updateStream($path, $stream);
278  }
279 
285  {
286  $path = '/path/to/your/file';
287  $fileContent = 'Awesome file content';
288  $stream = Streams::ofString($fileContent);
289 
290  $this->filesystemMock->shouldReceive('updateStream')
291  ->once()
292  ->withArgs([$path, resourceValue()])
293  ->andReturn(false);
294 
295  $this->expectException(IOException::class);
296  $this->expectExceptionMessage("Could not update file \"$path\"");
297 
298  $this->subject->updateStream($path, $stream);
299  }
300 
306  {
307  $path = '/path/to/your/file';
308  $fileContent = 'Awesome file content';
309  $stream = Streams::ofString($fileContent);
310 
311  $this->filesystemMock->shouldReceive('updateStream')
312  ->once()
313  ->withArgs([$path, resourceValue()])
314  ->andThrow(FileNotFoundException::class);
315 
316  $this->expectException(\ILIAS\Filesystem\Exception\FileNotFoundException::class);
317  $this->expectExceptionMessage("File \"$path\" not found.");
318 
319  $this->subject->updateStream($path, $stream);
320  }
321 }
$path
Definition: aliased.php:25
Class BaseForm.
$stream
PHP stream implementation.
static ofString($string)
Creates a new stream with an initial value.
Definition: Streams.php:30
setUp()
Sets up the fixture, for example, open a network connection.
Class FlySystemFileAccessTest.