Skip to content

Commit 777a643

Browse files
authored
PHPC-2144 Throw a LogicException when getting info from unacknowledged write result (#1687)
Remove nullable from WriteResult get*Count accessors
1 parent 4d37f3a commit 777a643

13 files changed

+60
-56
lines changed

src/MongoDB/WriteResult.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
#include "MongoDB/WriteError.h"
3232
#include "WriteResult_arginfo.h"
3333

34-
#define PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED(method) \
35-
if (!mongoc_write_concern_is_acknowledged(intern->write_concern)) { \
36-
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"); \
37-
RETURN_NULL(); \
34+
#define PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED(method) \
35+
if (!mongoc_write_concern_is_acknowledged(intern->write_concern)) { \
36+
phongo_throw_exception(PHONGO_ERROR_LOGIC, "MongoDB\\Driver\\WriteResult::" method "() should not be called for an unacknowledged write result"); \
3837
}
3938

4039
#define PHONGO_WRITERESULT_RETURN_LONG_FROM_BSON_INT32(iter, bson, key) \

src/MongoDB/WriteResult.stub.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ final class WriteResult
1212
{
1313
final private function __construct() {}
1414

15-
final public function getInsertedCount(): ?int {}
15+
final public function getInsertedCount(): int {}
1616

17-
final public function getMatchedCount(): ?int {}
17+
final public function getMatchedCount(): int {}
1818

19-
final public function getModifiedCount(): ?int {}
19+
final public function getModifiedCount(): int {}
2020

21-
final public function getDeletedCount(): ?int {}
21+
final public function getDeletedCount(): int {}
2222

23-
final public function getUpsertedCount(): ?int {}
23+
final public function getUpsertedCount(): int {}
2424

2525
final public function getServer(): Server {}
2626

src/MongoDB/WriteResult_arginfo.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/manager/manager-executeBulkWrite-012.phpt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ foreach ($writeConcerns as $wc) {
2323

2424
$result = $manager->executeBulkWrite(NS, $bulk, $options);
2525
var_dump($result->isAcknowledged());
26-
var_dump($result->getInsertedCount());
26+
if ($result->isAcknowledged()) {
27+
var_dump($result->getInsertedCount());
28+
}
2729
}
2830

2931
?>
3032
===DONE===
3133
<?php exit(0); ?>
32-
--EXPECTF--
34+
--EXPECT--
3335
bool(false)
34-
35-
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
36-
NULL
3736
bool(true)
3837
int(1)
3938
bool(true)

tests/server/server-executeBulkWrite-002.phpt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ foreach ($writeConcerns as $writeConcern) {
1919

2020
$result = $primary->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern($writeConcern));
2121
var_dump($result->isAcknowledged());
22-
var_dump($result->getInsertedCount());
22+
if ($result->isAcknowledged()) {
23+
var_dump($result->getInsertedCount());
24+
}
2325
}
2426

2527
?>
2628
===DONE===
2729
<?php exit(0); ?>
28-
--EXPECTF--
30+
--EXPECT--
2931
bool(false)
30-
31-
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
32-
NULL
3332
bool(true)
3433
int(1)
3534
===DONE===

tests/server/server-executeBulkWrite-003.phpt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ foreach ($writeConcerns as $wc) {
2020

2121
$result = $server->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern($wc));
2222
var_dump($result->isAcknowledged());
23-
var_dump($result->getInsertedCount());
23+
if ($result->isAcknowledged()) {
24+
var_dump($result->getInsertedCount());
25+
}
2426
}
2527

2628
?>
2729
===DONE===
2830
<?php exit(0); ?>
29-
--EXPECTF--
31+
--EXPECT--
3032
bool(false)
31-
32-
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
33-
NULL
3433
bool(true)
3534
int(1)
3635
bool(true)

tests/server/server-executeBulkWrite-005.phpt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ foreach ($writeConcerns as $wc) {
2525

2626
$result = $server->executeBulkWrite('local.' . COLLECTION_NAME, $bulk, new MongoDB\Driver\WriteConcern($wc));
2727
var_dump($result->isAcknowledged());
28-
var_dump($result->getInsertedCount());
28+
if ($result->isAcknowledged()) {
29+
var_dump($result->getInsertedCount());
30+
}
2931
}
3032

3133
$bulk = new MongoDB\Driver\BulkWrite();
@@ -35,11 +37,8 @@ $server->executeBulkWrite('local.' . COLLECTION_NAME, $bulk);
3537
?>
3638
===DONE===
3739
<?php exit(0); ?>
38-
--EXPECTF--
40+
--EXPECT--
3941
bool(false)
40-
41-
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
42-
NULL
4342
bool(true)
4443
int(1)
4544
===DONE===

tests/server/server-executeBulkWrite-006.phpt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ foreach ($writeConcerns as $wc) {
2424

2525
$result = $server->executeBulkWrite(NS, $bulk, $options);
2626
var_dump($result->isAcknowledged());
27-
var_dump($result->getInsertedCount());
27+
if ($result->isAcknowledged()) {
28+
var_dump($result->getInsertedCount());
29+
}
2830
}
2931

3032
?>
3133
===DONE===
3234
<?php exit(0); ?>
33-
--EXPECTF--
35+
--EXPECT--
3436
bool(false)
35-
36-
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
37-
NULL
3837
bool(true)
3938
int(1)
4039
bool(true)

tests/writeResult/writeresult-getdeletedcount-002.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);
1919

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

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

