Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed PHP-668: Allow empty setReadPreference() tags param for primary…
… mode

See doctrine/mongodb#92 for why this was helpful.
  • Loading branch information
jmikola committed Jan 28, 2013
1 parent df5d9da commit 99850ee
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 342 deletions.
2 changes: 1 addition & 1 deletion php_mongo.c
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions tests/generic/mongoclient-setreadpreference-003.phpt
@@ -0,0 +1,18 @@
--TEST--
MongoClient::setReadPreference() should allow empty tags parameter for primary mode
--SKIPIF--
<?php require_once dirname(__FILE__) ."/skipif.inc"; ?>
--FILE--
<?php require_once dirname(__FILE__) . "/../utils.inc"; ?>
<?php

$m = new_mongo();
var_dump($m->setReadPreference(Mongo::RP_PRIMARY, array()));
var_dump($m->getReadPreference());
?>
--EXPECT--
bool(true)
array(1) {
["type"]=>
string(7) "primary"
}
122 changes: 8 additions & 114 deletions 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--
<?php require_once dirname(__FILE__) ."/skipif.inc"; ?>
--FILE--
<?php require_once dirname(__FILE__) ."/skipif.inc"; ?>
<?php require_once dirname(__FILE__) . "/../utils.inc"; ?>
<?php
$host = hostname();
$port = port();
$db = dbname();

$baseString = sprintf("mongodb://%s:%d/%s", $host, $port, $db);

$a = array(
/* no tagsets */
array(),
/* one tag set */
array( array( 'dc' => '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"
}
---
122 changes: 8 additions & 114 deletions 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--
<?php require_once dirname(__FILE__) ."/skipif.inc"; ?>
--FILE--
<?php require_once dirname(__FILE__) ."/skipif.inc"; ?>
<?php require_once dirname(__FILE__) . "/../utils.inc"; ?>
<?php
$host = hostname();
$port = port();
$db = dbname();

$baseString = sprintf("mongodb://%s:%d/%s", $host, $port, $db);

$a = array(
/* no tagsets */
array(),
/* one tag set */
array( array( 'dc' => '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"
}
---

0 comments on commit 99850ee

Please sign in to comment.