Skip to content

Commit d6dce41

Browse files
committed
Add test to ensure bucket codec can be overridden
1 parent 9e37816 commit d6dce41

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/GridFS/Bucket.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use MongoDB\Operation\Find;
3838

3939
use function array_intersect_key;
40+
use function array_key_exists;
4041
use function assert;
4142
use function fopen;
4243
use function get_resource_type;
@@ -319,7 +320,7 @@ public function drop()
319320
*/
320321
public function find($filter = [], array $options = [])
321322
{
322-
if ($this->codec && ! isset($options['codec'])) {
323+
if ($this->codec && ! array_key_exists('codec', $options)) {
323324
$options['codec'] = $this->codec;
324325
}
325326

@@ -340,7 +341,7 @@ public function find($filter = [], array $options = [])
340341
*/
341342
public function findOne($filter = [], array $options = [])
342343
{
343-
if ($this->codec && ! isset($options['codec'])) {
344+
if ($this->codec && ! array_key_exists('codec', $options)) {
344345
$options['codec'] = $this->codec;
345346
}
346347

tests/GridFS/BucketFunctionalTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,18 @@ public function testFindInheritsBucketCodec(): void
344344
$this->assertSame('a', $fileDocument->filename);
345345
}
346346

347+
public function testFindResetsInheritedBucketCodec(): void
348+
{
349+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['codec' => new TestFileCodec()]);
350+
$bucket->uploadFromStream('a', $this->createStream('foo'));
351+
352+
$cursor = $bucket->find([], ['codec' => null]);
353+
$fileDocument = current($cursor->toArray());
354+
355+
$this->assertInstanceOf(BSONDocument::class, $fileDocument);
356+
$this->assertSame('a', $fileDocument->filename);
357+
}
358+
347359
public function testFindOne(): void
348360
{
349361
$this->bucket->uploadFromStream('a', $this->createStream('foo'));
@@ -393,15 +405,33 @@ public function testFindOneInheritsBucketCodec(): void
393405
$bucket->uploadFromStream('b', $this->createStream('foobar'));
394406
$bucket->uploadFromStream('c', $this->createStream('foobarbaz'));
395407

408+
$fileDocument = $bucket->findOne(
409+
['length' => ['$lte' => 6]],
410+
['sort' => ['length' => -1]],
411+
);
412+
413+
$this->assertInstanceOf(TestFile::class, $fileDocument);
414+
$this->assertSame('b', $fileDocument->filename);
415+
$this->assertSame(6, $fileDocument->length);
416+
}
417+
418+
public function testFindOneResetsInheritedBucketCodec(): void
419+
{
420+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['codec' => new TestFileCodec()]);
421+
422+
$bucket->uploadFromStream('a', $this->createStream('foo'));
423+
$bucket->uploadFromStream('b', $this->createStream('foobar'));
424+
$bucket->uploadFromStream('c', $this->createStream('foobarbaz'));
425+
396426
$fileDocument = $bucket->findOne(
397427
['length' => ['$lte' => 6]],
398428
[
399429
'sort' => ['length' => -1],
400-
'codec' => new TestFileCodec(),
430+
'codec' => null,
401431
],
402432
);
403433

404-
$this->assertInstanceOf(TestFile::class, $fileDocument);
434+
$this->assertInstanceOf(BSONDocument::class, $fileDocument);
405435
$this->assertSame('b', $fileDocument->filename);
406436
$this->assertSame(6, $fileDocument->length);
407437
}

0 commit comments

Comments
 (0)