Commit 1676d914 by Alexander Mohorev

Add unit tests

parent 61cacdfc
......@@ -187,6 +187,60 @@ class CollectionTest extends MongoDbTestCase
$this->assertNotEmpty($result[0]['items']);
}
public function testFindAndModify()
{
$collection = $this->getConnection()->getCollection('customer');
$rows = [
[
'name' => 'customer 1',
'status' => 1,
'amount' => 100,
],
[
'name' => 'customer 2',
'status' => 1,
'amount' => 200,
],
];
$collection->batchInsert($rows);
// increment field
$result = $collection->findAndModify(['name' => 'customer 1'], ['$inc' => ['status' => 1]]);
$this->assertEquals('customer 1', $result['name']);
$this->assertEquals(1, $result['status']);
$newResult = $collection->findOne(['name' => 'customer 1']);
$this->assertEquals(2, $newResult['status']);
// $set and return modified document
$result = $collection->findAndModify(
['name' => 'customer 2'],
['$set' => ['status' => 2]],
[],
['new' => true]
);
$this->assertEquals('customer 2', $result['name']);
$this->assertEquals(2, $result['status']);
// Full update document
$data = [
'name' => 'customer 3',
'city' => 'Minsk'
];
$result = $collection->findAndModify(
['name' => 'customer 2'],
$data,
[],
['new' => true]
);
$this->assertEquals('customer 3', $result['name']);
$this->assertEquals('Minsk', $result['city']);
$this->assertTrue(!isset($result['status']));
// Test exceptions
$this->setExpectedException('\yii\mongodb\Exception');
$collection->findAndModify(['name' => 'customer 1'], ['$wrongOperator' => ['status' => 1]]);
}
/**
* @depends testBatchInsert
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment