Skip to content

Commit e0b83d8

Browse files
committed
Merge pull request #462
2 parents 78711ed + 6dc41c4 commit e0b83d8

File tree

5 files changed

+41
-99
lines changed

5 files changed

+41
-99
lines changed

php_phongo.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,9 @@ bool phongo_execute_write(zval *manager, const char *namespace, php_phongo_bulkw
489489

490490
client = Z_MANAGER_OBJ_P(manager)->client;
491491

492-
/* Since BulkWrite objects can currently be executed multiple times, ensure
493-
* that the database and collection name are freed before we overwrite them.
494-
* This may be removed once PHPC-676 is implemented. */
495-
if (bulk_write->database) {
496-
efree(bulk_write->database);
497-
}
498-
499-
if (bulk_write->collection) {
500-
efree(bulk_write->collection);
492+
if (bulk_write->executed) {
493+
phongo_throw_exception(PHONGO_ERROR_WRITE_FAILED TSRMLS_CC, "BulkWrite objects may only be executed once and this instance has already been executed");
494+
return false;
501495
}
502496

503497
if (!phongo_split_namespace(namespace, &bulk_write->database, &bulk_write->collection)) {

tests/bulk/bulkwrite_error-002.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWrite cannot be executed multiple times
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(STANDALONE);
10+
11+
$bulk = new MongoDB\Driver\BulkWrite;
12+
$bulk->insert(['x' => 1]);
13+
$result = $manager->executeBulkWrite(NS, $bulk);
14+
printf("Inserted %d document(s)\n", $result->getInsertedCount());
15+
16+
echo throws(function() use ($manager, $bulk) {
17+
$result = $manager->executeBulkWrite(NS, $bulk);
18+
}, 'MongoDB\Driver\Exception\BulkWriteException'), "\n";
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
Inserted 1 document(s)
25+
OK: Got MongoDB\Driver\Exception\BulkWriteException
26+
BulkWrite objects may only be executed once and this instance has already been executed
27+
===DONE===

tests/bulk/write-0003.phpt

Lines changed: 0 additions & 85 deletions
This file was deleted.

tests/manager/manager-executeBulkWrite_error-007.phpt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ MongoDB\Driver\Manager::executeBulkWrite() should not issue warning before excep
66
<?php
77
require_once __DIR__ . "/../utils/basic.inc";
88

9-
$bulk = new MongoDB\Driver\BulkWrite;
10-
$bulk->insert(['x' => 1]);
11-
129
// Invalid host cannot be resolved
1310
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);
1411

15-
echo throws(function() use ($manager, $bulk) {
12+
echo throws(function() use ($manager) {
13+
$bulk = new MongoDB\Driver\BulkWrite;
14+
$bulk->insert(['x' => 1]);
1615
$manager->executeBulkWrite(NS, $bulk);
1716
}, 'MongoDB\Driver\Exception\ConnectionTimeoutException'), "\n";
1817

1918
// Valid host refuses connection
2019
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);
2120

22-
echo throws(function() use ($manager, $bulk) {
21+
echo throws(function() use ($manager) {
22+
$bulk = new MongoDB\Driver\BulkWrite;
23+
$bulk->insert(['x' => 1]);
2324
$manager->executeBulkWrite(NS, $bulk);
2425
}, 'MongoDB\Driver\Exception\ConnectionTimeoutException'), "\n";
2526

tests/replicaset/manager-selectserver-001.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ $bulk->insert(array('_id' => 3, 'x' => 4, 'y' => 5));
4141
throws(function() use($server2, $bulk) {
4242
$server2->executeBulkWrite(NS, $bulk);
4343
}, "MongoDB\Driver\Exception\BulkWriteException");
44+
45+
$bulk = new \MongoDB\Driver\BulkWrite();
46+
$bulk->insert(array('_id' => 1, 'x' => 2, 'y' => 3));
47+
$bulk->insert(array('_id' => 2, 'x' => 3, 'y' => 4));
48+
$bulk->insert(array('_id' => 3, 'x' => 4, 'y' => 5));
4449
$result = $server2->executeBulkWrite("local.example", $bulk);
4550
var_dump($result->getInsertedCount());
4651
?>

0 commit comments

Comments
 (0)