From 5d218ad47b0d9dacd61fc83ce45725944f63289c Mon Sep 17 00:00:00 2001 From: David Jonas Date: Wed, 18 Oct 2017 13:23:37 -0200 Subject: [PATCH 1/2] Add operator parameter on updateOne method --- src/Mongolid/DataMapper/BulkWrite.php | 10 +++++-- tests/Mongolid/DataMapper/BulkWriteTest.php | 29 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Mongolid/DataMapper/BulkWrite.php b/src/Mongolid/DataMapper/BulkWrite.php index af7cb82f..f1af5ab0 100644 --- a/src/Mongolid/DataMapper/BulkWrite.php +++ b/src/Mongolid/DataMapper/BulkWrite.php @@ -84,12 +84,18 @@ public function setBulkWrite(MongoBulkWrite $bulkWrite) * @param ObjectID|string $id * @param array $dataToSet * @param array $options + * @param string $operator */ - public function updateOne($id, array $dataToSet, array $options = ['upsert' => true]) + public function updateOne( + $id, + array $dataToSet, + array $options = ['upsert' => true], + string $operator = '$set' + ) { return $this->getBulkWrite()->update( ['_id' => $id], - ['$set' => $dataToSet], + [$operator => $dataToSet], $options ); } diff --git a/tests/Mongolid/DataMapper/BulkWriteTest.php b/tests/Mongolid/DataMapper/BulkWriteTest.php index 9c5e659e..47027a01 100644 --- a/tests/Mongolid/DataMapper/BulkWriteTest.php +++ b/tests/Mongolid/DataMapper/BulkWriteTest.php @@ -83,6 +83,35 @@ public function testShouldAddUpdateOneOperationToBulkWrite() $bulkWrite->updateOne($id, $data); } + public function testShouldUpdateOneWithUnsetOperationToBulkWrite() + { + // Arrange + $entity = m::mock(HasSchemaInterface::class); + $mongoBulkWrite = m::mock(new MongoBulkWrite()); + + $id = '123'; + $data = ['name' => 'John']; + + // Expect + $entity->shouldReceive('getSchema') + ->withNoArgs() + ->once(); + + $mongoBulkWrite->shouldReceive('update') + ->with(['_id' => $id], ['$unset' => $data], ['upsert' => true]) + ->once(); + + $bulkWrite = m::mock(BulkWrite::class.'[getBulkWrite]', [$entity]); + + $bulkWrite->shouldReceive('getBulkWrite') + ->with() + ->once() + ->andReturn($mongoBulkWrite); + + // Act + $bulkWrite->updateOne($id, $data, ['upsert' => true], '$unset'); + } + public function testShouldExecuteBulkWrite() { $entity = m::mock(HasSchemaInterface::class); From 8ea36d6ce997a6b82f0db3b03f7943928cffeafe Mon Sep 17 00:00:00 2001 From: David Jonas Date: Wed, 18 Oct 2017 13:25:58 -0200 Subject: [PATCH 2/2] Style code fix --- src/Mongolid/DataMapper/BulkWrite.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Mongolid/DataMapper/BulkWrite.php b/src/Mongolid/DataMapper/BulkWrite.php index f1af5ab0..0bc0217b 100644 --- a/src/Mongolid/DataMapper/BulkWrite.php +++ b/src/Mongolid/DataMapper/BulkWrite.php @@ -91,8 +91,7 @@ public function updateOne( array $dataToSet, array $options = ['upsert' => true], string $operator = '$set' - ) - { + ) { return $this->getBulkWrite()->update( ['_id' => $id], [$operator => $dataToSet],