Skip to content

Commit d1c99f6

Browse files
committed
Update TopologyDescription::hasReadableServer()
1 parent 40cc244 commit d1c99f6

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/MongoDB/TopologyDescription.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,24 @@ static PHP_METHOD(TopologyDescription, getServers)
6060
mongoc_server_descriptions_destroy_all(sds, n);
6161
} /* }}} */
6262

63-
/* {{{ proto boolean MongoDB\Driver\TopologyDescription::hasReadableServer()
63+
/* {{{ proto boolean MongoDB\Driver\TopologyDescription::hasReadableServer([?MongoDB\Driver\ReadPreference $readPreference])
6464
Returns whether the topology has a readable server available */
6565
static PHP_METHOD(TopologyDescription, hasReadableServer)
6666
{
67-
php_phongo_topologydescription_t* intern = Z_TOPOLOGYDESCRIPTION_OBJ_P(getThis());
68-
const mongoc_read_prefs_t* read_preference = NULL;
67+
php_phongo_topologydescription_t* intern;
68+
const mongoc_read_prefs_t* read_preference = NULL;
69+
zval* z_read_preference = NULL;
6970

70-
PHONGO_PARSE_PARAMETERS_NONE();
71+
intern = Z_TOPOLOGYDESCRIPTION_OBJ_P(getThis());
72+
73+
PHONGO_PARSE_PARAMETERS_START(0, 1)
74+
Z_PARAM_OPTIONAL
75+
Z_PARAM_ZVAL_EX(z_read_preference, 1, 0)
76+
PHONGO_PARSE_PARAMETERS_END();
77+
78+
if (z_read_preference) {
79+
read_preference = phongo_read_preference_from_zval(z_read_preference);
80+
}
7181

7282
RETVAL_BOOL(mongoc_topology_description_has_readable_server(intern->topology_description, read_preference));
7383
} /* }}} */
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
MongoDB\Driver\TopologyDescription::hasReadableServer()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
--FILE--
7+
<?php
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
$manager = create_test_manager();
11+
12+
class MySubscriber implements MongoDB\Driver\Monitoring\SDAMSubscriber
13+
{
14+
public function topologyChanged(MongoDB\Driver\Monitoring\TopologyChangedEvent $event)
15+
{
16+
$expected_types = array(
17+
MongoDB\Driver\TopologyDescription::TYPE_UNKNOWN,
18+
MongoDB\Driver\TopologyDescription::TYPE_SINGLE,
19+
MongoDB\Driver\TopologyDescription::TYPE_SHARDED,
20+
MongoDB\Driver\TopologyDescription::TYPE_REPLICA_SET_NO_PRIMARY,
21+
MongoDB\Driver\TopologyDescription::TYPE_REPLICA_SET_WITH_PRIMARY
22+
);
23+
24+
$topologyDescription = $event->getNewDescription();
25+
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
26+
27+
var_dump($topologyDescription->hasReadableServer($rp));
28+
}
29+
}
30+
31+
$subscriber = new MySubscriber;
32+
MongoDB\Driver\Monitoring\addSubscriber($subscriber);
33+
34+
$command = new MongoDB\Driver\Command(['ping' => 1]);
35+
$manager->executeCommand(DATABASE_NAME, $command);
36+
37+
?>
38+
===DONE===
39+
<?php exit(0); ?>
40+
--EXPECTF--
41+
bool(%s)
42+
bool(%s)
43+
bool(%s)
44+
===DONE===

0 commit comments

Comments
 (0)