From a955d575a16141223a1499896e85a7696d79446d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 30 Oct 2024 12:57:25 +0100 Subject: [PATCH] Add spec tests --- psalm-baseline.xml | 2 ++ src/GridFS/Bucket.php | 10 +++++++--- src/GridFS/CollectionWrapper.php | 2 +- tests/GridFS/BucketFunctionalTest.php | 13 +++---------- tests/UnifiedSpecTests/Operation.php | 21 +++++++++++++++++++++ tests/UnifiedSpecTests/Util.php | 3 +++ tests/specifications | 2 +- 7 files changed, 38 insertions(+), 15 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 253df75fb..ba4f3cb4f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -286,11 +286,13 @@ + + filesCollection->updateMany( ['filename' => $filename], ['$set' => ['filename' => $newFilename]], diff --git a/src/GridFS/Bucket.php b/src/GridFS/Bucket.php index 8a116bbfd..85046906c 100644 --- a/src/GridFS/Bucket.php +++ b/src/GridFS/Bucket.php @@ -252,7 +252,11 @@ public function delete(mixed $id) */ public function deleteByName(string $filename): void { - $this->collectionWrapper->deleteFileAndChunksByFilename($filename); + $count = $this->collectionWrapper->deleteFileAndChunksByFilename($filename); + + if ($count === 0) { + throw FileNotFoundException::byFilename($filename); + } } /** @@ -672,9 +676,9 @@ public function rename(mixed $id, string $newFilename) */ public function renameByName(string $filename, string $newFilename): void { - $matchedCount = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename); + $count = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename); - if (! $matchedCount) { + if ($count === 0) { throw FileNotFoundException::byFilename($filename); } } diff --git a/src/GridFS/CollectionWrapper.php b/src/GridFS/CollectionWrapper.php index 736e47cfb..09b06ba23 100644 --- a/src/GridFS/CollectionWrapper.php +++ b/src/GridFS/CollectionWrapper.php @@ -74,7 +74,7 @@ public function deleteChunksByFilesId(mixed $id): void /** * Delete all GridFS files and chunks for a given filename. */ - public function deleteFileAndChunksByFilename(string $filename): ?int + public function deleteFileAndChunksByFilename(string $filename): int { /** @var iterable $files */ $files = $this->findFiles(['filename' => $filename], [ diff --git a/tests/GridFS/BucketFunctionalTest.php b/tests/GridFS/BucketFunctionalTest.php index 22da8a346..8faef8e38 100644 --- a/tests/GridFS/BucketFunctionalTest.php +++ b/tests/GridFS/BucketFunctionalTest.php @@ -182,17 +182,10 @@ public function testDeleteByName(): void $this->assertCollectionCount($this->chunksCollection, 0); } - public function testDeleteByNameShouldIgnoreNonexistentFiles(): void + public function testDeleteByNameShouldRequireFileToExist(): void { - $this->bucket->uploadFromStream('filename', self::createStream('foobar')); - - $this->assertCollectionCount($this->filesCollection, 1); - $this->assertCollectionCount($this->chunksCollection, 1); - - $this->bucket->deleteByName('nonexistent-filename'); - - $this->assertCollectionCount($this->filesCollection, 1); - $this->assertCollectionCount($this->chunksCollection, 1); + $this->expectException(FileNotFoundException::class); + $this->bucket->deleteByName('nonexistent-name'); } public function testDownloadingFileWithMissingChunk(): void diff --git a/tests/UnifiedSpecTests/Operation.php b/tests/UnifiedSpecTests/Operation.php index cc7cd703a..3c568a0e0 100644 --- a/tests/UnifiedSpecTests/Operation.php +++ b/tests/UnifiedSpecTests/Operation.php @@ -789,6 +789,12 @@ private function executeForBucket(Bucket $bucket) return $bucket->delete($args['id']); + case 'deleteByName': + assertArrayHasKey('filename', $args); + assertIsString($args['filename']); + + return $bucket->deleteByName($args['filename']); + case 'downloadByName': assertArrayHasKey('filename', $args); assertIsString($args['filename']); @@ -803,6 +809,21 @@ private function executeForBucket(Bucket $bucket) return stream_get_contents($bucket->openDownloadStream($args['id'])); + case 'rename': + assertArrayHasKey('id', $args); + assertArrayHasKey('newFilename', $args); + assertIsString($args['newFilename']); + + return $bucket->rename($args['id'], $args['newFilename']); + + case 'renameByName': + assertArrayHasKey('filename', $args); + assertArrayHasKey('newFilename', $args); + assertIsString($args['filename']); + assertIsString($args['newFilename']); + + return $bucket->renameByName($args['filename'], $args['newFilename']); + case 'uploadWithId': assertArrayHasKey('id', $args); $args['_id'] = $args['id']; diff --git a/tests/UnifiedSpecTests/Util.php b/tests/UnifiedSpecTests/Util.php index 4f2fb22f1..b0d575d39 100644 --- a/tests/UnifiedSpecTests/Util.php +++ b/tests/UnifiedSpecTests/Util.php @@ -132,8 +132,11 @@ final class Util ], Bucket::class => [ 'delete' => ['id'], + 'deleteByName' => ['filename'], 'downloadByName' => ['filename', 'revision'], 'download' => ['id'], + 'rename' => ['id', 'newFilename'], + 'renameByName' => ['filename', 'newFilename'], 'uploadWithId' => ['id', 'filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'], 'upload' => ['filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'], ], diff --git a/tests/specifications b/tests/specifications index 9058491f2..bb21f78c5 160000 --- a/tests/specifications +++ b/tests/specifications @@ -1 +1 @@ -Subproject commit 9058491f2d5b5651913a56e373121a24d6498673 +Subproject commit bb21f78c59b998a0335bd1b4d6feb5a541075653