Commit 362a27ff authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-58: Functional tests for CRUD spec write methods

parent 89391d25
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for deleteMany().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class DeleteManyFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testDeleteManyWhenManyDocumentsMatch()
{
$filter = array('_id' => array('$gt' => 1));
$result = $this->collection->deleteMany($filter);
$this->assertSame(2, $result->getDeletedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testDeleteManyWhenNoDocumentsMatch()
{
$filter = array('_id' => 4);
$result = $this->collection->deleteMany($filter);
$this->assertSame(0, $result->getDeletedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for deleteOne().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class DeleteOneFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testDeleteOneWhenManyDocumentsMatch()
{
$filter = array('_id' => array('$gt' => 1));
$result = $this->collection->deleteOne($filter);
$this->assertSame(1, $result->getDeletedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testDeleteOneWhenOneDocumentMatches()
{
$filter = array('_id' => 2);
$result = $this->collection->deleteOne($filter);
$this->assertSame(1, $result->getDeletedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testDeleteOneWhenNoDocumentsMatch()
{
$filter = array('_id' => 4);
$result = $this->collection->deleteOne($filter);
$this->assertSame(0, $result->getDeletedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for findOneAndDelete().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class FindOneAndDeleteFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testFindOneAndDeleteWhenManyDocumentsMatch()
{
$filter = array('_id' => array('$gt' => 1));
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
);
$document = $this->collection->findOneAndDelete($filter, $options);
$this->assertSame(array('x' => 22), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndDeleteWhenOneDocumentMatches()
{
$filter = array('_id' => 2);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
);
$document = $this->collection->findOneAndDelete($filter, $options);
$this->assertSame(array('x' => 22), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndDeleteWhenNoDocumentsMatch()
{
$filter = array('_id' => 4);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
);
$document = $this->collection->findOneAndDelete($filter, $options);
$this->assertNull($document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
use MongoDB\Collection;
/**
* CRUD spec functional tests for findOneAndReplace().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentBeforeModification()
{
$filter = array('_id' => array('$gt' => 1));
$replacement = array('x' => 32);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
);
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSame(array('x' => 22), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 32),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfterModification()
{
$filter = array('_id' => array('$gt' => 1));
$replacement = array('x' => 32);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
);
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSame(array('x' => 32), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 32),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBeforeModification()
{
$filter = array('_id' => 2);
$replacement = array('x' => 32);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
);
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSame(array('x' => 22), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 32),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfterModification()
{
$filter = array('_id' => 2);
$replacement = array('x' => 32);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
);
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSame(array('x' => 32), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 32),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentBeforeModification()
{
$filter = array('_id' => 4);
$replacement = array('x' => 44);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
);
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertNull($document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification()
{
$filter = array('_id' => 4);
$replacement = array('x' => 44);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'upsert' => true,
);
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertNull($document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
array('_id' => 4, 'x' => 44),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentAfterModification()
{
$filter = array('_id' => 4);
$replacement = array('x' => 44);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
);
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertNull($document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification()
{
$filter = array('_id' => 4);
$replacement = array('x' => 44);
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
'upsert' => true,
);
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSame(array('x' => 44), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
array('_id' => 4, 'x' => 44),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
use MongoDB\Collection;
/**
* CRUD spec functional tests for findOneAndUpdate().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentBeforeModification()
{
$filter = array('_id' => array('$gt' => 1));
$update = array('$inc' => array('x' => 1));
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
);
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertSame(array('x' => 22), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 23),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentAfterModification()
{
$filter = array('_id' => array('$gt' => 1));
$update = array('$inc' => array('x' => 1));
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
);
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertSame(array('x' => 23), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 23),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentBeforeModification()
{
$filter = array('_id' => 2);
$update = array('$inc' => array('x' => 1));
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
);
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertSame(array('x' => 22), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 23),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentAfterModification()
{
$filter = array('_id' => 2);
$update = array('$inc' => array('x' => 1));
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
);
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertSame(array('x' => 23), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 23),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentBeforeModification()
{
$filter = array('_id' => 4);
$update = array('$inc' => array('x' => 1));
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
);
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertNull($document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification()
{
$filter = array('_id' => 4);
$update = array('$inc' => array('x' => 1));
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'upsert' => true,
);
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertNull($document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
array('_id' => 4, 'x' => 1),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentAfterModification()
{
$filter = array('_id' => 4);
$update = array('$inc' => array('x' => 1));
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
);
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertNull($document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification()
{
$filter = array('_id' => 4);
$update = array('$inc' => array('x' => 1));
$options = array(
'projection' => array('x' => 1, '_id' => 0),
'sort' => array('x' => 1),
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
'upsert' => true,
);
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertSame(array('x' => 1), $document);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
array('_id' => 4, 'x' => 1),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for insertMany().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class InsertManyFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(1);
}
public function testInsertManyWithNonexistentDocuments()
{
$documents = array(
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$result = $this->collection->insertMany($documents);
$this->assertSame(2, $result->getInsertedCount());
$this->assertSame(array(2, 3), $result->getInsertedIds());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for insertOne().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class InsertOneFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(1);
}
public function testInsertOneWithANonexistentDocument()
{
$document = array('_id' => 2, 'x' => 22);
$result = $this->collection->insertOne($document);
$this->assertSame(1, $result->getInsertedCount());
$this->assertSame(2, $result->getInsertedId());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for replaceOne().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class ReplaceOneFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testReplaceOneWhenManyDocumentsMatch()
{
$filter = array('_id' => array('$gt' => 1));
$replacement = array('x' => 111);
$result = $this->collection->replaceOne($filter, $replacement);
$this->assertSame(1, $result->getMatchedCount());
$this->assertSame(1, $result->getModifiedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 111),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testReplaceOneWhenOneDocumentMatches()
{
$filter = array('_id' => 1);
$replacement = array('x' => 111);
$result = $this->collection->replaceOne($filter, $replacement);
$this->assertSame(1, $result->getMatchedCount());
$this->assertSame(1, $result->getModifiedCount());
$expected = array(
array('_id' => 1, 'x' => 111),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testReplaceOneWhenNoDocumentsMatch()
{
$filter = array('_id' => 4);
$replacement = array('x' => 111);
$result = $this->collection->replaceOne($filter, $replacement);
$this->assertSame(0, $result->getMatchedCount());
$this->assertSame(0, $result->getModifiedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithAnIdSpecified()
{
$filter = array('_id' => 4);
$replacement = array('_id' => 4, 'x' => 1);
$options = array('upsert' => true);
$result = $this->collection->replaceOne($filter, $replacement, $options);
$this->assertSame(0, $result->getMatchedCount());
$this->assertSame(0, $result->getModifiedCount());
$this->assertSame(4, $result->getUpsertedId());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
array('_id' => 4, 'x' => 1),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithoutAnIdSpecified()
{
$filter = array('_id' => 4);
$replacement = array('x' => 1);
$options = array('upsert' => true);
$result = $this->collection->replaceOne($filter, $replacement, $options);
$this->assertSame(0, $result->getMatchedCount());
$this->assertSame(0, $result->getModifiedCount());
$this->assertSame(4, $result->getUpsertedId());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
array('_id' => 4, 'x' => 1),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for updateMany().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class UpdateManyFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testUpdateManyWhenManyDocumentsMatch()
{
$filter = array('_id' => array('$gt' => 1));
$update = array('$inc' => array('x' => 1));
$result = $this->collection->updateMany($filter, $update);
$this->assertSame(2, $result->getMatchedCount());
$this->assertSame(2, $result->getModifiedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 23),
array('_id' => 3, 'x' => 34),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testUpdateManyWhenOneDocumentMatches()
{
$filter = array('_id' => 1);
$update = array('$inc' => array('x' => 1));
$result = $this->collection->updateMany($filter, $update);
$this->assertSame(1, $result->getMatchedCount());
$this->assertSame(1, $result->getModifiedCount());
$expected = array(
array('_id' => 1, 'x' => 12),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testUpdateManyWhenNoDocumentsMatch()
{
$filter = array('_id' => 4);
$update = array('$inc' => array('x' => 1));
$result = $this->collection->updateMany($filter, $update);
$this->assertSame(0, $result->getMatchedCount());
$this->assertSame(0, $result->getModifiedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testUpdateManyWithUpsertWhenNoDocumentsMatch()
{
$filter = array('_id' => 4);
$update = array('$inc' => array('x' => 1));
$options = array('upsert' => true);
$result = $this->collection->updateMany($filter, $update, $options);
$this->assertSame(0, $result->getMatchedCount());
$this->assertSame(0, $result->getModifiedCount());
$this->assertSame(4, $result->getUpsertedId());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
array('_id' => 4, 'x' => 1),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for updateOne().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class UpdateOneFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testUpdateOneWhenManyDocumentsMatch()
{
$filter = array('_id' => array('$gt' => 1));
$update = array('$inc' => array('x' => 1));
$result = $this->collection->updateOne($filter, $update);
$this->assertSame(1, $result->getMatchedCount());
$this->assertSame(1, $result->getModifiedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 23),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testUpdateOneWhenOneDocumentMatches()
{
$filter = array('_id' => 1);
$update = array('$inc' => array('x' => 1));
$result = $this->collection->updateOne($filter, $update);
$this->assertSame(1, $result->getMatchedCount());
$this->assertSame(1, $result->getModifiedCount());
$expected = array(
array('_id' => 1, 'x' => 12),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testUpdateOneWhenNoDocumentsMatch()
{
$filter = array('_id' => 4);
$update = array('$inc' => array('x' => 1));
$result = $this->collection->updateOne($filter, $update);
$this->assertSame(0, $result->getMatchedCount());
$this->assertSame(0, $result->getModifiedCount());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
public function testUpdateOneWithUpsertWhenNoDocumentsMatch()
{
$filter = array('_id' => 4);
$update = array('$inc' => array('x' => 1));
$options = array('upsert' => true);
$result = $this->collection->updateOne($filter, $update, $options);
$this->assertSame(0, $result->getMatchedCount());
$this->assertSame(0, $result->getModifiedCount());
$this->assertSame(4, $result->getUpsertedId());
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
array('_id' => 4, 'x' => 1),
);
$this->assertSame($expected, $this->collection->find()->toArray());
}
}
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