Skip to content

Add tests on client bulk write replaceOne, updateMany and deleteMany #1820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteMany-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteMany() should always encode __pclass for Persistable objects
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_live(); ?>
<?php skip_if_server_version('<', '8.0'); ?>
<?php skip_if_not_clean(); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

class MyClass implements MongoDB\BSON\Persistable
{
private $id;
private $child;

public function __construct($id, ?MyClass $child = null)
{
$this->id = $id;
$this->child = $child;
}

public function bsonSerialize(): array
{
return [
'_id' => $this->id,
'child' => $this->child,
];
}

public function bsonUnserialize(array $data): void
{
$this->id = $data['_id'];
$this->child = $data['child'];
}
}

$manager = create_test_manager();

$document = new MyClass('foo', new MyClass('bar', new MyClass('baz')));

$bulk = new MongoDB\Driver\BulkWriteCommand();
$bulk->insertOne(NS, $document);
$bulk->insertOne(NS, new MyClass('foo2', new MyClass('bar', new MyClass('baz'))));
$bulk->insertOne(NS, new MyClass('foo3', new MyClass('bar', new MyClass('baz'))));
$result = $manager->executeBulkWriteCommand($bulk);
printf("Inserted %d document(s)\n", $result->getInsertedCount());

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
var_dump(count($cursor->toArray()));

$bulk = new MongoDB\Driver\BulkWriteCommand();
$bulk->deleteMany(NS, $document);
$result = $manager->executeBulkWriteCommand($bulk);
printf("Deleted %d document(s)\n", $result->getDeletedCount());

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
var_dump(count($cursor->toArray()));

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Inserted 3 document(s)
int(3)
Deleted 1 document(s)
int(2)
===DONE===
55 changes: 55 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteMany-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteMany() with hint option
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_live(); ?>
<?php skip_if_server_version('<', '8.0'); ?>
<?php skip_if_not_clean(); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

class CommandLogger implements MongoDB\Driver\Monitoring\CommandSubscriber
{
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event): void
{
if ($event->getCommandName() !== 'bulkWrite') {
return;
}

printf("delete included hint: %s\n", json_encode($event->getCommand()->ops[0]->hint));
}

public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event): void
{
}

public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event): void
{
}
}

$manager = create_test_manager();

$bulk = new MongoDB\Driver\BulkWriteCommand();
$bulk->insertOne(NS, ['x' => 1]);
$bulk->insertOne(NS, ['x' => 2]);
$manager->executeBulkWriteCommand($bulk);

MongoDB\Driver\Monitoring\addSubscriber(new CommandLogger);

$bulk = new MongoDB\Driver\BulkWriteCommand;
$bulk->deleteMany(NS, ['_id' => 1], ['hint' => '_id_']);
$manager->executeBulkWriteCommand($bulk);

$bulk = new MongoDB\Driver\BulkWriteCommand;
$bulk->deleteMany(NS, ['_id' => 2], ['hint' => ['_id' => 1]]);
$manager->executeBulkWriteCommand($bulk);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
delete included hint: "_id_"
delete included hint: {"_id":1}
===DONE===
34 changes: 34 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteMany-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteMany() $filter is MongoDB\BSON\Document
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_live(); ?>
<?php skip_if_server_version('<', '8.0'); ?>
<?php skip_if_not_clean(); ?>
--FILE--
<?php

require_once __DIR__ . "/../utils/basic.inc";

$manager = create_test_manager();

$bulk = new MongoDB\Driver\BulkWriteCommand();
$bulk->insertOne(NS, ['_id' => 1]);
$bulk->insertOne(NS, ['_id' => 2]);
$bulk->insertOne(NS, ['_id' => 3]);
$manager->executeBulkWriteCommand($bulk);

$filter = MongoDB\BSON\Document::fromJSON('{ "_id": { "$gt": 1 } }');

$bulk = new MongoDB\Driver\BulkWriteCommand;
$bulk->deleteMany(NS, $filter);
$result = $manager->executeBulkWriteCommand($bulk);

