Skip to content

Commit

Permalink
PHPC-2144 Throw a LogicException when getting info from unacknowledge…
Browse files Browse the repository at this point in the history
…d write result (#1687)

Remove nullable from WriteResult get*Count accessors
  • Loading branch information
GromNaN authored Sep 24, 2024
1 parent 4d37f3a commit 777a643
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 56 deletions.
7 changes: 3 additions & 4 deletions src/MongoDB/WriteResult.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
#include "MongoDB/WriteError.h"
#include "WriteResult_arginfo.h"

#define PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED(method) \
if (!mongoc_write_concern_is_acknowledged(intern->write_concern)) { \
php_error_docref(NULL, E_DEPRECATED, "Calling MongoDB\\Driver\\WriteResult::" method "() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0"); \
RETURN_NULL(); \
#define PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED(method) \
if (!mongoc_write_concern_is_acknowledged(intern->write_concern)) { \
phongo_throw_exception(PHONGO_ERROR_LOGIC, "MongoDB\\Driver\\WriteResult::" method "() should not be called for an unacknowledged write result"); \
}

#define PHONGO_WRITERESULT_RETURN_LONG_FROM_BSON_INT32(iter, bson, key) \
Expand Down
10 changes: 5 additions & 5 deletions src/MongoDB/WriteResult.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ final class WriteResult
{
final private function __construct() {}

final public function getInsertedCount(): ?int {}
final public function getInsertedCount(): int {}

final public function getMatchedCount(): ?int {}
final public function getMatchedCount(): int {}

final public function getModifiedCount(): ?int {}
final public function getModifiedCount(): int {}

final public function getDeletedCount(): ?int {}
final public function getDeletedCount(): int {}

final public function getUpsertedCount(): ?int {}
final public function getUpsertedCount(): int {}

final public function getServer(): Server {}

Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB/WriteResult_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions tests/manager/manager-executeBulkWrite-012.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ foreach ($writeConcerns as $wc) {

$result = $manager->executeBulkWrite(NS, $bulk, $options);
var_dump($result->isAcknowledged());
var_dump($result->getInsertedCount());
if ($result->isAcknowledged()) {
var_dump($result->getInsertedCount());
}
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
--EXPECT--
bool(false)

Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
NULL
bool(true)
int(1)
bool(true)
Expand Down
9 changes: 4 additions & 5 deletions tests/server/server-executeBulkWrite-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ foreach ($writeConcerns as $writeConcern) {

$result = $primary->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern($writeConcern));
var_dump($result->isAcknowledged());
var_dump($result->getInsertedCount());
if ($result->isAcknowledged()) {
var_dump($result->getInsertedCount());
}
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
--EXPECT--
bool(false)

Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
NULL
bool(true)
int(1)
===DONE===
9 changes: 4 additions & 5 deletions tests/server/server-executeBulkWrite-003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ foreach ($writeConcerns as $wc) {

$result = $server->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern($wc));
var_dump($result->isAcknowledged());
var_dump($result->getInsertedCount());
if ($result->isAcknowledged()) {
var_dump($result->getInsertedCount());
}
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
--EXPECT--
bool(false)

Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
NULL
bool(true)
int(1)
bool(true)
Expand Down
9 changes: 4 additions & 5 deletions tests/server/server-executeBulkWrite-005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ foreach ($writeConcerns as $wc) {

$result = $server->executeBulkWrite('local.' . COLLECTION_NAME, $bulk, new MongoDB\Driver\WriteConcern($wc));
var_dump($result->isAcknowledged());
var_dump($result->getInsertedCount());
if ($result->isAcknowledged()) {
var_dump($result->getInsertedCount());
}
}

$bulk = new MongoDB\Driver\BulkWrite();
Expand All @@ -35,11 +37,8 @@ $server->executeBulkWrite('local.' . COLLECTION_NAME, $bulk);
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
--EXPECT--
bool(false)

Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
NULL
bool(true)
int(1)
===DONE===
9 changes: 4 additions & 5 deletions tests/server/server-executeBulkWrite-006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ foreach ($writeConcerns as $wc) {

$result = $server->executeBulkWrite(NS, $bulk, $options);
var_dump($result->isAcknowledged());
var_dump($result->getInsertedCount());
if ($result->isAcknowledged()) {
var_dump($result->getInsertedCount());
}
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
--EXPECT--
bool(false)

Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
NULL
bool(true)
int(1)
bool(true)
Expand Down
10 changes: 6 additions & 4 deletions tests/writeResult/writeresult-getdeletedcount-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);

$result = $manager->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern(0));

var_dump($result->getDeletedCount());
echo throws(function() use ($result) {
$result->getDeletedCount();
}, MongoDB\Driver\Exception\LogicException::class), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Deprecated: MongoDB\Driver\WriteResult::getDeletedCount(): Calling MongoDB\Driver\WriteResult::getDeletedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
NULL
--EXPECT--
OK: Got MongoDB\Driver\Exception\LogicException
MongoDB\Driver\WriteResult::getDeletedCount() should not be called for an unacknowledged write result
===DONE===
10 changes: 6 additions & 4 deletions tests/writeResult/writeresult-getinsertedcount-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);

$result = $manager->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern(0));

var_dump($result->getInsertedCount());
echo throws(function() use ($result) {
$result->getInsertedCount();
}, MongoDB\Driver\Exception\LogicException::class), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
NULL
--EXPECT--
OK: Got MongoDB\Driver\Exception\LogicException
MongoDB\Driver\WriteResult::getInsertedCount() should not be called for an unacknowledged write result
===DONE===
10 changes: 6 additions & 4 deletions tests/writeResult/writeresult-getmatchedcount-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);

$result = $manager->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern(0));

var_dump($result->getMatchedCount());
echo throws(function() use ($result) {
$result->getMatchedCount();
}, MongoDB\Driver\Exception\LogicException::class), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Deprecated: MongoDB\Driver\WriteResult::getMatchedCount(): Calling MongoDB\Driver\WriteResult::getMatchedCount() for an unacknowledged write is deprecated and will throw an exception in %s
NULL
--EXPECT--
OK: Got MongoDB\Driver\Exception\LogicException
MongoDB\Driver\WriteResult::getMatchedCount() should not be called for an unacknowledged write result
===DONE===
10 changes: 6 additions & 4 deletions tests/writeResult/writeresult-getmodifiedcount-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);

$result = $manager->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern(0));

var_dump($result->getModifiedCount());
echo throws(function() use ($result) {
$result->getModifiedCount();
}, MongoDB\Driver\Exception\LogicException::class), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Deprecated: MongoDB\Driver\WriteResult::getModifiedCount(): Calling MongoDB\Driver\WriteResult::getModifiedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
NULL
--EXPECT--
OK: Got MongoDB\Driver\Exception\LogicException
MongoDB\Driver\WriteResult::getModifiedCount() should not be called for an unacknowledged write result
===DONE===
10 changes: 6 additions & 4 deletions tests/writeResult/writeresult-getupsertedcount-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);

$result = $manager->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern(0));

var_dump($result->getUpsertedCount());
echo throws(function() use ($result) {
$result->getUpsertedCount();
}, MongoDB\Driver\Exception\LogicException::class), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Deprecated: MongoDB\Driver\WriteResult::getUpsertedCount(): Calling MongoDB\Driver\WriteResult::getUpsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
NULL
--EXPECT--
OK: Got MongoDB\Driver\Exception\LogicException
MongoDB\Driver\WriteResult::getUpsertedCount() should not be called for an unacknowledged write result
===DONE===

0 comments on commit 777a643

Please sign in to comment.