Skip to content

Commit efbebe7

Browse files
committed
Add tests to improve coverage of untested or under-tested APIs
- Session::getServer() — no test existed; documents that the method returns null when the session is not pinned to a server (pinning was removed for sharded clusters in MongoDB 6.0+) - Session::startTransaction() with all options combined — each option (maxCommitTimeMS, readConcern, readPreference, writeConcern) was only tested in isolation; this test verifies they coexist correctly and are all reflected by getTransactionOptions() - ExecutionTimeoutException::hasErrorLabel() — hasErrorLabel() was tested on RuntimeException and BulkWriteException but not on this subclass - ConnectionTimeoutException::hasErrorLabel() — same gap as above - BulkWriteCommandException::getWriteConcernErrors() — the method appeared in debug output but was never explicitly asserted; this test verifies it returns an empty array when only write errors occur - Query construction with maxAwaitTimeMS — only error/range validation tests existed; this test verifies valid values are accepted - BulkWriteCommand constructor with multiple options combined — each option (ordered, comment, verboseResults) was tested in isolation; this test verifies they work together - Command construction with invalid maxAwaitTimeMS — the >= 0 validation existed in Command.c but had no corresponding test
1 parent 3fe80e4 commit efbebe7

8 files changed

Lines changed: 239 additions & 0 deletions
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::__construct() multiple options combined
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_server_version('<', '8.0'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
class CommandLogger implements MongoDB\Driver\Monitoring\CommandSubscriber
14+
{
15+
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event): void
16+
{
17+
if ($event->getCommandName() !== 'bulkWrite') {
18+
return;
19+
}
20+
21+
$command = $event->getCommand();
22+
23+
printf("ordered: %s\n", var_export((bool) $command->ordered, true));
24+
printf("comment: %s\n", json_encode($command->comment));
25+
}
26+
27+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event): void
28+
{
29+
}
30+
31+
public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event): void
32+
{
33+
}
34+
}
35+
36+
$manager = create_test_manager();
37+
38+
$bulk = new MongoDB\Driver\BulkWriteCommand([
39+
'ordered' => false,
40+
'comment' => 'test comment',
41+
'verboseResults' => true,
42+
]);
43+
$bulk->insertOne(NS, ['_id' => 1]);
44+
$bulk->insertOne(NS, ['_id' => 2]);
45+
46+
$manager->addSubscriber(new CommandLogger);
47+
$result = $manager->executeBulkWriteCommand($bulk);
48+
49+
var_dump($result->getInsertedCount());
50+
var_dump($result->getInsertResults() !== null);
51+
52+
?>
53+
===DONE===
54+
<?php exit(0); ?>
55+
--EXPECT--
56+
ordered: false
57+
comment: "test comment"
58+
int(2)
59+
bool(true)
60+
===DONE===
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\Driver\Command construction (invalid maxAwaitTimeMS range)
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
echo throws(function() {
9+
new MongoDB\Driver\Command(['ping' => 1], ['maxAwaitTimeMS' => -1]);
10+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
11+
12+
?>
13+
===DONE===
14+
<?php exit(0); ?>
15+
--EXPECT--
16+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
17+
Expected "maxAwaitTimeMS" option to be >= 0, -1 given
18+
===DONE===
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
MongoDB\Driver\Exception\BulkWriteCommandException::getWriteConcernErrors()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_server_version('<', '8.0'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
$manager = create_test_manager();
14+
15+
/* Trigger a write error (duplicate key) with no write concern error */
16+
$bulk = new MongoDB\Driver\BulkWriteCommand(['ordered' => false]);
17+
$bulk->insertOne(NS, ['_id' => 1]);
18+
$bulk->insertOne(NS, ['_id' => 1]);
19+
20+
try {
21+
$manager->executeBulkWriteCommand($bulk);
22+
} catch (MongoDB\Driver\Exception\BulkWriteCommandException $e) {
23+
var_dump($e->getWriteConcernErrors());
24+
}
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECT--
30+
array(0) {
31+
}
32+
===DONE===
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
MongoDB\Driver\Exception\ConnectionTimeoutException::hasErrorLabel()
3+
--FILE--
4+
<?php
5+
6+
$exception = new MongoDB\Driver\Exception\ConnectionTimeoutException();
7+
$labels = ['test', 'foo'];
8+
9+
$reflection = new ReflectionClass($exception);
10+
$errorLabelsProperty = $reflection->getProperty('errorLabels');
11+
$errorLabelsProperty->setValue($exception, $labels);
12+
13+
var_dump($exception->hasErrorLabel('foo'));
14+
var_dump($exception->hasErrorLabel('bar'));
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECT--
20+
bool(true)
21+
bool(false)
22+
===DONE===
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
MongoDB\Driver\Exception\ExecutionTimeoutException::hasErrorLabel()
3+
--FILE--
4+
<?php
5+
6+
$exception = new MongoDB\Driver\Exception\ExecutionTimeoutException();
7+
$labels = ['test', 'foo'];
8+
9+
$reflection = new ReflectionClass($exception);
10+
$errorLabelsProperty = $reflection->getProperty('errorLabels');
11+
$errorLabelsProperty->setValue($exception, $labels);
12+
13+
var_dump($exception->hasErrorLabel('foo'));
14+
var_dump($exception->hasErrorLabel('bar'));
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECT--
20+
bool(true)
21+
bool(false)
22+
===DONE===
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
MongoDB\Driver\Query construction with maxAwaitTimeMS option
3+
--FILE--
4+
<?php
5+
6+
$q = new MongoDB\Driver\Query([], ['maxAwaitTimeMS' => 0]);
7+
echo "maxAwaitTimeMS=0: OK\n";
8+
9+
$q = new MongoDB\Driver\Query([], ['maxAwaitTimeMS' => 1000]);
10+
echo "maxAwaitTimeMS=1000: OK\n";
11+
12+
$q = new MongoDB\Driver\Query([], ['maxAwaitTimeMS' => 4294967295]);
13+
echo "maxAwaitTimeMS=4294967295: OK\n";
14+
15+
?>
16+
===DONE===
17+
<?php exit(0); ?>
18+
--EXPECT--
19+
maxAwaitTimeMS=0: OK
20+
maxAwaitTimeMS=1000: OK
21+
maxAwaitTimeMS=4294967295: OK
22+
===DONE===
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
MongoDB\Driver\Session::getServer()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto(); ?>
6+
<?php skip_if_not_live(); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = create_test_manager();
12+
$session = $manager->startSession();
13+
14+
/* Session is not pinned to a server until a transaction operation is executed
15+
* on a sharded cluster. Returns null when the session is not pinned. */
16+
var_dump($session->getServer());
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECT--
22+
NULL
23+
===DONE===
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
MongoDB\Driver\Session::startTransaction() with all valid options combined
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto(); ?>
6+
<?php skip_if_no_transactions(); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = create_test_manager();
12+
$session = $manager->startSession();
13+
14+
$session->startTransaction([
15+
'maxCommitTimeMS' => 5000,
16+
'readConcern' => new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::MAJORITY),
17+
'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY),
18+
'writeConcern' => new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY),
19+
]);
20+
21+
$options = $session->getTransactionOptions();
22+
23+
var_dump(isset($options['maxCommitTimeMS']));
24+
var_dump($options['maxCommitTimeMS']);
25+
var_dump($options['readConcern']->getLevel());
26+
var_dump($options['readPreference']->getModeString());
27+
var_dump($options['writeConcern']->getW());
28+
29+
$session->abortTransaction();
30+
31+
?>
32+
===DONE===
33+
<?php exit(0); ?>
34+
--EXPECT--
35+
bool(true)
36+
int(5000)
37+
string(8) "majority"
38+
string(7) "primary"
39+
string(8) "majority"
40+
===DONE===

0 commit comments

Comments
 (0)