Skip to content

Commit 9803a12

Browse files
authored
PHPLIB-1646: Inherit builderEncoder in Database::withOptions() (#1640)
Adds missing options to tests for Database and Collection::withOptions(). Also simplifies assertion logic.
1 parent 37bc8df commit 9803a12

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

src/Database.php

+1
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ public function watch(array $pipeline = [], array $options = [])
680680
public function withOptions(array $options = [])
681681
{
682682
$options += [
683+
'builderEncoder' => $this->builderEncoder,
683684
'readConcern' => $this->readConcern,
684685
'readPreference' => $this->readPreference,
685686
'typeMap' => $this->typeMap,

tests/Collection/CollectionFunctionalTest.php

+12-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use MongoDB\BSON\Javascript;
7+
use MongoDB\Codec\DocumentCodec;
78
use MongoDB\Codec\Encoder;
89
use MongoDB\Collection;
910
use MongoDB\Database;
@@ -376,6 +377,8 @@ public function testRenameToDifferentDatabase(): void
376377
public function testWithOptionsInheritsOptions(): void
377378
{
378379
$collectionOptions = [
380+
'builderEncoder' => $this->createMock(Encoder::class),
381+
'codec' => $this->createMock(DocumentCodec::class),
379382
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
380383
'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED),
381384
'typeMap' => ['root' => 'array'],
@@ -389,20 +392,17 @@ public function testWithOptionsInheritsOptions(): void
389392
$this->assertSame($this->manager, $debug['manager']);
390393
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
391394
$this->assertSame($this->getCollectionName(), $debug['collectionName']);
392-
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
393-
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
394-
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
395-
$this->assertSame(ReadPreference::SECONDARY_PREFERRED, $debug['readPreference']->getModeString());
396-
$this->assertIsArray($debug['typeMap']);
397-
$this->assertSame(['root' => 'array'], $debug['typeMap']);
398-
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
399-
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
395+
396+
foreach ($collectionOptions as $key => $value) {
397+
$this->assertSame($value, $debug[$key]);
398+
}
400399
}
401400

402401
public function testWithOptionsPassesOptions(): void
403402
{
404403
$collectionOptions = [
405-
'builderEncoder' => $builderEncoder = $this->createMock(Encoder::class),
404+
'builderEncoder' => $this->createMock(Encoder::class),
405+
'codec' => $this->createMock(DocumentCodec::class),
406406
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
407407
'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED),
408408
'typeMap' => ['root' => 'array'],
@@ -412,15 +412,9 @@ public function testWithOptionsPassesOptions(): void
412412
$clone = $this->collection->withOptions($collectionOptions);
413413
$debug = $clone->__debugInfo();
414414

415-
$this->assertSame($builderEncoder, $debug['builderEncoder']);
416-
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
417-
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
418-
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
419-
$this->assertSame(ReadPreference::SECONDARY_PREFERRED, $debug['readPreference']->getModeString());
420-
$this->assertIsArray($debug['typeMap']);
421-
$this->assertSame(['root' => 'array'], $debug['typeMap']);
422-
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
423-
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
415+
foreach ($collectionOptions as $key => $value) {
416+
$this->assertSame($value, $debug[$key]);
417+
}
424418
}
425419

426420
#[Group('matrix-testing-exclude-server-4.4-driver-4.0')]

tests/Database/DatabaseFunctionalTest.php

+11-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Tests\Database;
44

55
use MongoDB\BSON\PackedArray;
6+
use MongoDB\Codec\Encoder;
67
use MongoDB\Collection;
78
use MongoDB\Database;
89
use MongoDB\Driver\BulkWrite;
@@ -50,6 +51,7 @@ public function testConstructorOptionTypeChecks(array $options): void
5051
public static function provideInvalidConstructorOptions()
5152
{
5253
return self::createOptionDataProvider([
54+
'builderEncoder' => self::getInvalidObjectValues(),
5355
'readConcern' => self::getInvalidReadConcernValues(),
5456
'readPreference' => self::getInvalidReadPreferenceValues(),
5557
'typeMap' => self::getInvalidArrayValues(),
@@ -370,6 +372,7 @@ public function testSelectGridFSBucketPassesOptions(): void
370372
public function testWithOptionsInheritsOptions(): void
371373
{
372374
$databaseOptions = [
375+
'builderEncoder' => $this->createMock(Encoder::class),
373376
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
374377
'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED),
375378
'typeMap' => ['root' => 'array'],
@@ -382,19 +385,16 @@ public function testWithOptionsInheritsOptions(): void
382385

383386
$this->assertSame($this->manager, $debug['manager']);
384387
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
385-
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
386-
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
387-
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
388-
$this->assertSame(ReadPreference::SECONDARY_PREFERRED, $debug['readPreference']->getModeString());
389-
$this->assertIsArray($debug['typeMap']);
390-
$this->assertSame(['root' => 'array'], $debug['typeMap']);
391-
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
392-
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
388+
389+
foreach ($databaseOptions as $key => $value) {
390+
$this->assertSame($value, $debug[$key]);
391+
}
393392
}
394393

395394
public function testWithOptionsPassesOptions(): void
396395
{
397396
$databaseOptions = [
397+
'builderEncoder' => $this->createMock(Encoder::class),
398398
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
399399
'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED),
400400
'typeMap' => ['root' => 'array'],
@@ -404,13 +404,8 @@ public function testWithOptionsPassesOptions(): void
404404
$clone = $this->database->withOptions($databaseOptions);
405405
$debug = $clone->__debugInfo();
406406

407-
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
408-
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
409-
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
410-
$this->assertSame(ReadPreference::SECONDARY_PREFERRED, $debug['readPreference']->getModeString());
411-
$this->assertIsArray($debug['typeMap']);
412-
$this->assertSame(['root' => 'array'], $debug['typeMap']);
413-
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
414-
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
407+
foreach ($databaseOptions as $key => $value) {
408+
$this->assertSame($value, $debug[$key]);
409+
}
415410
}
416411
}

0 commit comments

Comments
 (0)