ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
FlySystemFileStreamAccessTestTBD.php
Go to the documentation of this file.
1 <?php
2 
20 
22 use Hamcrest\Util;
26 
27 Util::registerGlobalFunctions();
28 
40 
45 class FlySystemFileStreamAccessTest extends TestCase
46 {
48 
52  private LegacyMockInterface $filesystemMock;
54 
55 
60  protected function setUp(): void
61  {
62  parent::setUp();
63 
64  $this->filesystemMock = \Mockery::mock(FilesystemOperator::class);
65  $this->subject = new FlySystemFileStreamAccess($this->filesystemMock);
66  }
67 
68  #[Test]
69 
70  public function testReadStreamWhichShouldSucceed(): void
71  {
72  $path = '/path/to/your/file';
73  $fileContent = 'Awesome file content';
74  $stream = fopen('data://text/plain,' . $fileContent, $fileContent, 'r');
75 
76  $this->filesystemMock->shouldReceive('readStream')
77  ->once()
78  ->with($path)
79  ->andReturn($stream);
80 
81  $wrappedStream = $this->subject->readStream($path);
82 
83  $this->assertSame($fileContent, $wrappedStream->getContents());
84  }
85 
86  #[Test]
87 
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(FileNotFoundException::class);
98  $this->expectExceptionMessage("File \"$path\" not found.");
99 
100  $this->subject->readStream($path);
101  }
102 
103  #[Test]
104 
106  {
107  $path = '/path/to/your/file';
108 
109  $this->filesystemMock->shouldReceive('readStream')
110  ->once()
111  ->with($path)
112  ->andReturn(false);
113 
114  $this->expectException(IOException::class);
115  $this->expectExceptionMessage("Could not open stream for file \"$path\"");
116 
117  $this->subject->readStream($path);
118  }
119 
120  #[Test]
121 
122  public function testWriteStreamWhichShouldSucceed(): void
123  {
124  $path = '/path/to/your/file';
125  $fileContent = 'Awesome file content';
126  $stream = Streams::ofString($fileContent);
127 
128  $this->filesystemMock
129  ->shouldReceive('fileExists')
130  ->once()
131  ->andReturn(false)
132  ->getMock()
133  ->shouldReceive('writeStream')
134  ->once()
135  ->withArgs([$path, \resourceValue()])
136  ->andReturn(true);
137 
138  $this->subject->writeStream($path, $stream);
139  }
140 
141  #[Test]
142 
144  {
145  $path = '/path/to/your/file';
146  $fileContent = 'Awesome file content';
147  $stream = Streams::ofString($fileContent);
148  $stream->detach();
149 
150  $this->expectException(\InvalidArgumentException::class);
151  $this->expectExceptionMessage('The given stream must not be detached.');
152 
153  $this->subject->writeStream($path, $stream);
154  }
155 
156  #[Test]
157 
159  {
160  $path = '/path/to/your/file';
161  $fileContent = 'Awesome file content';
162  $stream = Streams::ofString($fileContent);
163 
164  $this->filesystemMock
165  ->shouldReceive('fileExists')
166  ->once()
167  ->andReturn(true);
168 
169  $this->expectException(FileAlreadyExistsException::class);
170  $this->expectExceptionMessage("File \"$path\" already exists.");
171 
172  $this->subject->writeStream($path, $stream);
173  }
174 
175  #[Test]
176 
178  {
179  $path = '/path/to/your/file';
180  $fileContent = 'Awesome file content';
181  $stream = Streams::ofString($fileContent);
182 
183  $this->filesystemMock
184  ->shouldReceive('fileExists')
185  ->once()
186  ->andReturn(false)
187  ->getMock()
188  ->shouldReceive('writeStream')
189  ->once()
190  ->withArgs([$path, \resourceValue()])
191  ->andThrow(UnableToWriteFile::class);
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 
199  #[Test]
200 
201  public function testPutStreamWhichShouldSucceed(): void
202  {
203  $path = '/path/to/your/file';
204  $fileContent = 'Awesome file content';
205  $stream = Streams::ofString($fileContent);
206 
207  $this->filesystemMock->shouldReceive('putStream')
208  ->once()
209  ->withArgs([$path, \resourceValue()])
210  ->andReturn(true);
211 
212  $this->subject->putStream($path, $stream);
213  }
214 
215  #[Test]
216 
218  {
219  $path = '/path/to/your/file';
220  $fileContent = 'Awesome file content';
221  $stream = Streams::ofString($fileContent);
222 
223  $this->filesystemMock->shouldReceive('putStream')
224  ->once()
225  ->withArgs([$path, \resourceValue()])
226  ->andReturn(false);
227 
228  $this->expectException(IOException::class);
229  $this->expectExceptionMessage("Could not put stream content into \"$path\"");
230 
231  $this->subject->putStream($path, $stream);
232  }
233 
234  #[Test]
235 
237  {
238  $path = '/path/to/your/file';
239  $fileContent = 'Awesome file content';
240  $stream = Streams::ofString($fileContent);
241  $stream->detach();
242 
243  $this->expectException(\InvalidArgumentException::class);
244  $this->expectExceptionMessage('The given stream must not be detached.');
245 
246  $this->subject->putStream($path, $stream);
247  }
248 
249  #[Test]
250 
251  public function testUpdateStreamWhichShouldSucceed(): void
252  {
253  $path = '/path/to/your/file';
254  $fileContent = 'Awesome file content';
255  $stream = Streams::ofString($fileContent);
256 
257  $this->filesystemMock->shouldReceive('writeStream')
258  ->once()
259  ->withArgs([$path, \resourceValue()])
260  ->andReturn(true);
261 
262  $this->subject->updateStream($path, $stream);
263  }
264 
265  #[Test]
266 
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 
280  #[Test]
281 
283  {
284  $path = '/path/to/your/file';
285  $fileContent = 'Awesome file content';
286  $stream = Streams::ofString($fileContent);
287 
288  $this->filesystemMock
289  ->shouldReceive('writeStream')
290  ->once()
291  ->withArgs([$path, \resourceValue()])
292  ->andThrow(UnableToWriteFile::class);
293 
294  $this->expectException(IOException::class);
295  $this->expectExceptionMessage("Unable to update Stream in \"$path\".");
296 
297  $this->subject->updateStream($path, $stream);
298  }
299 
300  #[Test]
301 
303  {
304  $path = '/path/to/your/file';
305  $fileContent = 'Awesome file content';
306  $stream = Streams::ofString($fileContent);
307 
308  $this->filesystemMock
309  ->shouldReceive('writeStream')
310  ->once()
311  ->withArgs([$path, \resourceValue()])
312  ->andThrow(UnableToWriteFile::class);
313 
314  $this->expectException(FileNotFoundException::class);
315  $this->expectExceptionMessage("Unable to update Stream in \"$path\".");
316 
317  $this->subject->updateStream($path, $stream);
318  }
319 }
$path
Definition: ltiservices.php:29
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.