diff --git a/src/Database.php b/src/Database.php index 74089ec6f..265772b53 100644 --- a/src/Database.php +++ b/src/Database.php @@ -623,6 +623,7 @@ public function watch(array|Pipeline $pipeline = [], array $options = []): Chang public function withOptions(array $options = []): Database { $options += [ + 'builderEncoder' => $this->builderEncoder, 'readConcern' => $this->readConcern, 'readPreference' => $this->readPreference, 'typeMap' => $this->typeMap, diff --git a/tests/Collection/CollectionFunctionalTest.php b/tests/Collection/CollectionFunctionalTest.php index 27e3b7d1d..581041888 100644 --- a/tests/Collection/CollectionFunctionalTest.php +++ b/tests/Collection/CollectionFunctionalTest.php @@ -3,6 +3,7 @@ namespace MongoDB\Tests\Collection; use Closure; +use MongoDB\Codec\DocumentCodec; use MongoDB\Codec\Encoder; use MongoDB\Collection; use MongoDB\Database; @@ -369,6 +370,8 @@ public function testRenameToDifferentDatabase(): void public function testWithOptionsInheritsOptions(): void { $collectionOptions = [ + 'builderEncoder' => $this->createMock(Encoder::class), + 'codec' => $this->createMock(DocumentCodec::class), 'readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED), 'typeMap' => ['root' => 'array'], @@ -382,20 +385,17 @@ public function testWithOptionsInheritsOptions(): void $this->assertSame($this->manager, $debug['manager']); $this->assertSame($this->getDatabaseName(), $debug['databaseName']); $this->assertSame($this->getCollectionName(), $debug['collectionName']); - $this->assertInstanceOf(ReadConcern::class, $debug['readConcern']); - $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel()); - $this->assertInstanceOf(ReadPreference::class, $debug['readPreference']); - $this->assertSame(ReadPreference::SECONDARY_PREFERRED, $debug['readPreference']->getModeString()); - $this->assertIsArray($debug['typeMap']); - $this->assertSame(['root' => 'array'], $debug['typeMap']); - $this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']); - $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); + + foreach ($collectionOptions as $key => $value) { + $this->assertSame($value, $debug[$key]); + } } public function testWithOptionsPassesOptions(): void { $collectionOptions = [ - 'builderEncoder' => $builderEncoder = $this->createMock(Encoder::class), + 'builderEncoder' => $this->createMock(Encoder::class), + 'codec' => $this->createMock(DocumentCodec::class), 'readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED), 'typeMap' => ['root' => 'array'], @@ -405,15 +405,9 @@ public function testWithOptionsPassesOptions(): void $clone = $this->collection->withOptions($collectionOptions); $debug = $clone->__debugInfo(); - $this->assertSame($builderEncoder, $debug['builderEncoder']); - $this->assertInstanceOf(ReadConcern::class, $debug['readConcern']); - $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel()); - $this->assertInstanceOf(ReadPreference::class, $debug['readPreference']); - $this->assertSame(ReadPreference::SECONDARY_PREFERRED, $debug['readPreference']->getModeString()); - $this->assertIsArray($debug['typeMap']); - $this->assertSame(['root' => 'array'], $debug['typeMap']); - $this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']); - $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); + foreach ($collectionOptions as $key => $value) { + $this->assertSame($value, $debug[$key]); + } } public static function collectionMethodClosures() diff --git a/tests/Database/DatabaseFunctionalTest.php b/tests/Database/DatabaseFunctionalTest.php index 2da985aef..7929f0350 100644 --- a/tests/Database/DatabaseFunctionalTest.php +++ b/tests/Database/DatabaseFunctionalTest.php @@ -3,6 +3,7 @@ namespace MongoDB\Tests\Database; use MongoDB\BSON\PackedArray; +use MongoDB\Codec\Encoder; use MongoDB\Collection; use MongoDB\Database; use MongoDB\Driver\BulkWrite; @@ -50,6 +51,7 @@ public function testConstructorOptionTypeChecks(array $options): void public static function provideInvalidConstructorOptions() { return self::createOptionDataProvider([ + 'builderEncoder' => self::getInvalidObjectValues(), 'readConcern' => self::getInvalidReadConcernValues(), 'readPreference' => self::getInvalidReadPreferenceValues(), 'typeMap' => self::getInvalidArrayValues(), @@ -366,6 +368,7 @@ public function testSelectGridFSBucketPassesOptions(): void public function testWithOptionsInheritsOptions(): void { $databaseOptions = [ + 'builderEncoder' => $this->createMock(Encoder::class), 'readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED), 'typeMap' => ['root' => 'array'], @@ -378,19 +381,16 @@ public function testWithOptionsInheritsOptions(): void $this->assertSame($this->manager, $debug['manager']); $this->assertSame($this->getDatabaseName(), $debug['databaseName']); - $this->assertInstanceOf(ReadConcern::class, $debug['readConcern']); - $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel()); - $this->assertInstanceOf(ReadPreference::class, $debug['readPreference']); - $this->assertSame(ReadPreference::SECONDARY_PREFERRED, $debug['readPreference']->getModeString()); - $this->assertIsArray($debug['typeMap']); - $this->assertSame(['root' => 'array'], $debug['typeMap']); - $this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']); - $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); + + foreach ($databaseOptions as $key => $value) { + $this->assertSame($value, $debug[$key]); + } } public function testWithOptionsPassesOptions(): void { $databaseOptions = [ + 'builderEncoder' => $this->createMock(Encoder::class), 'readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED), 'typeMap' => ['root' => 'array'], @@ -400,13 +400,8 @@ public function testWithOptionsPassesOptions(): void $clone = $this->database->withOptions($databaseOptions); $debug = $clone->__debugInfo(); - $this->assertInstanceOf(ReadConcern::class, $debug['readConcern']); - $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel()); - $this->assertInstanceOf(ReadPreference::class, $debug['readPreference']); - $this->assertSame(ReadPreference::SECONDARY_PREFERRED, $debug['readPreference']->getModeString()); - $this->assertIsArray($debug['typeMap']); - $this->assertSame(['root' => 'array'], $debug['typeMap']); - $this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']); - $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); + foreach ($databaseOptions as $key => $value) { + $this->assertSame($value, $debug[$key]); + } } }