Skip to content

PHPLIB-1702: Ensure withOptions preserves autoEncryptionEnabled #1747

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
Aug 13, 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
4 changes: 4 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ public function __construct(?string $uri = null, array $uriOptions = [], array $
$this->uri = $uri ?? self::DEFAULT_URI;
$this->builderEncoder = $driverOptions['builderEncoder'] ?? new BuilderEncoder();
$this->typeMap = $driverOptions['typeMap'];

/* Database and Collection objects may need to know whether auto
* encryption is enabled for dropping collections. Track this via an
* internal option until PHPC-2615 is implemented. */
Comment on lines +140 to +142
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this kind of comment that gives context and reference.

$this->autoEncryptionEnabled = isset($driverOptions['autoEncryption']['keyVaultNamespace']);

$driverOptions = array_diff_key($driverOptions, ['builderEncoder' => 1, 'typeMap' => 1]);
Expand Down
1 change: 1 addition & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,7 @@ public function watch(array $pipeline = [], array $options = [])
public function withOptions(array $options = [])
{
$options += [
'autoEncryptionEnabled' => $this->autoEncryptionEnabled,
'builderEncoder' => $this->builderEncoder,
'codec' => $this->codec,
'readConcern' => $this->readConcern,
Expand Down
1 change: 1 addition & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ public function watch(array $pipeline = [], array $options = [])
public function withOptions(array $options = [])
{
$options += [
'autoEncryptionEnabled' => $this->autoEncryptionEnabled,
'builderEncoder' => $this->builderEncoder,
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
Expand Down
20 changes: 20 additions & 0 deletions tests/Collection/CollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use MongoDB\Tests\CommandObserver;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use ReflectionClass;
use TypeError;

use function array_filter;
Expand Down Expand Up @@ -396,6 +397,16 @@ public function testWithOptionsInheritsOptions(): void
foreach ($collectionOptions as $key => $value) {
$this->assertSame($value, $debug[$key]);
}

// autoEncryptionEnabled is an internal option not reported via debug info
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), ['autoEncryptionEnabled' => true]);
$clone = $collection->withOptions();

$rc = new ReflectionClass($clone);
$rp = $rc->getProperty('autoEncryptionEnabled');
$rp->setAccessible(true);

$this->assertSame(true, $rp->getValue($clone));
}

public function testWithOptionsPassesOptions(): void
Expand All @@ -415,6 +426,15 @@ public function testWithOptionsPassesOptions(): void
foreach ($collectionOptions as $key => $value) {
$this->assertSame($value, $debug[$key]);
}

// autoEncryptionEnabled is an internal option not reported via debug info
$clone = $this->collection->withOptions(['autoEncryptionEnabled' => true]);

$rc = new ReflectionClass($clone);
$rp = $rc->getProperty('autoEncryptionEnabled');
$rp->setAccessible(true);

$this->assertSame(true, $rp->getValue($clone));
}

#[Group('matrix-testing-exclude-server-4.4-driver-4.0')]
Expand Down
20 changes: 20 additions & 0 deletions tests/Database/DatabaseFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use MongoDB\Operation\CreateIndexes;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use ReflectionClass;
use TypeError;

use function array_key_exists;
Expand Down Expand Up @@ -389,6 +390,16 @@ public function testWithOptionsInheritsOptions(): void
foreach ($databaseOptions as $key => $value) {
$this->assertSame($value, $debug[$key]);
}

// autoEncryptionEnabled is an internal option not reported via debug info
$database = new Database($this->manager, $this->getDatabaseName(), ['autoEncryptionEnabled' => true]);
$clone = $database->withOptions();

$rc = new ReflectionClass($clone);
$rp = $rc->getProperty('autoEncryptionEnabled');
$rp->setAccessible(true);

$this->assertSame(true, $rp->getValue($clone));
}

public function testWithOptionsPassesOptions(): void
Expand All @@ -407,5 +418,14 @@ public function testWithOptionsPassesOptions(): void
foreach ($databaseOptions as $key => $value) {
$this->assertSame($value, $debug[$key]);
}

// autoEncryptionEnabled is an internal option not reported via debug info
$clone = $this->database->withOptions(['autoEncryptionEnabled' => true]);

$rc = new ReflectionClass($clone);
$rp = $rc->getProperty('autoEncryptionEnabled');
$rp->setAccessible(true);

$this->assertSame(true, $rp->getValue($clone));
}
}