diff --git a/docs/reference/method/MongoDBGridFSBucket-registerGlobalStreamWrapperAlias.txt b/docs/reference/method/MongoDBGridFSBucket-registerGlobalStreamWrapperAlias.txt index 99f296d60..59991e613 100644 --- a/docs/reference/method/MongoDBGridFSBucket-registerGlobalStreamWrapperAlias.txt +++ b/docs/reference/method/MongoDBGridFSBucket-registerGlobalStreamWrapperAlias.txt @@ -59,7 +59,7 @@ In write mode, the stream context can contain the option ``gridfs['chunkSizeByte If omitted, the defaults are inherited from the ``Bucket`` instance option. The functions `rename` and `unlink` will rename or remove all revisions of a -filename. If the filename does not exist, these functions will return ``false``. +filename. If the filename does not exist, these functions throw a ``FileNotFoundException``. Example ------- diff --git a/src/GridFS/Exception/FileNotFoundException.php b/src/GridFS/Exception/FileNotFoundException.php index 14d4dceb7..ff79c36c0 100644 --- a/src/GridFS/Exception/FileNotFoundException.php +++ b/src/GridFS/Exception/FileNotFoundException.php @@ -25,6 +25,17 @@ class FileNotFoundException extends RuntimeException { + /** + * Thrown when a file cannot be found by its filename. + * + * @param string $filename Filename + * @return self + */ + public static function byStreamWrapperFilename(string $filename) + { + return new self(sprintf('File with name "%s" not found', $filename)); + } + /** * Thrown when a file cannot be found by its filename and revision. * diff --git a/src/GridFS/StreamWrapper.php b/src/GridFS/StreamWrapper.php index 66d71d11c..582915a84 100644 --- a/src/GridFS/StreamWrapper.php +++ b/src/GridFS/StreamWrapper.php @@ -96,7 +96,8 @@ public static function register(string $protocol = 'gridfs'): void /** * Rename all revisions of a filename. * - * @return bool True on success or false on failure. + * @return true + * @throws FileNotFoundException */ public function rename(string $fromPath, string $toPath): bool { @@ -110,9 +111,12 @@ public function rename(string $fromPath, string $toPath): bool $newFilename = explode('/', $toPath, 4)[3] ?? ''; $count = $context['collectionWrapper']->updateFilenameForFilename($context['filename'], $newFilename); - // If $count === 0, the file does not exist. + if ($count === 0) { + throw FileNotFoundException::byStreamWrapperFilename($fromPath); + } + // If $count is null, the update is unacknowledged, the operation is considered successful. - return $count !== 0; + return true; } /** @@ -293,12 +297,23 @@ public function stream_write(string $data): int return $this->stream->writeBytes($data); } + /** + * Remove all revisions of a filename. + * + * @return true + * @throws FileNotFoundException + */ public function unlink(string $path): bool { $context = $this->getContext($path, 'w'); $count = $context['collectionWrapper']->deleteFileAndChunksByFilename($context['filename']); - return $count !== 0; + if ($count === 0) { + throw FileNotFoundException::byStreamWrapperFilename($path); + } + + // If $count is null, the update is unacknowledged, the operation is considered successful. + return true; } /** @return false|array */ diff --git a/tests/GridFS/StreamWrapperFunctionalTest.php b/tests/GridFS/StreamWrapperFunctionalTest.php index 2a7429720..53b68ea43 100644 --- a/tests/GridFS/StreamWrapperFunctionalTest.php +++ b/tests/GridFS/StreamWrapperFunctionalTest.php @@ -381,8 +381,9 @@ public function testRenameAllRevisions(): void $this->assertFalse(file_exists($path)); $this->assertSame('foobar', file_get_contents($path . '.renamed')); - $result = rename($path, $path . '.renamed'); - $this->assertFalse($result, 'File does not exist anymore'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('File with name "gridfs://bucket/filename" not found'); + rename($path, $path . '.renamed'); } public function testRenamePathMismatch(): void @@ -406,7 +407,8 @@ public function testUnlinkAllRevisions(): void $this->assertTrue($result); $this->assertFalse(file_exists($path)); - $result = unlink($path); - $this->assertFalse($result, 'File does not exist anymore'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('File with name "gridfs://bucket/path/to/filename" not found'); + unlink($path); } }