Skip to content

Commit bea901d

Browse files
committed
PHPC-443: Support "bypassDocumentValidation" option in BulkWrite
1 parent 018f216 commit bea901d

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/MongoDB/BulkWrite.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ PHP_METHOD(BulkWrite, __construct)
7676
}
7777

7878
intern->bulk = phongo_bulkwrite_init(ordered);
79+
80+
if (options && php_array_exists(options, "bypassDocumentValidation")) {
81+
mongoc_bulk_operation_set_bypass_document_validation(intern->bulk, php_array_fetch_bool(options, "bypassDocumentValidation"));
82+
}
7983
}
8084
/* }}} */
8185
/* {{{ proto mixed BulkWrite::insert(array|object $document)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWrite: bypassDocumentValidation option
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+
$command = new MongoDB\Driver\Command([
12+
'create' => COLLECTION_NAME,
13+
'validator' => ['x' => ['$type' => 'number']],
14+
]);
15+
$manager->executeCommand(DATABASE_NAME, $command);
16+
17+
$bulk = new MongoDB\Driver\BulkWrite();
18+
$bulk->insert(['_id' => 1, 'x' => 1]);
19+
$bulk->insert(['_id' => 2, 'x' => 2]);
20+
$manager->executeBulkWrite(NS, $bulk);
21+
22+
$bulk = new MongoDB\Driver\BulkWrite(['bypassDocumentValidation' => true]);
23+
$bulk->update(['_id' => 2], ['$set' => ['x' => 'two']]);
24+
$manager->executeBulkWrite(NS, $bulk);
25+
26+
$bulk = new MongoDB\Driver\BulkWrite(['bypassDocumentValidation' => true]);
27+
$bulk->insert(['_id' => 3, 'x' => 'three']);
28+
$manager->executeBulkWrite(NS, $bulk);
29+
30+
$bulk = new MongoDB\Driver\BulkWrite();
31+
$bulk->insert(['_id' => 4, 'x' => 'four']);
32+
33+
echo throws(function() use($manager, $bulk) {
34+
$manager->executeBulkWrite(NS, $bulk);
35+
}, "MongoDB\Driver\Exception\BulkWriteException"), "\n";
36+
37+
$bulk = new MongoDB\Driver\BulkWrite();
38+
$bulk->update(['_id' => 1], ['$set' => ['x' => 'one']]);
39+
40+
echo throws(function() use($manager, $bulk) {
41+
$manager->executeBulkWrite(NS, $bulk);
42+
}, "MongoDB\Driver\Exception\BulkWriteException"), "\n";
43+
44+
$bulk = new MongoDB\Driver\BulkWrite();
45+
$bulk->update(['_id' => 2], ['$set' => ['x' => 2]]);
46+
$manager->executeBulkWrite(NS, $bulk);
47+
48+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
49+
var_dump(iterator_to_array($cursor));
50+
51+
?>
52+
===DONE===
53+
<?php exit(0); ?>
54+
--EXPECTF--
55+
OK: Got MongoDB\Driver\Exception\BulkWriteException
56+
BulkWrite error
57+
OK: Got MongoDB\Driver\Exception\BulkWriteException
58+
BulkWrite error
59+
array(3) {
60+
[0]=>
61+
object(stdClass)#%d (2) {
62+
["_id"]=>
63+
int(1)
64+
["x"]=>
65+
int(1)
66+
}
67+
[1]=>
68+
object(stdClass)#%d (2) {
69+
["_id"]=>
70+
int(2)
71+
["x"]=>
72+
int(2)
73+
}
74+
[2]=>
75+
object(stdClass)#%d (2) {
76+
["_id"]=>
77+
int(3)
78+
["x"]=>
79+
string(5) "three"
80+
}
81+
}
82+
===DONE===

0 commit comments

Comments
 (0)