Skip to content

Commit

Permalink
Merge v1.x into v2.x (#1512)
Browse files Browse the repository at this point in the history
  • Loading branch information
mongodb-php-bot authored Nov 4, 2024
2 parents 99ded15 + 8a687f2 commit f9fac57
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ public function delete(mixed $id): void
}
}

/**
* Delete all the revisions of a file name from the GridFS bucket.
*
* @param string $filename Filename
*
* @throws FileNotFoundException if no file could be selected
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function deleteByName(string $filename): void
{
$count = $this->collectionWrapper->deleteFileAndChunksByFilename($filename);

if ($count === 0) {
throw FileNotFoundException::byFilename($filename);
}
}

/**
* Writes the contents of a GridFS file to a writable stream.
*
Expand Down Expand Up @@ -590,6 +607,24 @@ public function rename(mixed $id, string $newFilename): void
}
}

/**
* Renames all the revisions of a file name in the GridFS bucket.
*
* @param string $filename Filename
* @param string $newFilename New filename
*
* @throws FileNotFoundException if no file could be selected
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function renameByName(string $filename, string $newFilename): void
{
$count = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename);

if ($count === 0) {
throw FileNotFoundException::byFilename($filename);
}
}

/**
* Writes the contents of a readable stream to a GridFS file.
*
Expand Down
3 changes: 0 additions & 3 deletions src/GridFS/CollectionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ public function findChunksByFileId(mixed $id, int $fromChunk = 0): CursorInterfa
*/
public function findFileByFilenameAndRevision(string $filename, int $revision): ?object
{
$filename = $filename;
$revision = $revision;

if ($revision < 0) {
$skip = abs($revision) - 1;
$sortOrder = -1;
Expand Down
46 changes: 46 additions & 0 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,34 @@ public function testDeleteStillRemovesChunksIfFileDoesNotExist($input, $expected
$this->assertCollectionCount($this->chunksCollection, 0);
}

public function testDeleteByName(): void
{
$this->bucket->uploadFromStream('filename', self::createStream('foobar1'));
$this->bucket->uploadFromStream('filename', self::createStream('foobar2'));
$this->bucket->uploadFromStream('filename', self::createStream('foobar3'));

$this->bucket->uploadFromStream('other', self::createStream('foobar'));

$this->assertCollectionCount($this->filesCollection, 4);
$this->assertCollectionCount($this->chunksCollection, 4);

$this->bucket->deleteByName('filename');

$this->assertCollectionCount($this->filesCollection, 1);
$this->assertCollectionCount($this->chunksCollection, 1);

$this->bucket->deleteByName('other');

$this->assertCollectionCount($this->filesCollection, 0);
$this->assertCollectionCount($this->chunksCollection, 0);
}

public function testDeleteByNameShouldRequireFileToExist(): void
{
$this->expectException(FileNotFoundException::class);
$this->bucket->deleteByName('nonexistent-name');
}

public function testDownloadingFileWithMissingChunk(): void
{
$id = $this->bucket->uploadFromStream('filename', self::createStream('foobar'));
Expand Down Expand Up @@ -721,6 +749,24 @@ public function testRenameShouldRequireFileToExist(): void
$this->bucket->rename('nonexistent-id', 'b');
}

public function testRenameByName(): void
{
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
$this->bucket->uploadFromStream('filename', self::createStream('foo'));

$this->bucket->renameByName('filename', 'newname');

$this->assertNull($this->bucket->findOne(['filename' => 'filename']), 'No file has the old name');
$this->assertStreamContents('foo', $this->bucket->openDownloadStreamByName('newname'));
}

public function testRenameByNameShouldRequireFileToExist(): void
{
$this->expectException(FileNotFoundException::class);
$this->bucket->renameByName('nonexistent-name', 'b');
}

public function testUploadFromStream(): void
{
$options = [
Expand Down
14 changes: 14 additions & 0 deletions tests/UnifiedSpecTests/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,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']);
Expand All @@ -800,6 +806,14 @@ private function executeForBucket(Bucket $bucket)

return null;

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'];
Expand Down
2 changes: 2 additions & 0 deletions tests/UnifiedSpecTests/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +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', 'metadata'],
'upload' => ['filename', 'source', 'chunkSizeBytes', 'metadata'],
],
Expand Down

0 comments on commit f9fac57

Please sign in to comment.