Skip to content

Commit 3a45310

Browse files
committed
wip BulkWriteCommand tests
1 parent 61912ed commit 3a45310

11 files changed

+895
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::__construct() bypassDocumentValidation=true
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+
$manager->executeWriteCommand(DATABASE_NAME, new MongoDB\Driver\Command([
16+
'create' => COLLECTION_NAME,
17+
'validator' => [
18+
'$jsonSchema' => [
19+
'bsonType' => 'object',
20+
'required' => ['x'],
21+
],
22+
],
23+
]));
24+
25+
$bulk = new MongoDB\Driver\BulkWriteCommand(['bypassDocumentValidation' => true]);
26+
$bulk->insertOne(NS, ['_id' => 1]);
27+
28+
$result = $manager->executeBulkWriteCommand($bulk);
29+
30+
var_dump($result);
31+
32+
?>
33+
===DONE===
34+
<?php exit(0); ?>
35+
--EXPECTF--
36+
object(MongoDB\Driver\BulkWriteCommandResult)#%d (%d) {
37+
["isAcknowledged"]=>
38+
bool(true)
39+
["insertedCount"]=>
40+
int(1)
41+
["matchedCount"]=>
42+
int(0)
43+
["modifiedCount"]=>
44+
int(0)
45+
["upsertedCount"]=>
46+
int(0)
47+
["deletedCount"]=>
48+
int(0)
49+
["insertResults"]=>
50+
NULL
51+
["updateResults"]=>
52+
NULL
53+
["deleteResults"]=>
54+
NULL
55+
["writeErrors"]=>
56+
array(0) {
57+
}
58+
["writeConcernErrors"]=>
59+
array(0) {
60+
}
61+
["errorReply"]=>
62+
NULL
63+
["server"]=>
64+
object(MongoDB\Driver\Server)#%d (%d) {%A
65+
}
66+
}
67+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::__construct() bypassDocumentValidation=false
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+
$manager->executeWriteCommand(DATABASE_NAME, new MongoDB\Driver\Command([
16+
'create' => COLLECTION_NAME,
17+
'validator' => [
18+
'$jsonSchema' => [
19+
'bsonType' => 'object',
20+
'required' => ['x'],
21+
],
22+
],
23+
]));
24+
25+
$bulk = new MongoDB\Driver\BulkWriteCommand(['bypassDocumentValidation' => false]);
26+
/* Include a successful write operation to ensure that mongoc_bulkwriteresult_t
27+
* is populated (CDRIVER-5856). */
28+
$bulk->insertOne(NS, ['_id' => 1, 'x' => 1]);
29+
$bulk->insertOne(NS, ['_id' => 2]);
30+
31+
try {
32+
$manager->executeBulkWriteCommand($bulk);
33+
} catch (MongoDB\Driver\Exception\BulkWriteCommandException $e) {
34+
printf("%s(%d): %s\n", get_class($e), $e->getCode(), $e->getMessage());
35+
var_dump($e->getBulkWriteCommandResult());
36+
}
37+
38+
?>
39+
===DONE===
40+
<?php exit(0); ?>
41+
--EXPECTF--
42+
MongoDB\Driver\Exception\BulkWriteCommandException(0): Bulk write failed
43+
object(MongoDB\Driver\BulkWriteCommandResult)#%d (%d) {
44+
["isAcknowledged"]=>
45+
bool(true)
46+
["insertedCount"]=>
47+
int(1)
48+
["matchedCount"]=>
49+
int(0)
50+
["modifiedCount"]=>
51+
int(0)
52+
["upsertedCount"]=>
53+
int(0)
54+
["deletedCount"]=>
55+
int(0)
56+
["insertResults"]=>
57+
NULL
58+
["updateResults"]=>
59+
NULL
60+
["deleteResults"]=>
61+
NULL
62+
["writeErrors"]=>
63+
array(1) {
64+
[1]=>
65+
object(MongoDB\Driver\WriteError)#%d (%d) {
66+
["message"]=>
67+
string(26) "Document failed validation"
68+
["code"]=>
69+
int(121)
70+
["index"]=>
71+
int(1)
72+
["info"]=>
73+
object(stdClass)#%d (%d) {
74+
["failingDocumentId"]=>
75+
int(2)
76+
["details"]=>
77+
object(stdClass)#%d (%d) {
78+
["operatorName"]=>
79+
string(11) "$jsonSchema"
80+
["schemaRulesNotSatisfied"]=>
81+
array(1) {
82+
[0]=>
83+
object(stdClass)#%d (%d) {
84+
["operatorName"]=>
85+
string(8) "required"
86+
["specifiedAs"]=>
87+
object(stdClass)#%d (%d) {
88+
["required"]=>
89+
array(1) {
90+
[0]=>
91+
string(1) "x"
92+
}
93+
}
94+
["missingProperties"]=>
95+
array(1) {
96+
[0]=>
97+
string(1) "x"
98+
}
99+
}
100+
}
101+
}
102+
}
103+
}
104+
}
105+
["writeConcernErrors"]=>
106+
array(0) {
107+
}
108+
["errorReply"]=>
109+
NULL
110+
["server"]=>
111+
object(MongoDB\Driver\Server)#%d (%d) {%A
112+
}
113+
}
114+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::__construct() comment
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+
$command = $event->getCommand();
18+
19+
if (!isset($command->comment)) {
20+
printf("%s does not include comment option\n", $event->getCommandName());
21+
22+
return;
23+
}
24+
25+
printf("%s included comment: %s\n", $event->getCommandName(), json_encode($command->comment));
26+
}
27+
28+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event): void
29+
{
30+
}
31+
32+
public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event): void
33+
{
34+
}
35+
}
36+
37+
$manager = create_test_manager();
38+
39+
$bulk = new MongoDB\Driver\BulkWriteCommand(['comment' => ['foo' => 1]]);
40+
$bulk->insertOne(NS, ['_id' => 1]);
41+
42+
$manager->addSubscriber(new CommandLogger);
43+
$manager->executeBulkWriteCommand($bulk);
44+
45+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
46+
var_dump($cursor->toArray());
47+
48+
?>
49+
===DONE===
50+
<?php exit(0); ?>
51+
--EXPECTF--
52+
bulkWrite included comment: {"foo":1}
53+
find does not include comment option
54+
array(1) {
55+
[0]=>
56+
object(stdClass)#%d (%d) {
57+
["_id"]=>
58+
int(1)
59+
}
60+
}
61+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::__construct() comment option bsonSerialize() exception
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . "/../utils/basic.inc";
7+
8+
class Comment implements MongoDB\BSON\Serializable
9+
{
10+
#[\ReturnTypeWillChange]
11+
public function bsonSerialize(): array
12+
{
13+
throw new Exception('phongo_zval_to_bson_value fails');
14+
}
15+
}
16+
17+
echo throws(function() {
18+
new MongoDB\Driver\BulkWriteCommand(['comment' => new Comment()]);
19+
}, Exception::class), "\n";
20+
21+
?>
22+
===DONE===
23+
<?php exit(0); ?>
24+
--EXPECT--
25+
OK: Got Exception
26+
phongo_zval_to_bson_value fails
27+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::__construct() let
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+
$let = [
16+
'targetFlavor' => 'cherry',
17+
'newFlavor' => 'orange',
18+
];
19+
20+
$bulk = new MongoDB\Driver\BulkWriteCommand(['let' => $let]);
21+
22+
$bulk->insertOne(NS, ['_id' => 1, 'flavor' => 'chocolate']);
23+
$bulk->insertOne(NS, ['_id' => 2, 'flavor' => 'strawberry']);
24+
$bulk->insertOne(NS, ['_id' => 3, 'flavor' => 'cherry']);
25+
26+
$bulk->updateMany(
27+
NS,
28+
['$expr' => ['$eq' => ['$flavor', '$$targetFlavor']]],
29+
['$set' => ['flavor' => '$$newFlavor']],
30+
);
31+
32+
$result = $manager->executeBulkWriteCommand($bulk);
33+
34+
var_dump($result);
35+
36+
?>
37+
===DONE===
38+
<?php exit(0); ?>
39+
--EXPECTF--
40+
object(MongoDB\Driver\BulkWriteCommandResult)#%d (%d) {
41+
["isAcknowledged"]=>
42+
bool(true)
43+
["insertedCount"]=>
44+
int(3)
45+
["matchedCount"]=>
46+
int(1)
47+
["modifiedCount"]=>
48+
int(1)
49+
["upsertedCount"]=>
50+
int(0)
51+
["deletedCount"]=>
52+
int(0)
53+
["insertResults"]=>
54+
NULL
55+
["updateResults"]=>
56+
NULL
57+
["deleteResults"]=>
58+
NULL
59+
["writeErrors"]=>
60+
array(0) {
61+
}
62+
["writeConcernErrors"]=>
63+
array(0) {
64+
}
65+
["errorReply"]=>
66+
NULL
67+
["server"]=>
68+
object(MongoDB\Driver\Server)#%d (%d) {%A
69+
}
70+
}
71+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::__construct() let option invalid type
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
$invalidValues = [true, 1, 'string', null];
9+
10+
foreach ($invalidValues as $invalidValue) {
11+
echo throws(function() use ($invalidValue) {
12+
new MongoDB\Driver\BulkWriteCommand(['let' => $invalidValue]);
13+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
14+
}
15+
16+
echo throws(function() {
17+
new MongoDB\Driver\BulkWriteCommand(['let' => MongoDB\BSON\PackedArray::fromPHP([])]);
18+
}, MongoDB\Driver\Exception\UnexpectedValueException::class), "\n";
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECT--
24+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
25+
Expected "let" option to be array or object, bool given
26+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
27+
Expected "let" option to be array or object, int given
28+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
29+
Expected "let" option to be array or object, string given
30+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
31+
Expected "let" option to be array or object, null given
32+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
33+
MongoDB\BSON\PackedArray cannot be serialized as a root document
34+
===DONE===

0 commit comments

Comments
 (0)