From bccdd2771ffdf3b3b8eef1bb5ebc00fe71f1d4e5 Mon Sep 17 00:00:00 2001 From: Hannes Magnusson Date: Wed, 26 Nov 2014 13:03:18 -0800 Subject: [PATCH] PHP-1294: Emtpy Query Result should still return QueryResult --- php_phongo.c | 6 ++--- .../standalone/manager-executeDelete-002.phpt | 7 +----- tests/standalone/server-executeQuery-004.phpt | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 tests/standalone/server-executeQuery-004.phpt diff --git a/php_phongo.c b/php_phongo.c index 546e5010..41ccf07a 100644 --- a/php_phongo.c +++ b/php_phongo.c @@ -219,8 +219,7 @@ void phongo_result_init(zval *return_value, zend_class_entry *result_class, mong } else { result->hint = server_hint; } - /* FIXME: Support empty result cursor. eg. when there simply aren't any matches for a query */ - result->firstBatch = bson_copy(bson); + result->firstBatch = bson ? bson_copy(bson) : NULL; } /* }}} */ void phongo_server_init(zval *return_value, int hint, mongoc_host_list_t *host TSRMLS_DC) /* {{{ */ @@ -580,14 +579,13 @@ int phongo_execute_query(mongoc_client_t *client, char *namespace, php_phongo_qu if (!mongoc_cursor_next(cursor, &doc)) { bson_error_t error; + /* Could simply be no docs, which is not an error */ if (mongoc_cursor_error(cursor, &error)) { phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC); mongoc_cursor_destroy(cursor); return false; } - mongoc_cursor_destroy(cursor); - return false; } if (!return_value_used) { diff --git a/tests/standalone/manager-executeDelete-002.phpt b/tests/standalone/manager-executeDelete-002.phpt index 19ef8f18..55fe74b9 100644 --- a/tests/standalone/manager-executeDelete-002.phpt +++ b/tests/standalone/manager-executeDelete-002.phpt @@ -20,12 +20,7 @@ printWriteResult($result); echo "\n===> Collection\n"; $cursor = $manager->executeQuery(NS, new MongoDB\Query(array())); -/* FIXME: Returns NULL, not empty iterator when nothing matches */ -if ($cursor) { - var_dump(iterator_to_array($cursor)); -} else { - var_dump(array()); -} +var_dump(iterator_to_array($cursor)); ?> ===DONE=== diff --git a/tests/standalone/server-executeQuery-004.phpt b/tests/standalone/server-executeQuery-004.phpt new file mode 100644 index 00000000..db95f890 --- /dev/null +++ b/tests/standalone/server-executeQuery-004.phpt @@ -0,0 +1,24 @@ +--TEST-- +MongoDB\Manager::executeDelete() multiple documents +--SKIPIF-- + +--FILE-- +executeInsert(NS, array('_id' => 1, 'x' => 1)); +$manager->executeInsert(NS, array('_id' => 2, 'x' => 1)); + +$cursor = $manager->executeQuery(NS, new MongoDB\Query(array("x" => 2))); + +var_dump(iterator_to_array($cursor)); + +?> +===DONE=== + +--EXPECT-- +array(0) { +} +===DONE===