Skip to content

Commit

Permalink
Fix "w" option
Browse files Browse the repository at this point in the history
  • Loading branch information
ossinkine committed Mar 10, 2021
1 parent a88db3e commit 116f6ac
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\ObjectRepository;
use InvalidArgumentException;
use MongoDB\Driver\WriteConcern;
use ProxyManager\Configuration as ProxyManagerConfiguration;
use ProxyManager\Factory\LazyLoadingGhostFactory;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
Expand Down Expand Up @@ -347,7 +348,7 @@ public function getClassMetadataFactoryName(): string

public function getDefaultCommitOptions(): array
{
return $this->attributes['defaultCommitOptions'] ?? ['w' => 1];
return $this->attributes['defaultCommitOptions'] ?? ['writeConcern' => new WriteConcern(1)];
}

public function setDefaultCommitOptions(array $defaultCommitOptions): void
Expand Down
16 changes: 14 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Exception\Exception as DriverException;
use MongoDB\Driver\Exception\WriteException;
use MongoDB\Driver\WriteConcern;
use MongoDB\GridFS\Bucket;
use ProxyManager\Proxy\GhostObjectInterface;
use stdClass;

use function array_combine;
use function array_fill;
use function array_intersect_key;
use function array_key_exists;
use function array_keys;
use function array_map;
use function array_merge;
Expand All @@ -61,6 +63,9 @@
use function sprintf;
use function strpos;
use function strtolower;
use function trigger_error;

use const E_USER_DEPRECATED;

/**
* The DocumentPersister is responsible for persisting documents.
Expand Down Expand Up @@ -1493,10 +1498,17 @@ private function getWriteOptions(array $options = []): array
$defaultOptions = $this->dm->getConfiguration()->getDefaultCommitOptions();
$documentOptions = [];
if ($this->class->hasWriteConcern()) {
$documentOptions['w'] = $this->class->getWriteConcern();
$documentOptions['writeConcern'] = new WriteConcern($this->class->getWriteConcern());
}

$writeOptions = array_merge($defaultOptions, $documentOptions, $options);
if (array_key_exists('w', $writeOptions)) {
@trigger_error(sprintf('The "w" option as commit option is deprecated, please pass %s object.', WriteConcern::class), E_USER_DEPRECATED);
$writeOptions['writeConcern'] = new WriteConcern($writeOptions['w']);
unset($writeOptions['w']);
}

return array_merge($defaultOptions, $documentOptions, $options);
return $writeOptions;
}

private function prepareReference(string $fieldName, $value, array $mapping, bool $inNewObj): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Collection;
use MongoDB\Driver\WriteConcern;
use ReflectionProperty;

use function get_class;
Expand Down Expand Up @@ -644,7 +645,7 @@ public function testExecuteInsertsRespectsWriteConcern($class, $writeConcern)
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('insertMany')
->with($this->isType('array'), $this->logicalAnd($this->arrayHasKey('w'), $this->containsEqual($writeConcern)));
->with($this->isType('array'), $this->logicalAnd($this->arrayHasKey('writeConcern'), $this->containsEqual(new WriteConcern($writeConcern))));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setAccessible(true);
Expand All @@ -668,7 +669,7 @@ public function testExecuteUpsertsRespectsWriteConcern($class, $writeConcern)
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('updateOne')
->with($this->isType('array'), $this->isType('array'), $this->logicalAnd($this->arrayHasKey('w'), $this->containsEqual($writeConcern)));
->with($this->isType('array'), $this->isType('array'), $this->logicalAnd($this->arrayHasKey('writeConcern'), $this->containsEqual(new WriteConcern($writeConcern))));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setAccessible(true);
Expand All @@ -693,7 +694,7 @@ public function testRemoveRespectsWriteConcern($class, $writeConcern)
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('deleteOne')
->with($this->isType('array'), $this->logicalAnd($this->arrayHasKey('w'), $this->containsEqual($writeConcern)));
->with($this->isType('array'), $this->logicalAnd($this->arrayHasKey('writeConcern'), $this->containsEqual(new WriteConcern($writeConcern))));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setAccessible(true);
Expand All @@ -715,7 +716,28 @@ public function testDefaultWriteConcernIsRespected()
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('insertMany')
->with($this->isType('array'), $this->equalTo(['w' => 0]));
->with($this->isType('array'), $this->equalTo(['writeConcern' => new WriteConcern(0)]));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($documentPersister, $collection);

$this->dm->getConfiguration()->setDefaultCommitOptions(['writeConcern' => new WriteConcern(0)]);

$testDocument = new $class();
$this->dm->persist($testDocument);
$this->dm->flush();
}

public function testDefaultWriteConcernIsRespectedBackwardCompatibility()
{
$class = DocumentPersisterTestDocument::class;
$documentPersister = $this->uow->getDocumentPersister($class);

$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('insertMany')
->with($this->isType('array'), $this->equalTo(['writeConcern' => new WriteConcern(0)]));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setAccessible(true);
Expand Down

0 comments on commit 116f6ac

Please sign in to comment.