Skip to content

Commit b9d5dc0

Browse files
committed
Add spec tests
1 parent afaf1d8 commit b9d5dc0

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

src/GridFS/Bucket.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ public function delete(mixed $id)
252252
*/
253253
public function deleteByName(string $filename): void
254254
{
255-
$this->collectionWrapper->deleteFileAndChunksByFilename($filename);
255+
$count = $this->collectionWrapper->deleteFileAndChunksByFilename($filename);
256+
257+
if ($count === 0) {
258+
throw FileNotFoundException::byFilename($filename);
259+
}
256260
}
257261

258262
/**
@@ -672,9 +676,9 @@ public function rename(mixed $id, string $newFilename)
672676
*/
673677
public function renameByName(string $filename, string $newFilename): void
674678
{
675-
$matchedCount = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename);
679+
$count = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename);
676680

677-
if (! $matchedCount) {
681+
if ($count === 0) {
678682
throw FileNotFoundException::byFilename($filename);
679683
}
680684
}

src/GridFS/CollectionWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function deleteChunksByFilesId(mixed $id): void
7474
/**
7575
* Delete all GridFS files and chunks for a given filename.
7676
*/
77-
public function deleteFileAndChunksByFilename(string $filename): ?int
77+
public function deleteFileAndChunksByFilename(string $filename): int
7878
{
7979
/** @var iterable<array{_id: mixed}> $files */
8080
$files = $this->findFiles(['filename' => $filename], [

tests/GridFS/BucketFunctionalTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,10 @@ public function testDeleteByName(): void
182182
$this->assertCollectionCount($this->chunksCollection, 0);
183183
}
184184

185-
public function testDeleteByNameShouldIgnoreNonexistentFiles(): void
185+
public function testDeleteByNameShouldRequireFileToExist(): void
186186
{
187-
$this->bucket->uploadFromStream('filename', self::createStream('foobar'));
188-
189-
$this->assertCollectionCount($this->filesCollection, 1);
190-
$this->assertCollectionCount($this->chunksCollection, 1);
191-
192-
$this->bucket->deleteByName('nonexistent-filename');
193-
194-
$this->assertCollectionCount($this->filesCollection, 1);
195-
$this->assertCollectionCount($this->chunksCollection, 1);
187+
$this->expectException(FileNotFoundException::class);
188+
$this->bucket->deleteByName('nonexistent-name');
196189
}
197190

198191
public function testDownloadingFileWithMissingChunk(): void

tests/UnifiedSpecTests/Operation.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,12 @@ private function executeForBucket(Bucket $bucket)
789789

790790
return $bucket->delete($args['id']);
791791

792+
case 'deleteByName':
793+
assertArrayHasKey('filename', $args);
794+
assertIsString($args['filename']);
795+
796+
return $bucket->deleteByName($args['filename']);
797+
792798
case 'downloadByName':
793799
assertArrayHasKey('filename', $args);
794800
assertIsString($args['filename']);
@@ -803,6 +809,21 @@ private function executeForBucket(Bucket $bucket)
803809

804810
return stream_get_contents($bucket->openDownloadStream($args['id']));
805811

812+
case 'rename':
813+
assertArrayHasKey('id', $args);
814+
assertArrayHasKey('newFilename', $args);
815+
assertIsString($args['newFilename']);
816+
817+
return $bucket->rename($args['id'], $args['newFilename']);
818+
819+
case 'renameByName':
820+
assertArrayHasKey('filename', $args);
821+
assertArrayHasKey('newFilename', $args);
822+
assertIsString($args['filename']);
823+
assertIsString($args['newFilename']);
824+
825+
return $bucket->renameByName($args['filename'], $args['newFilename']);
826+
806827
case 'uploadWithId':
807828
assertArrayHasKey('id', $args);
808829
$args['_id'] = $args['id'];

tests/UnifiedSpecTests/Util.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,11 @@ final class Util
132132
],
133133
Bucket::class => [
134134
'delete' => ['id'],
135+
'deleteByName' => ['filename'],
135136
'downloadByName' => ['filename', 'revision'],
136137
'download' => ['id'],
138+
'rename' => ['id', 'newFilename'],
139+
'renameByName' => ['filename', 'newFilename'],
137140
'uploadWithId' => ['id', 'filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'],
138141
'upload' => ['filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'],
139142
],

0 commit comments

Comments
 (0)