Commit ce26ec3c authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #47

parents 34b74f85 5c266962
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
namespace MongoDB; namespace MongoDB;
use MongoDB\BSON\ObjectId;
use MongoDB\Driver\WriteResult; use MongoDB\Driver\WriteResult;
use MongoDB\Exception\BadMethodCallException;
/** /**
* Result class for a bulk write operation. * Result class for a bulk write operation.
...@@ -12,6 +12,7 @@ class BulkWriteResult ...@@ -12,6 +12,7 @@ class BulkWriteResult
{ {
private $writeResult; private $writeResult;
private $insertedIds; private $insertedIds;
private $isAcknowledged;
/** /**
* Constructor. * Constructor.
...@@ -23,41 +24,53 @@ class BulkWriteResult ...@@ -23,41 +24,53 @@ class BulkWriteResult
{ {
$this->writeResult = $writeResult; $this->writeResult = $writeResult;
$this->insertedIds = $insertedIds; $this->insertedIds = $insertedIds;
$this->isAcknowledged = $writeResult->isAcknowledged();
} }
/** /**
* Return the number of documents that were deleted. * Return the number of documents that were deleted.
* *
* This value is undefined if the write was not acknowledged. * This method should only be called if the write was acknowledged.
* *
* @see BulkWriteResult::isAcknowledged() * @see BulkWriteResult::isAcknowledged()
* @return integer * @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getDeletedCount() public function getDeletedCount()
{ {
return $this->writeResult->getDeletedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getDeletedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
* Return the number of documents that were inserted. * Return the number of documents that were inserted.
* *
* This value is undefined if the write was not acknowledged. * This method should only be called if the write was acknowledged.
* *
* @see BulkWriteResult::isAcknowledged() * @see BulkWriteResult::isAcknowledged()
* @return integer * @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getInsertedCount() public function getInsertedCount()
{ {
return $this->writeResult->getInsertedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getInsertedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
* Return a map of the inserted documents' IDs. * Return a map of the inserted documents' IDs.
* *
* The index of each ID in the map corresponds to the document's position * The index of each ID in the map corresponds to the document's position in
* in bulk operation. If the document had an ID prior to insertion (i.e. the * the bulk operation. If the document had an ID prior to insertion (i.e.
* driver did not generate an ID), this will contain its "_id" field value. * the driver did not generate an ID), this will contain its "_id" field
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance. * value. Any driver-generated ID will be an MongoDB\Driver\ObjectID
* instance.
* *
* @return mixed[] * @return mixed[]
*/ */
...@@ -69,41 +82,58 @@ class BulkWriteResult ...@@ -69,41 +82,58 @@ class BulkWriteResult
/** /**
* Return the number of documents that were matched by the filter. * Return the number of documents that were matched by the filter.
* *
* This value is undefined if the write was not acknowledged. * This method should only be called if the write was acknowledged.
* *
* @see BulkWriteResult::isAcknowledged() * @see BulkWriteResult::isAcknowledged()
* @return integer * @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getMatchedCount() public function getMatchedCount()
{ {
return $this->writeResult->getMatchedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getMatchedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
* Return the number of documents that were modified. * Return the number of documents that were modified.
* *
* This value is undefined if the write was not acknowledged or if the write * This value is undefined (i.e. null) if the write executed as a legacy
* executed as a legacy operation instead of write command. * operation instead of command.
*
* This method should only be called if the write was acknowledged.
* *
* @see BulkWriteResult::isAcknowledged() * @see BulkWriteResult::isAcknowledged()
* @return integer|null * @return integer|null
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getModifiedCount() public function getModifiedCount()
{ {
return $this->writeResult->getModifiedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getModifiedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
* Return the number of documents that were upserted. * Return the number of documents that were upserted.
* *
* This value is undefined if the write was not acknowledged. * This method should only be called if the write was acknowledged.
* *
* @see BulkWriteResult::isAcknowledged() * @see BulkWriteResult::isAcknowledged()
* @return integer * @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getUpsertedCount() public function getUpsertedCount()
{ {
return $this->writeResult->getUpsertedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getUpsertedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
...@@ -114,11 +144,19 @@ class BulkWriteResult ...@@ -114,11 +144,19 @@ class BulkWriteResult
* server did not need to generate an ID), this will contain its "_id". Any * server did not need to generate an ID), this will contain its "_id". Any
* server-generated ID will be an MongoDB\Driver\ObjectID instance. * server-generated ID will be an MongoDB\Driver\ObjectID instance.
* *
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return mixed[] * @return mixed[]
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getUpsertedIds() public function getUpsertedIds()
{ {
return $this->writeResult->getUpsertedIds(); if ($this->isAcknowledged) {
return $this->writeResult->getUpsertedIds();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
...@@ -131,6 +169,6 @@ class BulkWriteResult ...@@ -131,6 +169,6 @@ class BulkWriteResult
*/ */
public function isAcknowledged() public function isAcknowledged()
{ {
return $this->writeResult->isAcknowledged(); return $this->isAcknowledged;
} }
} }
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace MongoDB; namespace MongoDB;
use MongoDB\Driver\WriteResult; use MongoDB\Driver\WriteResult;
use MongoDB\Exception\BadMethodCallException;
/** /**
* Result class for a delete operation. * Result class for a delete operation.
...@@ -10,6 +11,7 @@ use MongoDB\Driver\WriteResult; ...@@ -10,6 +11,7 @@ use MongoDB\Driver\WriteResult;
class DeleteResult class DeleteResult
{ {
private $writeResult; private $writeResult;
private $isAcknowledged;
/** /**
* Constructor. * Constructor.
...@@ -19,19 +21,25 @@ class DeleteResult ...@@ -19,19 +21,25 @@ class DeleteResult
public function __construct(WriteResult $writeResult) public function __construct(WriteResult $writeResult)
{ {
$this->writeResult = $writeResult; $this->writeResult = $writeResult;
$this->isAcknowledged = $writeResult->isAcknowledged();
} }
/** /**
* Return the number of documents that were deleted. * Return the number of documents that were deleted.
* *
* This value is undefined if the write was not acknowledged. * This method should only be called if the write was acknowledged.
* *
* @see UpdateResult::isAcknowledged() * @see DeleteResult::isAcknowledged()
* @return integer * @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getDeletedCount() public function getDeletedCount()
{ {
return $this->writeResult->getDeletedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getDeletedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
...@@ -44,6 +52,6 @@ class DeleteResult ...@@ -44,6 +52,6 @@ class DeleteResult
*/ */
public function isAcknowledged() public function isAcknowledged()
{ {
return $this->writeResult->isAcknowledged(); return $this->isAcknowledged;
} }
} }
...@@ -4,4 +4,11 @@ namespace MongoDB\Exception; ...@@ -4,4 +4,11 @@ namespace MongoDB\Exception;
class BadMethodCallException extends \BadMethodCallException implements Exception class BadMethodCallException extends \BadMethodCallException implements Exception
{ {
/**
* Thrown when accessing a result field on an unacknowledged write result.
*/
public static function unacknowledgedWriteResultAccess($method)
{
return new static(sprintf('%s should not be called for an unacknowledged write result', $method));
}
} }
...@@ -3,14 +3,16 @@ ...@@ -3,14 +3,16 @@
namespace MongoDB; namespace MongoDB;
use MongoDB\Driver\WriteResult; use MongoDB\Driver\WriteResult;
use MongoDB\Exception\BadMethodCallException;
/** /**
* Result class for a multi-document write operation. * Result class for a multi-document insert operation.
*/ */
class InsertManyResult class InsertManyResult
{ {
private $writeResult; private $writeResult;
private $insertedIds; private $insertedIds;
private $isAcknowledged;
/** /**
* Constructor. * Constructor.
...@@ -22,28 +24,35 @@ class InsertManyResult ...@@ -22,28 +24,35 @@ class InsertManyResult
{ {
$this->writeResult = $writeResult; $this->writeResult = $writeResult;
$this->insertedIds = $insertedIds; $this->insertedIds = $insertedIds;
$this->isAcknowledged = $writeResult->isAcknowledged();
} }
/** /**
* Return the number of documents that were inserted. * Return the number of documents that were inserted.
* *
* This value is undefined if the write was not acknowledged. * This method should only be called if the write was acknowledged.
* *
* @see InsertManyResult::isAcknowledged() * @see InsertManyResult::isAcknowledged()
* @return integer * @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getInsertedCount() public function getInsertedCount()
{ {
return $this->writeResult->getInsertedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getInsertedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
* Return a map of the inserted documents' IDs. * Return a map of the inserted documents' IDs.
* *
* The index of each ID in the map corresponds to the document's position * The index of each ID in the map corresponds to the document's position in
* in bulk operation. If the document had an ID prior to insertion (i.e. the * the bulk operation. If the document had an ID prior to insertion (i.e.
* driver did not generate an ID), this will contain its "_id" field value. * the driver did not generate an ID), this will contain its "_id" field
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance. * value. Any driver-generated ID will be an MongoDB\Driver\ObjectID
* instance.
* *
* @return mixed[] * @return mixed[]
*/ */
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace MongoDB; namespace MongoDB;
use MongoDB\Driver\WriteResult; use MongoDB\Driver\WriteResult;
use MongoDB\Exception\BadMethodCallException;
/** /**
* Result class for a single-document insert operation. * Result class for a single-document insert operation.
...@@ -11,6 +12,7 @@ class InsertOneResult ...@@ -11,6 +12,7 @@ class InsertOneResult
{ {
private $writeResult; private $writeResult;
private $insertedId; private $insertedId;
private $isAcknowledged;
/** /**
* Constructor. * Constructor.
...@@ -22,19 +24,25 @@ class InsertOneResult ...@@ -22,19 +24,25 @@ class InsertOneResult
{ {
$this->writeResult = $writeResult; $this->writeResult = $writeResult;
$this->insertedId = $insertedId; $this->insertedId = $insertedId;
$this->isAcknowledged = $writeResult->isAcknowledged();
} }
/** /**
* Return the number of documents that were inserted. * Return the number of documents that were inserted.
* *
* This value is undefined if the write was not acknowledged. * This method should only be called if the write was acknowledged.
* *
* @see InsertOneResult::isAcknowledged() * @see InsertOneResult::isAcknowledged()
* @return integer * @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getInsertedCount() public function getInsertedCount()
{ {
return $this->writeResult->getInsertedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getInsertedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
...@@ -57,6 +65,10 @@ class InsertOneResult ...@@ -57,6 +65,10 @@ class InsertOneResult
* If the insert was not acknowledged, other fields from the WriteResult * If the insert was not acknowledged, other fields from the WriteResult
* (e.g. insertedCount) will be undefined. * (e.g. insertedCount) will be undefined.
* *
* If the insert was not acknowledged, other fields from the WriteResult
* (e.g. insertedCount) will be undefined and their getter methods should
* not be invoked.
*
* @return boolean * @return boolean
*/ */
public function isAcknowledged() public function isAcknowledged()
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace MongoDB; namespace MongoDB;
use MongoDB\Driver\WriteResult; use MongoDB\Driver\WriteResult;
use MongoDB\Exception\BadMethodCallException;
/** /**
* Result class for an update operation. * Result class for an update operation.
...@@ -10,6 +11,7 @@ use MongoDB\Driver\WriteResult; ...@@ -10,6 +11,7 @@ use MongoDB\Driver\WriteResult;
class UpdateResult class UpdateResult
{ {
private $writeResult; private $writeResult;
private $isAcknowledged;
/** /**
* Constructor. * Constructor.
...@@ -19,72 +21,101 @@ class UpdateResult ...@@ -19,72 +21,101 @@ class UpdateResult
public function __construct(WriteResult $writeResult) public function __construct(WriteResult $writeResult)
{ {
$this->writeResult = $writeResult; $this->writeResult = $writeResult;
$this->isAcknowledged = $writeResult->isAcknowledged();
} }
/** /**
* Return the number of documents that were matched by the filter. * Return the number of documents that were matched by the filter.
* *
* This value is undefined if the write was not acknowledged. * This method should only be called if the write was acknowledged.
* *
* @see UpdateResult::isAcknowledged() * @see UpdateResult::isAcknowledged()
* @return integer * @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getMatchedCount() public function getMatchedCount()
{ {
return $this->writeResult->getMatchedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getMatchedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
* Return the number of documents that were modified. * Return the number of documents that were modified.
* *
* This value is undefined if the write was not acknowledged or if the write * This value is undefined (i.e. null) if the write executed as a legacy
* executed as a legacy operation instead of write command. * operation instead of command.
*
* This method should only be called if the write was acknowledged.
* *
* @see UpdateResult::isAcknowledged() * @see UpdateResult::isAcknowledged()
* @return integer * @return integer|null
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getModifiedCount() public function getModifiedCount()
{ {
return $this->writeResult->getModifiedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getModifiedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
* Return the number of documents that were upserted. * Return the number of documents that were upserted.
* *
* This value is undefined if the write was not acknowledged. * This method should only be called if the write was acknowledged.
* *
* @see UpdateResult::isAcknowledged() * @see UpdateResult::isAcknowledged()
* @return integer * @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getUpsertedCount() public function getUpsertedCount()
{ {
return $this->writeResult->getUpsertedCount(); if ($this->isAcknowledged) {
return $this->writeResult->getUpsertedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
* Return the ID of the document inserted by an upsert operation. * Return the ID of the document inserted by an upsert operation.
* *
* This value is undefined if an upsert did not take place. * This value is undefined (i.e. null) if an upsert did not take place.
*
* This method should only be called if the write was acknowledged.
* *
* @see UpdateResult::isAcknowledged()
* @return mixed|null * @return mixed|null
* @throws BadMethodCallException is the write result is unacknowledged
*/ */
public function getUpsertedId() public function getUpsertedId()
{ {
foreach ($this->writeResult->getUpsertedIds() as $id) { if ($this->isAcknowledged) {
return $id; foreach ($this->writeResult->getUpsertedIds() as $id) {
return $id;
}
return null;
} }
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
} }
/** /**
* Return whether this update was acknowledged by the server. * Return whether this update was acknowledged by the server.
* *
* If the update was not acknowledged, other fields from the WriteResult * If the update was not acknowledged, other fields from the WriteResult
* (e.g. matchedCount) will be undefined. * (e.g. matchedCount) will be undefined and their getter methods should not
* be invoked.
* *
* @return boolean * @return boolean
*/ */
public function isAcknowledged() public function isAcknowledged()
{ {
return $this->writeResult->isAcknowledged(); return $this->isAcknowledged;
} }
} }
...@@ -2,7 +2,10 @@ ...@@ -2,7 +2,10 @@
namespace MongoDB\Tests\Collection; namespace MongoDB\Tests\Collection;
use MongoDB\Driver\BulkWrite; use MongoDB\BulkWriteResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\BulkWrite;
class BulkWriteFunctionalTest extends FunctionalTestCase class BulkWriteFunctionalTest extends FunctionalTestCase
{ {
...@@ -22,7 +25,9 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -22,7 +25,9 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
['insertOne' => [['x' => 22]]], ['insertOne' => [['x' => 22]]],
]; ];
$result = $this->collection->bulkWrite($ops); $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\BulkWriteResult', $result); $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
$this->assertSame(2, $result->getInsertedCount()); $this->assertSame(2, $result->getInsertedCount());
...@@ -50,7 +55,9 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -50,7 +55,9 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
['updateMany' => [['x' => ['$gt' => 50]], ['$inc' => ['x' => 1]]]], ['updateMany' => [['x' => ['$gt' => 50]], ['$inc' => ['x' => 1]]]],
]; ];
$result = $this->collection->bulkWrite($ops); $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\BulkWriteResult', $result); $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
$this->assertSame(5, $result->getMatchedCount()); $this->assertSame(5, $result->getMatchedCount());
$this->omitModifiedCount or $this->assertSame(5, $result->getModifiedCount()); $this->omitModifiedCount or $this->assertSame(5, $result->getModifiedCount());
...@@ -81,7 +88,9 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -81,7 +88,9 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
['deleteMany' => [['_id' => ['$gt' => 2]]]], ['deleteMany' => [['_id' => ['$gt' => 2]]]],
]; ];
$result = $this->collection->bulkWrite($ops); $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\BulkWriteResult', $result); $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
$this->assertSame(3, $result->getDeletedCount()); $this->assertSame(3, $result->getDeletedCount());
...@@ -104,7 +113,9 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -104,7 +113,9 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
['replaceOne' => [['_id' => 4], ['_id' => 4, 'x' => 44], ['upsert' => true]]], ['replaceOne' => [['_id' => 4], ['_id' => 4, 'x' => 44], ['upsert' => true]]],
]; ];
$result = $this->collection->bulkWrite($ops); $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\BulkWriteResult', $result); $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
$this->assertSame(1, $result->getInsertedCount()); $this->assertSame(1, $result->getInsertedCount());
...@@ -126,13 +137,85 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -126,13 +137,85 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $this->collection->find()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testUnacknowledgedWriteConcern()
{
$ops = [['insertOne' => [['_id' => 1]]]];
$options = ['writeConcern' => new WriteConcern(0)];
$operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops, $options);
$result = $operation->execute($this->getPrimaryServer());
$this->assertFalse($result->isAcknowledged());
return $result;
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesDeletedCount(BulkWriteResult $result)
{
$result->getDeletedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesInsertCount(BulkWriteResult $result)
{
$result->getInsertedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesMatchedCount(BulkWriteResult $result)
{
$result->getMatchedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesModifiedCount(BulkWriteResult $result)
{
$result->getModifiedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesUpsertedCount(BulkWriteResult $result)
{
$result->getUpsertedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesUpsertedIds(BulkWriteResult $result)
{
$result->getUpsertedIds();
}
/** /**
* @expectedException MongoDB\Exception\InvalidArgumentException * @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Unknown operation type "foo" in $operations[0] * @expectedExceptionMessage Unknown operation type "foo" in $operations[0]
*/ */
public function testUnknownOperation() public function testUnknownOperation()
{ {
$this->collection->bulkWrite([ new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
['foo' => [['_id' => 1]]], ['foo' => [['_id' => 1]]],
]); ]);
} }
...@@ -144,7 +227,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -144,7 +227,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
*/ */
public function testMissingArguments(array $ops) public function testMissingArguments(array $ops)
{ {
$this->collection->bulkWrite($ops); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
} }
public function provideOpsWithMissingArguments() public function provideOpsWithMissingArguments()
...@@ -168,7 +251,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -168,7 +251,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
*/ */
public function testUpdateOneRequiresUpdateOperators() public function testUpdateOneRequiresUpdateOperators()
{ {
$this->collection->bulkWrite([ new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
['updateOne' => [['_id' => 1], ['x' => 1]]], ['updateOne' => [['_id' => 1], ['x' => 1]]],
]); ]);
} }
...@@ -179,7 +262,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -179,7 +262,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
*/ */
public function testUpdateManyRequiresUpdateOperators() public function testUpdateManyRequiresUpdateOperators()
{ {
$this->collection->bulkWrite([ new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
['updateMany' => [['_id' => ['$gt' => 1]], ['x' => 1]]], ['updateMany' => [['_id' => ['$gt' => 1]], ['x' => 1]]],
]); ]);
} }
...@@ -190,7 +273,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -190,7 +273,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
*/ */
public function testReplaceOneRequiresReplacementDocument() public function testReplaceOneRequiresReplacementDocument()
{ {
$this->collection->bulkWrite([ new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
['replaceOne' => [['_id' => 1], ['$inc' => ['x' => 1]]]], ['replaceOne' => [['_id' => 1], ['$inc' => ['x' => 1]]]],
]); ]);
} }
...@@ -202,7 +285,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -202,7 +285,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
*/ */
private function createFixtures($n) private function createFixtures($n)
{ {
$bulkWrite = new BulkWrite(['ordered' => true]); $bulkWrite = new Bulk(['ordered' => true]);
for ($i = 1; $i <= $n; $i++) { for ($i = 1; $i <= $n; $i++) {
$bulkWrite->insert([ $bulkWrite->insert([
...@@ -215,4 +298,4 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -215,4 +298,4 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
$this->assertEquals($n, $result->getInsertedCount()); $this->assertEquals($n, $result->getInsertedCount());
} }
} }
\ No newline at end of file
<?php
namespace MongoDB\Tests\Collection;
use MongoDB\DeleteResult;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\Delete;
class DeleteFunctionalTest extends FunctionalTestCase
{
public function testDeleteOne()
{
$this->createFixtures(3);
$filter = ['_id' => 1];
$operation = new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 1);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\DeleteResult', $result);
$this->assertSame(1, $result->getDeletedCount());
$expected = [
['_id' => 2, 'x' => 22],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testDeleteMany()
{
$this->createFixtures(3);
$filter = ['_id' => ['$gt' => 1]];
$operation = new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 0);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\DeleteResult', $result);
$this->assertSame(2, $result->getDeletedCount());
$expected = [
['_id' => 1, 'x' => 11],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testUnacknowledgedWriteConcern()
{
$filter = ['_id' => 1];
$options = ['writeConcern' => new WriteConcern(0)];
$operation = new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 0, $options);
$result = $operation->execute($this->getPrimaryServer());
$this->assertFalse($result->isAcknowledged());
return $result;
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesDeletedCount(DeleteResult $result)
{
$result->getDeletedCount();
}
/**
* Create data fixtures.
*
* @param integer $n
*/
private function createFixtures($n)
{
$bulkWrite = new BulkWrite(['ordered' => true]);
for ($i = 1; $i <= $n; $i++) {
$bulkWrite->insert([
'_id' => $i,
'x' => (integer) ($i . $i),
]);
}
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
$this->assertEquals($n, $result->getInsertedCount());
}
}
<?php
namespace MongoDB\Tests\Collection;
use MongoDB\InsertManyResult;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\InsertMany;
class InsertManyFunctionalTest extends FunctionalTestCase
{
public function testInsertMany()
{
$documents = [
['_id' => 'foo', 'x' => 11],
['x' => 22],
['_id' => 'bar', 'x' => 22],
];
$operation = new InsertMany($this->getDatabaseName(), $this->getCollectionName(), $documents);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\InsertManyResult', $result);
$this->assertSame(3, $result->getInsertedCount());
$insertedIds = $result->getInsertedIds();
$this->assertSame('foo', $insertedIds[0]);
$this->assertInstanceOf('MongoDB\BSON\ObjectId', $insertedIds[1]);
$this->assertSame('bar', $insertedIds[2]);
$expected = [
['_id' => 'foo', 'x' => 11],
['_id' => $insertedIds[1], 'x' => 22],
['_id' => 'bar', 'x' => 22],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testUnacknowledgedWriteConcern()
{
$documents = [['x' => 11]];
$options = ['writeConcern' => new WriteConcern(0)];
$operation = new InsertMany($this->getDatabaseName(), $this->getCollectionName(), $documents, $options);
$result = $operation->execute($this->getPrimaryServer());
$this->assertFalse($result->isAcknowledged());
return $result;
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesInsertedCount(InsertManyResult $result)
{
$result->getInsertedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
*/
public function testUnacknowledgedWriteConcernAccessesInsertedId(InsertManyResult $result)
{
$this->assertInstanceOf('MongoDB\BSON\ObjectId', $result->getInsertedIds()[0]);
}
}
<?php
namespace MongoDB\Tests\Collection;
use MongoDB\InsertOneResult;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\InsertOne;
class InsertOneFunctionalTest extends FunctionalTestCase
{
public function testInsertOneWithExistingId()
{
$document = ['_id' => 'foo', 'x' => 11];
$operation = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
$this->assertSame(1, $result->getInsertedCount());
$this->assertSame('foo', $result->getInsertedId());
$expected = [
['_id' => 'foo', 'x' => 11],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testInsertOneWithGeneratedId()
{
$document = ['x' => 11];
$operation = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
$this->assertSame(1, $result->getInsertedCount());
$this->assertInstanceOf('MongoDB\BSON\ObjectId', $result->getInsertedId());
$expected = [
['_id' => $result->getInsertedId(), 'x' => 11],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testUnacknowledgedWriteConcern()
{
$document = ['x' => 11];
$options = ['writeConcern' => new WriteConcern(0)];
$operation = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document, $options);
$result = $operation->execute($this->getPrimaryServer());
$this->assertFalse($result->isAcknowledged());
return $result;
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesInsertedCount(InsertOneResult $result)
{
$result->getInsertedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
*/
public function testUnacknowledgedWriteConcernAccessesInsertedId(InsertOneResult $result)
{
$this->assertInstanceOf('MongoDB\BSON\ObjectId', $result->getInsertedId());
}
}
<?php
namespace MongoDB\Tests\Collection;
use MongoDB\UpdateResult;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\Update;
class UpdateFunctionalTest extends FunctionalTestCase
{
private $omitModifiedCount;
public function setUp()
{
parent::setUp();
$this->omitModifiedCount = version_compare($this->getServerVersion(), '2.6.0', '<');
}
public function testUpdateOne()
{
$this->createFixtures(3);
$filter = ['_id' => ['$gt' => 1]];
$update = ['$inc' => ['x' => 1]];
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\UpdateResult', $result);
$this->assertSame(1, $result->getMatchedCount());
$this->omitModifiedCount or $this->assertSame(1, $result->getModifiedCount());
$this->assertSame(0, $result->getUpsertedCount());
$this->assertNull($result->getUpsertedId());
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 23],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testUpdateMany()
{
$this->createFixtures(3);
$filter = ['_id' => ['$gt' => 1]];
$update = ['$inc' => ['x' => 1]];
$options = ['multi' => true];
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\UpdateResult', $result);
$this->assertSame(2, $result->getMatchedCount());
$this->omitModifiedCount or $this->assertSame(2, $result->getModifiedCount());
$this->assertSame(0, $result->getUpsertedCount());
$this->assertNull($result->getUpsertedId());
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 23],
['_id' => 3, 'x' => 34],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testUpdateManyWithExistingId()
{
$this->createFixtures(3);
$filter = ['_id' => 5];
$update = ['$set' => ['x' => 55]];
$options = ['upsert' => true];
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\UpdateResult', $result);
$this->assertSame(0, $result->getMatchedCount());
$this->omitModifiedCount or $this->assertSame(0, $result->getModifiedCount());
$this->assertSame(1, $result->getUpsertedCount());
$this->assertSame(5, $result->getUpsertedId());
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 22],
['_id' => 3, 'x' => 33],
['_id' => 5, 'x' => 55],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testUpdateManyWithGeneratedId()
{
$this->createFixtures(3);
$filter = ['x' => 66];
$update = ['$set' => ['x' => 66]];
$options = ['upsert' => true];
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\UpdateResult', $result);
$this->assertSame(0, $result->getMatchedCount());
$this->omitModifiedCount or $this->assertSame(0, $result->getModifiedCount());
$this->assertSame(1, $result->getUpsertedCount());
$this->assertInstanceOf('MongoDB\BSON\ObjectId', $result->getUpsertedId());
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 22],
['_id' => 3, 'x' => 33],
['_id' => $result->getUpsertedId(), 'x' => 66],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testUnacknowledgedWriteConcern()
{
$filter = ['_id' => 1];
$update = ['$set' => ['x' => 1]];
$options = ['writeConcern' => new WriteConcern(0)];
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options);
$result = $operation->execute($this->getPrimaryServer());
$this->assertFalse($result->isAcknowledged());
return $result;
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesMatchedCount(UpdateResult $result)
{
$result->getMatchedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesModifiedCount(UpdateResult $result)
{
$result->getModifiedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesUpsertedCount(UpdateResult $result)
{
$result->getUpsertedCount();
}
/**
* @depends testUnacknowledgedWriteConcern
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
*/
public function testUnacknowledgedWriteConcernAccessesUpsertedId(UpdateResult $result)
{
$result->getUpsertedId();
}
/**
* Create data fixtures.
*
* @param integer $n
*/
private function createFixtures($n)
{
$bulkWrite = new BulkWrite(['ordered' => true]);
for ($i = 1; $i <= $n; $i++) {
$bulkWrite->insert([
'_id' => $i,
'x' => (integer) ($i . $i),
]);
}
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
$this->assertEquals($n, $result->getInsertedCount());
}
}
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