From 99850eef3ea74945fe7a398afefb3d832bfc27a3 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Wed, 23 Jan 2013 18:24:00 -0500 Subject: [PATCH] Fixed PHP-668: Allow empty setReadPreference() tags param for primary mode See doctrine/mongodb#92 for why this was helpful. --- php_mongo.c | 2 +- .../mongoclient-setreadpreference-003.phpt | 18 +++ ...mongocollection-setreadpreference-003.phpt | 122 ++---------------- .../mongocursor-setreadpreference-003.phpt | 122 ++---------------- .../mongodb-setreadpreference-003.phpt | 121 ++--------------- 5 files changed, 43 insertions(+), 342 deletions(-) create mode 100644 tests/generic/mongoclient-setreadpreference-003.phpt diff --git a/php_mongo.c b/php_mongo.c index 26a94cc76..2c06dee99 100644 --- a/php_mongo.c +++ b/php_mongo.c @@ -504,7 +504,7 @@ int php_mongo_set_readpreference(mongo_read_preference *rp, char *read_preferenc php_error_docref(NULL TSRMLS_CC, E_WARNING, "The value '%s' is not valid as read preference type", read_preference); return 0; } - if (tags) { + if (tags && zend_hash_num_elements(tags)) { if (strcasecmp(read_preference, "primary") == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "You can't use read preference tags with a read preference of PRIMARY"); return 0; diff --git a/tests/generic/mongoclient-setreadpreference-003.phpt b/tests/generic/mongoclient-setreadpreference-003.phpt new file mode 100644 index 000000000..cdf5e5002 --- /dev/null +++ b/tests/generic/mongoclient-setreadpreference-003.phpt @@ -0,0 +1,18 @@ +--TEST-- +MongoClient::setReadPreference() should allow empty tags parameter for primary mode +--SKIPIF-- + +--FILE-- + +setReadPreference(Mongo::RP_PRIMARY, array())); +var_dump($m->getReadPreference()); +?> +--EXPECT-- +bool(true) +array(1) { + ["type"]=> + string(7) "primary" +} diff --git a/tests/generic/mongocollection-setreadpreference-003.phpt b/tests/generic/mongocollection-setreadpreference-003.phpt index 2a147a89f..93b986560 100644 --- a/tests/generic/mongocollection-setreadpreference-003.phpt +++ b/tests/generic/mongocollection-setreadpreference-003.phpt @@ -1,125 +1,19 @@ --TEST-- -MongoCollection::setReadPreference [3] +MongoCollection::setReadPreference() should allow empty tags parameter for primary mode --SKIPIF-- --FILE-- - + 'east' ) ), - array( array( 'dc' => 'east', 'use' => 'reporting' ) ), - array( array() ), - /* two tag sets */ - array( array( 'dc' => 'east', 'use' => 'reporting' ), array( 'dc' => 'west' ) ), - /* two tag sets + empty one */ - array( array( 'dc' => 'east', 'use' => 'reporting' ), array( 'dc' => 'west' ), array() ), -); - -foreach ($a as $value) { - $m = new mongo($baseString); - $d = $m->phpunit; - $c = $d->test; - $c->setReadPreference(Mongo::RP_SECONDARY, $value); - $rp = $c->getReadPreference(); - var_dump($rp); - echo "---\n"; -} +$m = new_mongo(); +$c = $m->phpunit->test; +var_dump($c->setReadPreference(Mongo::RP_PRIMARY, array())); +var_dump($c->getReadPreference()); ?> --EXPECT-- +bool(true) array(1) { ["type"]=> - string(9) "secondary" -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(1) { - [0]=> - array(1) { - ["dc"]=> - string(4) "east" - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(1) { - [0]=> - array(2) { - ["dc"]=> - string(4) "east" - ["use"]=> - string(9) "reporting" - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(1) { - [0]=> - array(0) { - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(2) { - [0]=> - array(2) { - ["dc"]=> - string(4) "east" - ["use"]=> - string(9) "reporting" - } - [1]=> - array(1) { - ["dc"]=> - string(4) "west" - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(3) { - [0]=> - array(2) { - ["dc"]=> - string(4) "east" - ["use"]=> - string(9) "reporting" - } - [1]=> - array(1) { - ["dc"]=> - string(4) "west" - } - [2]=> - array(0) { - } - } + string(7) "primary" } ---- diff --git a/tests/generic/mongocursor-setreadpreference-003.phpt b/tests/generic/mongocursor-setreadpreference-003.phpt index ca791d66f..fd06c8266 100644 --- a/tests/generic/mongocursor-setreadpreference-003.phpt +++ b/tests/generic/mongocursor-setreadpreference-003.phpt @@ -1,125 +1,19 @@ --TEST-- -MongoCursor::setReadPreference [3] +MongoCursor::setReadPreference() should allow empty tags parameter for primary mode --SKIPIF-- --FILE-- - + 'east' ) ), - array( array( 'dc' => 'east', 'use' => 'reporting' ) ), - array( array() ), - /* two tag sets */ - array( array( 'dc' => 'east', 'use' => 'reporting' ), array( 'dc' => 'west' ) ), - /* two tag sets + empty one */ - array( array( 'dc' => 'east', 'use' => 'reporting' ), array( 'dc' => 'west' ), array() ), -); - -foreach ($a as $value) { - $m = new mongo($baseString); - $d = $m->phpunit; - $c = $d->test->find(); - $c->setReadPreference(Mongo::RP_SECONDARY, $value); - $rp = $c->getReadPreference(); - var_dump($rp); - echo "---\n"; -} +$m = new_mongo(); +$c = $m->phpunit->test->find(); +var_dump($c === $c->setReadPreference(Mongo::RP_PRIMARY, array())); +var_dump($c->getReadPreference()); ?> --EXPECT-- +bool(true) array(1) { ["type"]=> - string(9) "secondary" -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(1) { - [0]=> - array(1) { - ["dc"]=> - string(4) "east" - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(1) { - [0]=> - array(2) { - ["dc"]=> - string(4) "east" - ["use"]=> - string(9) "reporting" - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(1) { - [0]=> - array(0) { - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(2) { - [0]=> - array(2) { - ["dc"]=> - string(4) "east" - ["use"]=> - string(9) "reporting" - } - [1]=> - array(1) { - ["dc"]=> - string(4) "west" - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(3) { - [0]=> - array(2) { - ["dc"]=> - string(4) "east" - ["use"]=> - string(9) "reporting" - } - [1]=> - array(1) { - ["dc"]=> - string(4) "west" - } - [2]=> - array(0) { - } - } + string(7) "primary" } ---- diff --git a/tests/generic/mongodb-setreadpreference-003.phpt b/tests/generic/mongodb-setreadpreference-003.phpt index d0da9ce6a..9de490b98 100644 --- a/tests/generic/mongodb-setreadpreference-003.phpt +++ b/tests/generic/mongodb-setreadpreference-003.phpt @@ -1,124 +1,19 @@ --TEST-- -MongoDB::setReadPreference [3] +MongoDB::setReadPreference() should allow empty tags parameter for primary mode --SKIPIF-- --FILE-- - + 'east' ) ), - array( array( 'dc' => 'east', 'use' => 'reporting' ) ), - array( array() ), - /* two tag sets */ - array( array( 'dc' => 'east', 'use' => 'reporting' ), array( 'dc' => 'west' ) ), - /* two tag sets + empty one */ - array( array( 'dc' => 'east', 'use' => 'reporting' ), array( 'dc' => 'west' ), array() ), -); - -foreach ($a as $value) { - $m = new mongo($baseString); - $d = $m->phpunit; - $d->setReadPreference(Mongo::RP_SECONDARY, $value); - $rp = $d->getReadPreference(); - var_dump($rp); - echo "---\n"; -} +$m = new_mongo(); +$db = $m->phpunit; +var_dump($db->setReadPreference(Mongo::RP_PRIMARY, array())); +var_dump($db->getReadPreference()); ?> --EXPECT-- +bool(true) array(1) { ["type"]=> - string(9) "secondary" -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(1) { - [0]=> - array(1) { - ["dc"]=> - string(4) "east" - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(1) { - [0]=> - array(2) { - ["dc"]=> - string(4) "east" - ["use"]=> - string(9) "reporting" - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(1) { - [0]=> - array(0) { - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(2) { - [0]=> - array(2) { - ["dc"]=> - string(4) "east" - ["use"]=> - string(9) "reporting" - } - [1]=> - array(1) { - ["dc"]=> - string(4) "west" - } - } -} ---- -array(2) { - ["type"]=> - string(9) "secondary" - ["tagsets"]=> - array(3) { - [0]=> - array(2) { - ["dc"]=> - string(4) "east" - ["use"]=> - string(9) "reporting" - } - [1]=> - array(1) { - ["dc"]=> - string(4) "west" - } - [2]=> - array(0) { - } - } + string(7) "primary" } ----