Skip to content

Commit b2fb5b2

Browse files
committed
PHPLIB-1568 Add GridFS\Bucket::deleteByName(filename)
1 parent a6396a4 commit b2fb5b2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/GridFS/Bucket.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,19 @@ public function delete(mixed $id)
242242
}
243243
}
244244

245+
/**
246+
* Delete all the revisions of a file name from the GridFS bucket.
247+
*
248+
* @param string $filename Filename
249+
*
250+
* @throws FileNotFoundException if no file could be selected
251+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
252+
*/
253+
public function deleteByName(string $filename): void
254+
{
255+
$this->collectionWrapper->deleteFileAndChunksByFilename($filename);
256+
}
257+
245258
/**
246259
* Writes the contents of a GridFS file to a writable stream.
247260
*

tests/GridFS/BucketFunctionalTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,41 @@ public function testDeleteStillRemovesChunksIfFileDoesNotExist($input, $expected
160160
$this->assertCollectionCount($this->chunksCollection, 0);
161161
}
162162

163+
public function testDeleteByName(): void
164+
{
165+
$this->bucket->uploadFromStream('filename', self::createStream('foobar1'));
166+
$this->bucket->uploadFromStream('filename', self::createStream('foobar2'));
167+
$this->bucket->uploadFromStream('filename', self::createStream('foobar3'));
168+
169+
$this->bucket->uploadFromStream('other', self::createStream('foobar'));
170+
171+
$this->assertCollectionCount($this->filesCollection, 4);
172+
$this->assertCollectionCount($this->chunksCollection, 4);
173+
174+
$this->bucket->deleteByName('filename');
175+
176+
$this->assertCollectionCount($this->filesCollection, 1);
177+
$this->assertCollectionCount($this->chunksCollection, 1);
178+
179+
$this->bucket->deleteByName('other');
180+
181+
$this->assertCollectionCount($this->filesCollection, 0);
182+
$this->assertCollectionCount($this->chunksCollection, 0);
183+
}
184+
185+
public function testDeleteByNameShouldIgnoreNonexistentFiles(): void
186+
{
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);
196+
}
197+
163198
public function testDownloadingFileWithMissingChunk(): void
164199
{
165200
$id = $this->bucket->uploadFromStream('filename', self::createStream('foobar'));

0 commit comments

Comments
 (0)