Skip to content

Commit

Permalink
Merge pull request #113 from leroy-merlin-br/add-unset-on-bulk-write
Browse files Browse the repository at this point in the history
Add operator parameter on BulkWrite updateOne method
  • Loading branch information
djonasm committed Oct 18, 2017
2 parents 7548292 + 8ea36d6 commit d66a994
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Mongolid/DataMapper/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,17 @@ 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
);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Mongolid/DataMapper/BulkWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d66a994

Please sign in to comment.