public function testShouldSave($entity, $writeConcern, $shouldFireEventAfter, $expected)
{
// Arrange
$connPool = m::mock(Pool::class);
$mapper = m::mock(DataMapper::class . '[parseToDocument,getCollection]', [$connPool]);
$options = ['writeConcern' => new WriteConcern($writeConcern)];
$collection = m::mock(Collection::class);
$parsedObject = ['_id' => 123];
$operationResult = m::mock();
$entity->_id = null;
// Act
$mapper->shouldAllowMockingProtectedMethods();
$mapper->shouldReceive('parseToDocument')->once()->with($entity)->andReturn($parsedObject);
$mapper->shouldReceive('getCollection')->once()->andReturn($collection);
$collection->shouldReceive('replaceOne')->once()->with(['_id' => 123], $parsedObject, ['upsert' => true, 'writeConcern' => new WriteConcern($writeConcern)])->andReturn($operationResult);
$operationResult->shouldReceive('isAcknowledged')->once()->andReturn((bool) $writeConcern);
$operationResult->shouldReceive('getModifiedCount', 'getUpsertedCount')->andReturn(1);
if ($entity instanceof AttributesAccessInterface) {
$entity->shouldReceive('syncOriginalAttributes')->once()->with();
}
$this->expectEventToBeFired('saving', $entity, true);
if ($shouldFireEventAfter) {
$this->expectEventToBeFired('saved', $entity, false);
} else {
$this->expectEventNotToBeFired('saved', $entity);
}
// Assert
$this->assertEquals($expected, $mapper->save($entity, $options));
}