Skip to content

PHPC-2571 Import BulkWrite tests for Client BulkWriteCommand #1819

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 2 commits into from
Apr 17, 2025
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
106 changes: 106 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-debug-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
--TEST--
MongoDB\Driver\BulkWriteCommand debug output before execution
--FILE--
<?php

$tests = [
[],
['ordered' => true],
['ordered' => false],
['bypassDocumentValidation' => true],
['bypassDocumentValidation' => false],
['comment' => ['foo' => 1]],
['let' => ['id' => 1, 'x' => 'foo']],
];

foreach ($tests as $options) {
var_dump(new MongoDB\Driver\BulkWriteCommand($options));
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
["bypassDocumentValidation"]=>
NULL
["ordered"]=>
bool(true)
["verboseResults"]=>
bool(false)
["session"]=>
NULL
}
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
["bypassDocumentValidation"]=>
NULL
["ordered"]=>
bool(true)
["verboseResults"]=>
bool(false)
["session"]=>
NULL
}
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
["bypassDocumentValidation"]=>
NULL
["ordered"]=>
bool(false)
["verboseResults"]=>
bool(false)
["session"]=>
NULL
}
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
["bypassDocumentValidation"]=>
bool(true)
["ordered"]=>
bool(true)
["verboseResults"]=>
bool(false)
["session"]=>
NULL
}
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
["bypassDocumentValidation"]=>
bool(false)
["ordered"]=>
bool(true)
["verboseResults"]=>
bool(false)
["session"]=>
NULL
}
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
["bypassDocumentValidation"]=>
NULL
["comment"]=>
object(stdClass)#2 (1) {
["foo"]=>
int(1)
}
["ordered"]=>
bool(true)
["verboseResults"]=>
bool(false)
["session"]=>
NULL
}
object(MongoDB\Driver\BulkWriteCommand)#%d (%d) {
["bypassDocumentValidation"]=>
NULL
["let"]=>
object(stdClass)#2 (2) {
["id"]=>
int(1)
["x"]=>
string(3) "foo"
}
["ordered"]=>
bool(true)
["verboseResults"]=>
bool(false)
["session"]=>
NULL
}
===DONE===
85 changes: 85 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteOne-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteOne() 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);
$result = $manager->executeBulkWriteCommand($bulk);
printf("Inserted %d document(s)\n", $result->getInsertedCount());

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

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

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

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Inserted 1 document(s)
array(1) {
[0]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "foo"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "bar"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "baz"
["child":"MyClass":private]=>
NULL
}
}
}
}
Deleted 1 document(s)
array(0) {
}
===DONE===
55 changes: 55 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteOne-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteOne() 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->deleteOne(NS, ['_id' => 1], ['hint' => '_id_']);
$manager->executeBulkWriteCommand($bulk);

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

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
delete included hint: "_id_"
delete included hint: {"_id":1}
===DONE===
33 changes: 33 additions & 0 deletions tests/bulkwritecommand/bulkwritecommand-deleteOne-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteOne() $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]);
$manager->executeBulkWriteCommand($bulk);

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

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

var_dump($result->getDeletedCount());

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
int(1)
===DONE===
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteOne() with invalid options
--FILE--
<?php

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

$bulk = new MongoDB\Driver\BulkWriteCommand;

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

echo throws(function() use ($bulk) {
$bulk->deleteOne(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===
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteOne() 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->deleteOne(NS, ['x' => "\xc3\x28"]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";

echo throws(function() use ($bulk) {
$bulk->deleteOne(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===
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
MongoDB\Driver\BulkWriteCommand::deleteOne() 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->deleteOne(NS, ["\0" => 1]);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n\n";

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

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

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

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

echo throws(function() use ($bulk) {
$bulk->deleteOne(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===
Loading