var_dump($result->getDeletedCount());

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
int(2)
===DONE===
27 changes: 27 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteMany_error-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteMany() with invalid options
--FILE--
<?php

require_once __DIR__ . '/../utils/basic.inc';

$bulk = new MongoDB\Driver\BulkWriteCommand;

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ['x' => 1], ['collation' => 1]);
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n\n";

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ['x' => 1], ['hint' => 1]);
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected "collation" option to be array or object, int given

OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected "hint" option to be string, array, or object, int given
===DONE===
27 changes: 27 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteMany_error-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteMany() with BSON encoding error (invalid UTF-8 string)
--FILE--
<?php

require_once __DIR__ . '/../utils/basic.inc';

$bulk = new MongoDB\Driver\BulkWriteCommand;

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ['x' => "\xc3\x28"]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ['x' => 1], ['collation' => ['locale' => "\xc3\x28"]]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Detected invalid UTF-8 for field path "x": %s

OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Detected invalid UTF-8 for field path "locale": %s
===DONE===
55 changes: 55 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteMany_error-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteMany() with BSON encoding error (null bytes in keys)
--FILE--
<?php

require_once __DIR__ . '/../utils/basic.inc';

$bulk = new MongoDB\Driver\BulkWriteCommand;

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ["\0" => 1]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ["x\0" => 1]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ["\0\0\0" => 1]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ['x' => 1], ['collation' => ["\0" => 1]]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ['x' => 1], ['collation' => ["x\0" => 1]]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, ['x' => 1], ['collation' => ["\0\0\0" => 1]]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
BSON keys cannot contain null bytes. Unexpected null byte after "".

OK: Got MongoDB\Driver\Exception\UnexpectedValueException
BSON keys cannot contain null bytes. Unexpected null byte after "x".

OK: Got MongoDB\Driver\Exception\UnexpectedValueException
BSON keys cannot contain null bytes. Unexpected null byte after "".

OK: Got MongoDB\Driver\Exception\UnexpectedValueException
BSON keys cannot contain null bytes. Unexpected null byte after "".

OK: Got MongoDB\Driver\Exception\UnexpectedValueException
BSON keys cannot contain null bytes. Unexpected null byte after "x".

OK: Got MongoDB\Driver\Exception\UnexpectedValueException
BSON keys cannot contain null bytes. Unexpected null byte after "".
===DONE===
32 changes: 32 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteMany_error-004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteMany() prohibits PackedArray for document values
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$bulk = new MongoDB\Driver\BulkWriteCommand;

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, MongoDB\BSON\PackedArray::fromPHP([]));
}, MongoDB\Driver\Exception\UnexpectedValueException::class), "\n";

echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, [], ['collation' => MongoDB\BSON\PackedArray::fromPHP([])]);
}, MongoDB\Driver\Exception\UnexpectedValueException::class), "\n";

// Expected "hint" option to yield string or document but got "array"
echo throws(function() use ($bulk) {
$bulk->deleteMany(NS, [], ['hint' => MongoDB\BSON\PackedArray::fromPHP([])]);
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
MongoDB\BSON\PackedArray cannot be serialized as a root document
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
MongoDB\BSON\PackedArray cannot be serialized as a root document
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected "hint" option to yield string or document but got "array"
===DONE===
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ echo throws(function() use ($bulk) {
$bulk->deleteOne(NS, [], ['collation' => MongoDB\BSON\PackedArray::fromPHP([])]);
}, MongoDB\Driver\Exception\UnexpectedValueException::class), "\n";

// @TODO: ALMOST: Got MongoDB\Driver\Exception\InvalidArgumentException - expected MongoDB\Driver\Exception\UnexpectedValueException
// Expected "hint" option to yield string or document but got "array"
echo throws(function() use ($bulk) {
$bulk->deleteOne(NS, [], ['hint' => MongoDB\BSON\PackedArray::fromPHP([])]);
Expand Down
Loading