Skip to content
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
1 change: 1 addition & 0 deletions php_phongo_structs-5.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ typedef struct {
typedef struct {
zend_object std;
mongoc_bulk_operation_t *bulk;
size_t num_ops;
} php_phongo_bulkwrite_t;

typedef struct {
Expand Down
1 change: 1 addition & 0 deletions php_phongo_structs-7.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ typedef struct {

typedef struct {
mongoc_bulk_operation_t *bulk;
size_t num_ops;
zend_object std;
} php_phongo_bulkwrite_t;

Expand Down
9 changes: 8 additions & 1 deletion src/MongoDB/BulkWrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ PHP_METHOD(BulkWrite, __construct)
}

intern->bulk = phongo_bulkwrite_init(ordered);
intern->num_ops = 0;

if (options && php_array_exists(options, "bypassDocumentValidation")) {
mongoc_bulk_operation_set_bypass_document_validation(intern->bulk, php_array_fetch_bool(options, "bypassDocumentValidation"));
Expand Down Expand Up @@ -111,6 +112,8 @@ PHP_METHOD(BulkWrite, insert)
mongoc_bulk_operation_insert(intern->bulk, bson);
bson_clear(&bson);

intern->num_ops++;

if (bson_out && return_value_used) {
bson_iter_t iter;

Expand Down Expand Up @@ -177,6 +180,8 @@ PHP_METHOD(BulkWrite, update)
}
}

intern->num_ops++;

bson_clear(&bquery);
bson_clear(&bupdate);
}
Expand Down Expand Up @@ -208,6 +213,8 @@ PHP_METHOD(BulkWrite, delete)
mongoc_bulk_operation_remove(intern->bulk, bson);
}

intern->num_ops++;

bson_clear(&bson);
}
/* }}} */
Expand All @@ -225,7 +232,7 @@ PHP_METHOD(BulkWrite, count)
return;
}

RETURN_LONG(intern->bulk->commands.len);
RETURN_LONG(intern->num_ops);
}
/* }}} */

Expand Down
41 changes: 41 additions & 0 deletions tests/bulk/bulkwrite-count-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
MongoDB\Driver\BulkWrite::count() should return the number of operations
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$bulk = new MongoDB\Driver\BulkWrite;
var_dump($bulk->count());

$bulk->insert(['x' => 1]);
var_dump($bulk->count());

$bulk->insert(['x' => 2]);
var_dump($bulk->count());

$bulk->update(['x' => 3], ['$set' => ['y' => 3]]);
var_dump($bulk->count());

$bulk->update(['x' => 4], ['$set' => ['y' => 4]]);
var_dump($bulk->count());

$bulk->delete(['x' => 5]);
var_dump($bulk->count());

$bulk->delete(['x' => 6]);
var_dump($bulk->count());

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
int(0)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
===DONE===
17 changes: 17 additions & 0 deletions tests/bulk/bulkwrite-countable-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
MongoDB\Driver\BulkWrite implements Countable
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$bulk = new MongoDB\Driver\BulkWrite;
var_dump($bulk instanceof Countable);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
bool(true)
===DONE===