23use Mockery\LegacyMockInterface;
24use PHPUnit\Framework\Attributes\Test;
25use PHPUnit\Framework\Attributes\Small;
27Util::registerGlobalFunctions();
33use League\Flysystem\FilesystemInterface;
34use League\Flysystem\FilesystemOperator;
35use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
36use Mockery\MockInterface;
37use PHPUnit\Framework\TestCase;
38use League\Flysystem\UnableToReadFile;
39use League\Flysystem\UnableToWriteFile;
47 use MockeryPHPUnitIntegration;
60 protected function setUp(): void
64 $this->filesystemMock = \Mockery::mock(FilesystemOperator::class);
72 $path =
'/path/to/your/file';
73 $fileContent =
'Awesome file content';
74 $stream = fopen(
'data://text/plain,' . $fileContent, $fileContent,
'r');
76 $this->filesystemMock->shouldReceive(
'readStream')
81 $wrappedStream = $this->subject->readStream(
$path);
83 $this->assertSame($fileContent, $wrappedStream->getContents());
90 $path =
'/path/to/your/file';
92 $this->filesystemMock->shouldReceive(
'readStream')
95 ->andThrow(UnableToReadFile::class);
97 $this->expectException(FileNotFoundException::class);
98 $this->expectExceptionMessage(
"File \"$path\" not found.");
100 $this->subject->readStream(
$path);
107 $path =
'/path/to/your/file';
109 $this->filesystemMock->shouldReceive(
'readStream')
114 $this->expectException(IOException::class);
115 $this->expectExceptionMessage(
"Could not open stream for file \"$path\"");
117 $this->subject->readStream(
$path);
124 $path =
'/path/to/your/file';
125 $fileContent =
'Awesome file content';
128 $this->filesystemMock
129 ->shouldReceive(
'fileExists')
133 ->shouldReceive(
'writeStream')
135 ->withArgs([
$path, \resourceValue()])
138 $this->subject->writeStream(
$path, $stream);
145 $path =
'/path/to/your/file';
146 $fileContent =
'Awesome file content';
150 $this->expectException(\InvalidArgumentException::class);
151 $this->expectExceptionMessage(
'The given stream must not be detached.');
153 $this->subject->writeStream(
$path, $stream);
160 $path =
'/path/to/your/file';
161 $fileContent =
'Awesome file content';
164 $this->filesystemMock
165 ->shouldReceive(
'fileExists')
169 $this->expectException(FileAlreadyExistsException::class);
170 $this->expectExceptionMessage(
"File \"$path\" already exists.");
172 $this->subject->writeStream(
$path, $stream);
179 $path =
'/path/to/your/file';
180 $fileContent =
'Awesome file content';
183 $this->filesystemMock
184 ->shouldReceive(
'fileExists')
188 ->shouldReceive(
'writeStream')
190 ->withArgs([
$path, \resourceValue()])
191 ->andThrow(UnableToWriteFile::class);
193 $this->expectException(IOException::class);
194 $this->expectExceptionMessage(
"Could not write stream to file \"$path\"");
196 $this->subject->writeStream(
$path, $stream);
203 $path =
'/path/to/your/file';
204 $fileContent =
'Awesome file content';
207 $this->filesystemMock->shouldReceive(
'putStream')
209 ->withArgs([
$path, \resourceValue()])
212 $this->subject->putStream(
$path, $stream);
219 $path =
'/path/to/your/file';
220 $fileContent =
'Awesome file content';
223 $this->filesystemMock->shouldReceive(
'putStream')
225 ->withArgs([
$path, \resourceValue()])
228 $this->expectException(IOException::class);
229 $this->expectExceptionMessage(
"Could not put stream content into \"$path\"");
231 $this->subject->putStream(
$path, $stream);
238 $path =
'/path/to/your/file';
239 $fileContent =
'Awesome file content';
243 $this->expectException(\InvalidArgumentException::class);
244 $this->expectExceptionMessage(
'The given stream must not be detached.');
246 $this->subject->putStream(
$path, $stream);
253 $path =
'/path/to/your/file';
254 $fileContent =
'Awesome file content';
257 $this->filesystemMock->shouldReceive(
'writeStream')
259 ->withArgs([
$path, \resourceValue()])
262 $this->subject->updateStream(
$path, $stream);
269 $path =
'/path/to/your/file';
270 $fileContent =
'Awesome file content';
274 $this->expectException(\InvalidArgumentException::class);
275 $this->expectExceptionMessage(
'The given stream must not be detached.');
277 $this->subject->updateStream(
$path, $stream);
284 $path =
'/path/to/your/file';
285 $fileContent =
'Awesome file content';
288 $this->filesystemMock
289 ->shouldReceive(
'writeStream')
291 ->withArgs([
$path, \resourceValue()])
292 ->andThrow(UnableToWriteFile::class);
294 $this->expectException(IOException::class);
295 $this->expectExceptionMessage(
"Unable to update Stream in \"$path\".");
297 $this->subject->updateStream(
$path, $stream);
304 $path =
'/path/to/your/file';
305 $fileContent =
'Awesome file content';
308 $this->filesystemMock
309 ->shouldReceive(
'writeStream')
311 ->withArgs([
$path, \resourceValue()])
312 ->andThrow(UnableToWriteFile::class);
314 $this->expectException(FileNotFoundException::class);
315 $this->expectExceptionMessage(
"Unable to update Stream in \"$path\".");
317 $this->subject->updateStream(
$path, $stream);
testWriteStreamWithExistingFileWhichShouldFail()
testPutStreamWithDetachedStreamWhichShouldFail()
testUpdateStreamWithMissingFileWhichShouldFail()
testPutStreamWithGeneralFailureWhichShouldFail()
testUpdateStreamWithGeneralFailureWhichShouldFail()
LegacyMockInterface $filesystemMock
FlySystemFileStreamAccess $subject
testUpdateStreamWithDetachedStreamWhichShouldFail()
testPutStreamWhichShouldSucceed()
setUp()
Sets up the fixture, for example, open a network connection.
testWriteStreamWithFailingAdapterWhichShouldFail()
testReadStreamWhichShouldSucceed()
testUpdateStreamWhichShouldSucceed()
testWriteStreamWhichShouldSucceed()
testReadStreamWithGeneralFailureWhichShouldFail()
testReadStreamWithMissingFileWhichShouldFail()
testWriteStreamWithDetachedStreamWhichShouldFail()
Indicates that a file is missing or not found.
Indicates general problems with the input or output operations.
Stream factory which enables the user to create streams without the knowledge of the concrete class.
static ofString(string $string)
Creates a new stream with an initial value.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...