Skip to content

Commit

Permalink
PHPLIB-1142: Use disableMD5 when provided to GridFS Bucket construc…
Browse files Browse the repository at this point in the history
…tor (#1104)

Support for the disableMD5 option of to the Bucket constructor for file uploads
Bugfix, the option value set to the constructor was ignored.
Add functional test on gridfs option passed to the constructor
  • Loading branch information
GromNaN authored Jun 14, 2023
1 parent 8c999d1 commit 4fd79ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public function __debugInfo()
return [
'bucketName' => $this->bucketName,
'databaseName' => $this->databaseName,
'disableMD5' => $this->disableMD5,
'manager' => $this->manager,
'chunkSizeBytes' => $this->chunkSizeBytes,
'readConcern' => $this->readConcern,
Expand Down Expand Up @@ -547,7 +548,10 @@ public function openDownloadStreamByName(string $filename, array $options = [])
*/
public function openUploadStream(string $filename, array $options = [])
{
$options += ['chunkSizeBytes' => $this->chunkSizeBytes];
$options += [
'chunkSizeBytes' => $this->chunkSizeBytes,
'disableMD5' => $this->disableMD5,
];

$path = $this->createPathForUpload();
$context = stream_context_create([
Expand Down
27 changes: 27 additions & 0 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function testValidConstructorOptions(): void
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
'readPreference' => new ReadPreference(ReadPreference::PRIMARY),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY, 1000),
'disableMD5' => true,
]);
}

Expand Down Expand Up @@ -669,6 +670,32 @@ public function testUploadingAnEmptyFile(): void
$this->assertSameDocument($expected, $fileDocument);
}

public function testDisableMD5(): void
{
$options = ['disableMD5' => true];
$id = $this->bucket->uploadFromStream('filename', $this->createStream('data'), $options);

$fileDocument = $this->filesCollection->findOne(
['_id' => $id]
);

$this->assertArrayNotHasKey('md5', $fileDocument);
}

public function testDisableMD5OptionInConstructor(): void
{
$options = ['disableMD5' => true];

$this->bucket = new Bucket($this->manager, $this->getDatabaseName(), $options);
$id = $this->bucket->uploadFromStream('filename', $this->createStream('data'));

$fileDocument = $this->filesCollection->findOne(
['_id' => $id]
);

$this->assertArrayNotHasKey('md5', $fileDocument);
}

public function testUploadingFirstFileCreatesIndexes(): void
{
$this->bucket->uploadFromStream('filename', $this->createStream('foo'));
Expand Down
4 changes: 2 additions & 2 deletions tests/GridFS/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function setUp(): void
$this->bucket = new Bucket($this->manager, $this->getDatabaseName());
$this->bucket->drop();

$this->chunksCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.chunks');
$this->filesCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.files');
$this->chunksCollection = $this->createCollection($this->getDatabaseName(), 'fs.chunks');
$this->filesCollection = $this->createCollection($this->getDatabaseName(), 'fs.files');
}

/**
Expand Down

0 comments on commit 4fd79ac

Please sign in to comment.