Commit 5c266962 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-68: Throw when accessing fields in unacknowledged write result

parent 42683dc5
...@@ -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,9 @@ ...@@ -2,7 +2,9 @@
namespace MongoDB\Tests\Collection; namespace MongoDB\Tests\Collection;
use MongoDB\BulkWriteResult;
use MongoDB\Driver\BulkWrite as Bulk; use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\BulkWrite; use MongoDB\Operation\BulkWrite;
class BulkWriteFunctionalTest extends FunctionalTestCase class BulkWriteFunctionalTest extends FunctionalTestCase
...@@ -135,6 +137,78 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -135,6 +137,78 @@ 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]
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
namespace MongoDB\Tests\Collection; namespace MongoDB\Tests\Collection;
use MongoDB\DeleteResult;
use MongoDB\Driver\BulkWrite; use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\Delete; use MongoDB\Operation\Delete;
class DeleteFunctionalTest extends FunctionalTestCase class DeleteFunctionalTest extends FunctionalTestCase
...@@ -46,6 +48,29 @@ class DeleteFunctionalTest extends FunctionalTestCase ...@@ -46,6 +48,29 @@ class DeleteFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $this->collection->find()); $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. * Create data fixtures.
* *
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace MongoDB\Tests\Collection; namespace MongoDB\Tests\Collection;
use MongoDB\InsertManyResult;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\InsertMany; use MongoDB\Operation\InsertMany;
class InsertManyFunctionalTest extends FunctionalTestCase class InsertManyFunctionalTest extends FunctionalTestCase
...@@ -33,4 +35,35 @@ class InsertManyFunctionalTest extends FunctionalTestCase ...@@ -33,4 +35,35 @@ class InsertManyFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $this->collection->find()); $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]);
}
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace MongoDB\Tests\Collection; namespace MongoDB\Tests\Collection;
use MongoDB\InsertOneResult;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\InsertOne; use MongoDB\Operation\InsertOne;
class InsertOneFunctionalTest extends FunctionalTestCase class InsertOneFunctionalTest extends FunctionalTestCase
...@@ -41,4 +43,35 @@ class InsertOneFunctionalTest extends FunctionalTestCase ...@@ -41,4 +43,35 @@ class InsertOneFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $this->collection->find()); $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());
}
} }
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
namespace MongoDB\Tests\Collection; namespace MongoDB\Tests\Collection;
use MongoDB\UpdateResult;
use MongoDB\Driver\BulkWrite; use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\Update; use MongoDB\Operation\Update;
class UpdateFunctionalTest extends FunctionalTestCase class UpdateFunctionalTest extends FunctionalTestCase
...@@ -121,6 +123,59 @@ class UpdateFunctionalTest extends FunctionalTestCase ...@@ -121,6 +123,59 @@ class UpdateFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $this->collection->find()); $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. * Create data fixtures.
* *
......
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