Skip to content

Commit e3fa20c

Browse files
authored
PHPC-2571 Import BulkWrite tests for Client BulkWriteCommand (#1819)
1 parent 3929480 commit e3fa20c

28 files changed

+1555
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand debug output before execution
3+
--FILE--
4+
<?php
5+
6+
$tests = [
7+
[],
8+
['ordered' => true],
9+
['ordered' => false],
10+
['bypassDocumentValidation' => true],
11+
['bypassDocumentValidation' => false],
12+
['comment' => ['foo' => 1]],
13+
['let' => ['id' => 1, 'x' => 'foo']],
14+
];
15+
16+
foreach ($tests as $options) {
17+
var_dump(new MongoDB\Driver\BulkWriteCommand($options));
18+
}
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
25+
["bypassDocumentValidation"]=>
26+
NULL
27+
["ordered"]=>
28+
bool(true)
29+
["verboseResults"]=>
30+
bool(false)
31+
["session"]=>
32+
NULL
33+
}
34+
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
35+
["bypassDocumentValidation"]=>
36+
NULL
37+
["ordered"]=>
38+
bool(true)
39+
["verboseResults"]=>
40+
bool(false)
41+
["session"]=>
42+
NULL
43+
}
44+
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
45+
["bypassDocumentValidation"]=>
46+
NULL
47+
["ordered"]=>
48+
bool(false)
49+
["verboseResults"]=>
50+
bool(false)
51+
["session"]=>
52+
NULL
53+
}
54+
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
55+
["bypassDocumentValidation"]=>
56+
bool(true)
57+
["ordered"]=>
58+
bool(true)
59+
["verboseResults"]=>
60+
bool(false)
61+
["session"]=>
62+
NULL
63+
}
64+
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
65+
["bypassDocumentValidation"]=>
66+
bool(false)
67+
["ordered"]=>
68+
bool(true)
69+
["verboseResults"]=>
70+
bool(false)
71+
["session"]=>
72+
NULL
73+
}
74+
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
75+
["bypassDocumentValidation"]=>
76+
NULL
77+
["comment"]=>
78+
object(stdClass)#2 (1) {
79+
["foo"]=>
80+
int(1)
81+
}
82+
["ordered"]=>
83+
bool(true)
84+
["verboseResults"]=>
85+
bool(false)
86+
["session"]=>
87+
NULL
88+
}
89+
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
90+
["bypassDocumentValidation"]=>
91+
NULL
92+
["let"]=>
93+
object(stdClass)#2 (2) {
94+
["id"]=>
95+
int(1)
96+
["x"]=>
97+
string(3) "foo"
98+
}
99+
["ordered"]=>
100+
bool(true)
101+
["verboseResults"]=>
102+
bool(false)
103+
["session"]=>
104+
NULL
105+
}
106+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::deleteOne() should always encode __pclass for Persistable objects
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+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
class MyClass implements MongoDB\BSON\Persistable
13+
{
14+
private $id;
15+
private $child;
16+
17+
public function __construct($id, ?MyClass $child = null)
18+
{
19+
$this->id = $id;
20+
$this->child = $child;
21+
}
22+
23+
public function bsonSerialize(): array
24+
{
25+
return [
26+
'_id' => $this->id,
27+
'child' => $this->child,
28+
];
29+
}
30+
31+
public function bsonUnserialize(array $data): void
32+
{
33+
$this->id = $data['_id'];
34+
$this->child = $data['child'];
35+
}
36+
}
37+
38+
$manager = create_test_manager();
39+
40+
$document = new MyClass('foo', new MyClass('bar', new MyClass('baz')));
41+
42+
$bulk = new MongoDB\Driver\BulkWriteCommand();
43+
$bulk->insertOne(NS, $document);
44+
$result = $manager->executeBulkWriteCommand($bulk);
45+
printf("Inserted %d document(s)\n", $result->getInsertedCount());
46+
47+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
48+
var_dump($cursor->toArray());
49+
50+
$bulk = new MongoDB\Driver\BulkWriteCommand();
51+
$bulk->deleteOne(NS, $document);
52+
$result = $manager->executeBulkWriteCommand($bulk);
53+
printf("Deleted %d document(s)\n", $result->getDeletedCount());
54+
55+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
56+
var_dump($cursor->toArray());
57+
58+
?>
59+
===DONE===
60+
<?php exit(0); ?>
61+
--EXPECTF--
62+
Inserted 1 document(s)
63+
array(1) {
64+
[0]=>
65+
object(MyClass)#%d (%d) {
66+
["id":"MyClass":private]=>
67+
string(3) "foo"
68+
["child":"MyClass":private]=>
69+
object(MyClass)#%d (%d) {
70+
["id":"MyClass":private]=>
71+
string(3) "bar"
72+
["child":"MyClass":private]=>
73+
object(MyClass)#%d (%d) {
74+
["id":"MyClass":private]=>
75+
string(3) "baz"
76+
["child":"MyClass":private]=>
77+
NULL
78+
}
79+
}
80+
}
81+
}
82+
Deleted 1 document(s)
83+
array(0) {
84+
}
85+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::deleteOne() with hint option
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+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
class CommandLogger implements MongoDB\Driver\Monitoring\CommandSubscriber
13+
{
14+
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event): void
15+
{
16+
if ($event->getCommandName() !== 'bulkWrite') {
17+
return;
18+
}
19+
20+
printf("delete included hint: %s\n", json_encode($event->getCommand()->ops[0]->hint));
21+
}
22+
23+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event): void
24+
{
25+
}
26+
27+
public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event): void
28+
{
29+
}
30+
}
31+
32+
$manager = create_test_manager();
33+
34+
$bulk = new MongoDB\Driver\BulkWriteCommand();
35+
$bulk->insertOne(NS, ['x' => 1]);
36+
$bulk->insertOne(NS, ['x' => 2]);
37+
$manager->executeBulkWriteCommand($bulk);
38+
39+
MongoDB\Driver\Monitoring\addSubscriber(new CommandLogger);
40+
41+
$bulk = new MongoDB\Driver\BulkWriteCommand;
42+
$bulk->deleteOne(NS, ['_id' => 1], ['hint' => '_id_']);
43+
$manager->executeBulkWriteCommand($bulk);
44+
45+
$bulk = new MongoDB\Driver\BulkWriteCommand;
46+
$bulk->deleteOne(NS, ['_id' => 2], ['hint' => ['_id' => 1]]);
47+
$manager->executeBulkWriteCommand($bulk);
48+
49+
?>
50+
===DONE===
51+
<?php exit(0); ?>
52+
--EXPECTF--
53+
delete included hint: "_id_"
54+
delete included hint: {"_id":1}
55+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::deleteOne() $filter is MongoDB\BSON\Document
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+
$bulk = new MongoDB\Driver\BulkWriteCommand();
16+
$bulk->insertOne(NS, ['_id' => 1]);
17+
$bulk->insertOne(NS, ['_id' => 2]);
18+
$manager->executeBulkWriteCommand($bulk);
19+
20+
$filter = MongoDB\BSON\Document::fromJSON('{ "_id": { "$gt": 1 } }');
21+
22+
$bulk = new MongoDB\Driver\BulkWriteCommand;
23+
$bulk->deleteOne(NS, $filter);
24+
$result = $manager->executeBulkWriteCommand($bulk);
25+
26+
var_dump($result->getDeletedCount());
27+
28+
?>
29+
===DONE===
30+
<?php exit(0); ?>
31+
--EXPECT--
32+
int(1)
33+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::deleteOne() with invalid options
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
$bulk = new MongoDB\Driver\BulkWriteCommand;
9+
10+
echo throws(function() use ($bulk) {
11+
$bulk->deleteOne(NS, ['x' => 1], ['collation' => 1]);
12+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n\n";
13+
14+
echo throws(function() use ($bulk) {
15+
$bulk->deleteOne(NS, ['x' => 1], ['hint' => 1]);
16+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECT--
22+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
23+
Expected "collation" option to be array or object, int given
24+
25+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
26+
Expected "hint" option to be string, array, or object, int given
27+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::deleteOne() with BSON encoding error (invalid UTF-8 string)
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
$bulk = new MongoDB\Driver\BulkWriteCommand;
9+
10+
echo throws(function() use ($bulk) {
11+
$bulk->deleteOne(NS, ['x' => "\xc3\x28"]);
12+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";
13+
14+
echo throws(function() use ($bulk) {
15+
$bulk->deleteOne(NS, ['x' => 1], ['collation' => ['locale' => "\xc3\x28"]]);
16+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECTF--
22+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
23+
Detected invalid UTF-8 for field path "x": %s
24+
25+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
26+
Detected invalid UTF-8 for field path "locale": %s
27+
===DONE===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWriteCommand::deleteOne() with BSON encoding error (null bytes in keys)
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
$bulk = new MongoDB\Driver\BulkWriteCommand;
9+
10+
echo throws(function() use ($bulk) {
11+
$bulk->deleteOne(NS, ["\0" => 1]);
12+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";
13+
14+
echo throws(function() use ($bulk) {
15+
$bulk->deleteOne(NS, ["x\0" => 1]);
16+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";
17+
18+
echo throws(function() use ($bulk) {
19+
$bulk->deleteOne(NS, ["\0\0\0" => 1]);
20+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";
21+
22+
echo throws(function() use ($bulk) {
23+
$bulk->deleteOne(NS, ['x' => 1], ['collation' => ["\0" => 1]]);
24+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";
25+
26+
echo throws(function() use ($bulk) {
27+
$bulk->deleteOne(NS, ['x' => 1], ['collation' => ["x\0" => 1]]);
28+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";
29+
30+
echo throws(function() use ($bulk) {
31+
$bulk->deleteOne(NS, ['x' => 1], ['collation' => ["\0\0\0" => 1]]);
32+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
33+
34+
?>
35+
===DONE===
36+
<?php exit(0); ?>
37+
--EXPECT--
38+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
39+
BSON keys cannot contain null bytes. Unexpected null byte after "".
40+
41+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
42+
BSON keys cannot contain null bytes. Unexpected null byte after "x".
43+
44+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
45+
BSON keys cannot contain null bytes. Unexpected null byte after "".
46+
47+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
48+
BSON keys cannot contain null bytes. Unexpected null byte after "".
49+
50+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
51+
BSON keys cannot contain null bytes. Unexpected null byte after "x".
52+
53+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
54+
BSON keys cannot contain null bytes. Unexpected null byte after "".
55+
===DONE===

0 commit comments

Comments
 (0)