2426
?>
2527
===DONE===
2628
<?php exit(0); ?>
27-
--EXPECTF--
28-
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
29-
NULL
29+
--EXPECT--
30+
OK: Got MongoDB\Driver\Exception\LogicException
31+
MongoDB\Driver\WriteResult::getDeletedCount() should not be called for an unacknowledged write result
3032
===DONE===

tests/writeResult/writeresult-getinsertedcount-002.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);
1919

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

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

2426
?>
2527
===DONE===
2628
<?php exit(0); ?>
27-
--EXPECTF--
28-
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
29-
NULL
29+
--EXPECT--
30+
OK: Got MongoDB\Driver\Exception\LogicException
31+
MongoDB\Driver\WriteResult::getInsertedCount() should not be called for an unacknowledged write result
3032
===DONE===

tests/writeResult/writeresult-getmatchedcount-002.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);
1919

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

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

2426
?>
2527
===DONE===
2628
<?php exit(0); ?>
27-
--EXPECTF--
28-
Deprecated: MongoDB\Driver\WriteResult::getMatchedCount(): Calling MongoDB\Driver\WriteResult::getMatchedCount() for an unacknowledged write is deprecated and will throw an exception in %s
29-
NULL
29+
--EXPECT--
30+
OK: Got MongoDB\Driver\Exception\LogicException
31+
MongoDB\Driver\WriteResult::getMatchedCount() should not be called for an unacknowledged write result
3032
===DONE===

tests/writeResult/writeresult-getmodifiedcount-002.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);
1919

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

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

2426
?>
2527
===DONE===
2628
<?php exit(0); ?>
27-
--EXPECTF--
28-
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
29-
NULL
29+
--EXPECT--
30+
OK: Got MongoDB\Driver\Exception\LogicException
31+
MongoDB\Driver\WriteResult::getModifiedCount() should not be called for an unacknowledged write result
3032
===DONE===

tests/writeResult/writeresult-getupsertedcount-002.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ $bulk->delete(['x' => 1]);
1919

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

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

2426
?>
2527
===DONE===
2628
<?php exit(0); ?>
27-
--EXPECTF--
28-
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
29-
NULL
29+
--EXPECT--
30+
OK: Got MongoDB\Driver\Exception\LogicException
31+
MongoDB\Driver\WriteResult::getUpsertedCount() should not be called for an unacknowledged write result
3032
===DONE===

0 commit comments

Comments
 (0)