Skip to content

PHPLIB-1646: Inherit builderEncoder in Database::withOptions() #1640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ public function watch(array $pipeline = [], array $options = [])
public function withOptions(array $options = [])
{
$options += [
'builderEncoder' => $this->builderEncoder,
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
'typeMap' => $this->typeMap,
Expand Down
30 changes: 12 additions & 18 deletions tests/Collection/CollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use MongoDB\BSON\Javascript;
use MongoDB\Codec\DocumentCodec;
use MongoDB\Codec\Encoder;
use MongoDB\Collection;
use MongoDB\Database;
Expand Down Expand Up @@ -376,6 +377,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'],
Expand All @@ -389,20 +392,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'],
Expand All @@ -412,15 +412,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]);
}
}

#[Group('matrix-testing-exclude-server-4.4-driver-4.0')]
Expand Down
27 changes: 11 additions & 16 deletions tests/Database/DatabaseFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -370,6 +372,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'],
Expand All @@ -382,19 +385,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'],
Expand All @@ -404,13 +404,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]);
}
}
}
Loading