Skip to content

Commit f9fac57

Browse files
Merge v1.x into v2.x (#1512)
2 parents 99ded15 + 8a687f2 commit f9fac57

File tree

5 files changed

+97
-3
lines changed

5 files changed

+97
-3
lines changed

src/GridFS/Bucket.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,23 @@ public function delete(mixed $id): void
219219
}
220220
}
221221

222+
/**
223+
* Delete all the revisions of a file name from the GridFS bucket.
224+
*
225+
* @param string $filename Filename
226+
*
227+
* @throws FileNotFoundException if no file could be selected
228+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
229+
*/
230+
public function deleteByName(string $filename): void
231+
{
232+
$count = $this->collectionWrapper->deleteFileAndChunksByFilename($filename);
233+
234+
if ($count === 0) {
235+
throw FileNotFoundException::byFilename($filename);
236+
}
237+
}
238+
222239
/**
223240
* Writes the contents of a GridFS file to a writable stream.
224241
*
@@ -590,6 +607,24 @@ public function rename(mixed $id, string $newFilename): void
590607
}
591608
}
592609

610+
/**
611+
* Renames all the revisions of a file name in the GridFS bucket.
612+
*
613+
* @param string $filename Filename
614+
* @param string $newFilename New filename
615+
*
616+
* @throws FileNotFoundException if no file could be selected
617+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
618+
*/
619+
public function renameByName(string $filename, string $newFilename): void
620+
{
621+
$count = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename);
622+
623+
if ($count === 0) {
624+
throw FileNotFoundException::byFilename($filename);
625+
}
626+
}
627+
593628
/**
594629
* Writes the contents of a readable stream to a GridFS file.
595630
*

src/GridFS/CollectionWrapper.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ public function findChunksByFileId(mixed $id, int $fromChunk = 0): CursorInterfa
148148
*/
149149
public function findFileByFilenameAndRevision(string $filename, int $revision): ?object
150150
{
151-
$filename = $filename;
152-
$revision = $revision;
153-
154151
if ($revision < 0) {
155152
$skip = abs($revision) - 1;
156153
$sortOrder = -1;

tests/GridFS/BucketFunctionalTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,34 @@ public function testDeleteStillRemovesChunksIfFileDoesNotExist($input, $expected
158158
$this->assertCollectionCount($this->chunksCollection, 0);
159159
}
160160

161+
public function testDeleteByName(): void
162+
{
163+
$this->bucket->uploadFromStream('filename', self::createStream('foobar1'));
164+
$this->bucket->uploadFromStream('filename', self::createStream('foobar2'));
165+
$this->bucket->uploadFromStream('filename', self::createStream('foobar3'));
166+
167+
$this->bucket->uploadFromStream('other', self::createStream('foobar'));
168+
169+
$this->assertCollectionCount($this->filesCollection, 4);
170+
$this->assertCollectionCount($this->chunksCollection, 4);
171+
172+
$this->bucket->deleteByName('filename');
173+
174+
$this->assertCollectionCount($this->filesCollection, 1);
175+
$this->assertCollectionCount($this->chunksCollection, 1);
176+
177+
$this->bucket->deleteByName('other');
178+
179+
$this->assertCollectionCount($this->filesCollection, 0);
180+
$this->assertCollectionCount($this->chunksCollection, 0);
181+
}
182+
183+
public function testDeleteByNameShouldRequireFileToExist(): void
184+
{
185+
$this->expectException(FileNotFoundException::class);
186+
$this->bucket->deleteByName('nonexistent-name');
187+
}
188+
161189
public function testDownloadingFileWithMissingChunk(): void
162190
{
163191
$id = $this->bucket->uploadFromStream('filename', self::createStream('foobar'));
@@ -721,6 +749,24 @@ public function testRenameShouldRequireFileToExist(): void
721749
$this->bucket->rename('nonexistent-id', 'b');
722750
}
723751

752+
public function testRenameByName(): void
753+
{
754+
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
755+
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
756+
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
757+
758+
$this->bucket->renameByName('filename', 'newname');
759+
760+
$this->assertNull($this->bucket->findOne(['filename' => 'filename']), 'No file has the old name');
761+
$this->assertStreamContents('foo', $this->bucket->openDownloadStreamByName('newname'));
762+
}
763+
764+
public function testRenameByNameShouldRequireFileToExist(): void
765+
{
766+
$this->expectException(FileNotFoundException::class);
767+
$this->bucket->renameByName('nonexistent-name', 'b');
768+
}
769+
724770
public function testUploadFromStream(): void
725771
{
726772
$options = [

tests/UnifiedSpecTests/Operation.php

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

778778
return $bucket->delete($args['id']);
779779

780+
case 'deleteByName':
781+
assertArrayHasKey('filename', $args);
782+
assertIsString($args['filename']);
783+
784+
return $bucket->deleteByName($args['filename']);
785+
780786
case 'downloadByName':
781787
assertArrayHasKey('filename', $args);
782788
assertIsString($args['filename']);
@@ -800,6 +806,14 @@ private function executeForBucket(Bucket $bucket)
800806

801807
return null;
802808

809+
case 'renameByName':
810+
assertArrayHasKey('filename', $args);
811+
assertArrayHasKey('newFilename', $args);
812+
assertIsString($args['filename']);
813+
assertIsString($args['newFilename']);
814+
815+
return $bucket->renameByName($args['filename'], $args['newFilename']);
816+
803817
case 'uploadWithId':
804818
assertArrayHasKey('id', $args);
805819
$args['_id'] = $args['id'];

tests/UnifiedSpecTests/Util.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,11 @@ final class Util
132132
],
133133
Bucket::class => [
134134
'delete' => ['id'],
135+
'deleteByName' => ['filename'],
135136
'downloadByName' => ['filename', 'revision'],
136137
'download' => ['id'],
137138
'rename' => ['id', 'newFilename'],
139+
'renameByName' => ['filename', 'newFilename'],
138140
'uploadWithId' => ['id', 'filename', 'source', 'chunkSizeBytes', 'metadata'],
139141
'upload' => ['filename', 'source', 'chunkSizeBytes', 'metadata'],
140142
],

0 commit comments

Comments
 (0)