Skip to content

PHPC-1925: Change ServerDescription::getType() to return a string #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2021
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
34 changes: 22 additions & 12 deletions src/MongoDB/ServerDescription.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@

zend_class_entry* php_phongo_serverdescription_ce;

#define PHONGO_SERVER_UNKNOWN "Unknown"
#define PHONGO_SERVER_STANDALONE "Standalone"
#define PHONGO_SERVER_MONGOS "Mongos"
#define PHONGO_SERVER_POSSIBLE_PRIMARY "PossiblePrimary"
#define PHONGO_SERVER_RS_PRIMARY "RSPrimary"
#define PHONGO_SERVER_RS_SECONDARY "RSSecondary"
#define PHONGO_SERVER_RS_ARBITER "RSArbiter"
#define PHONGO_SERVER_RS_OTHER "RSOther"
#define PHONGO_SERVER_RS_GHOST "RSGhost"

/* {{{ proto array MongoDB\Driver\ServerDescription::getHelloResponse()
Returns the most recent "hello" response */
static PHP_METHOD(ServerDescription, getHelloResponse)
Expand Down Expand Up @@ -105,7 +115,7 @@ static PHP_METHOD(ServerDescription, getRoundTripTime)
RETVAL_LONG(mongoc_server_description_round_trip_time(intern->server_description));
} /* }}} */

/* {{{ proto integer MongoDB\Driver\ServerDescription::getType()
/* {{{ proto string MongoDB\Driver\ServerDescription::getType()
Returns the server’s node type */
static PHP_METHOD(ServerDescription, getType)
{
Expand All @@ -115,7 +125,7 @@ static PHP_METHOD(ServerDescription, getType)

PHONGO_PARSE_PARAMETERS_NONE();

RETVAL_LONG(php_phongo_server_description_type(intern->server_description));
RETVAL_STRING(mongoc_server_description_type(intern->server_description));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good opportunity to revise serverDescription-getType-001.phpt similar to what you did for the TopologyDescription::getType() test.

Additionally, php_phongo_serverdescription_get_properties_hash can be changed to output a "type" as a string instead of calling php_phongo_server_description_type. Its test can also be revised to use %r like the corresponding TopologyDescription tests.

} /* }}} */

/* {{{ MongoDB\Driver\ServerDescription function entries */
Expand Down Expand Up @@ -192,7 +202,7 @@ HashTable* php_phongo_serverdescription_get_properties_hash(phongo_compat_object
ZVAL_LONG(&port, host_list->port);
zend_hash_str_update(props, "port", sizeof("port") - 1, &port);

ZVAL_LONG(&type, php_phongo_server_description_type(intern->server_description));
ZVAL_STRING(&type, mongoc_server_description_type(intern->server_description));
zend_hash_str_update(props, "type", sizeof("type") - 1, &type);
}

Expand Down Expand Up @@ -250,15 +260,15 @@ void php_phongo_serverdescription_init_ce(INIT_FUNC_ARGS) /* {{{ */
php_phongo_handler_serverdescription.free_obj = php_phongo_serverdescription_free_object;
php_phongo_handler_serverdescription.offset = XtOffsetOf(php_phongo_serverdescription_t, std);

zend_declare_class_constant_long(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_UNKNOWN"), PHONGO_SERVER_UNKNOWN);
zend_declare_class_constant_long(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_STANDALONE"), PHONGO_SERVER_STANDALONE);
zend_declare_class_constant_long(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_MONGOS"), PHONGO_SERVER_MONGOS);
zend_declare_class_constant_long(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_POSSIBLE_PRIMARY"), PHONGO_SERVER_POSSIBLE_PRIMARY);
zend_declare_class_constant_long(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_PRIMARY"), PHONGO_SERVER_RS_PRIMARY);
zend_declare_class_constant_long(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_SECONDARY"), PHONGO_SERVER_RS_SECONDARY);
zend_declare_class_constant_long(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_ARBITER"), PHONGO_SERVER_RS_ARBITER);
zend_declare_class_constant_long(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_OTHER"), PHONGO_SERVER_RS_OTHER);
zend_declare_class_constant_long(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_GHOST"), PHONGO_SERVER_RS_GHOST);
zend_declare_class_constant_string(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_UNKNOWN"), PHONGO_SERVER_UNKNOWN);
zend_declare_class_constant_string(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_STANDALONE"), PHONGO_SERVER_STANDALONE);
zend_declare_class_constant_string(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_MONGOS"), PHONGO_SERVER_MONGOS);
zend_declare_class_constant_string(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_POSSIBLE_PRIMARY"), PHONGO_SERVER_POSSIBLE_PRIMARY);
zend_declare_class_constant_string(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_PRIMARY"), PHONGO_SERVER_RS_PRIMARY);
zend_declare_class_constant_string(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_SECONDARY"), PHONGO_SERVER_RS_SECONDARY);
zend_declare_class_constant_string(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_ARBITER"), PHONGO_SERVER_RS_ARBITER);
zend_declare_class_constant_string(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_OTHER"), PHONGO_SERVER_RS_OTHER);
zend_declare_class_constant_string(php_phongo_serverdescription_ce, ZEND_STRL("TYPE_RS_GHOST"), PHONGO_SERVER_RS_GHOST);
} /* }}} */

/*
Expand Down
2 changes: 1 addition & 1 deletion tests/server/server-getServerDescription-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object(MongoDB\Driver\ServerDescription)#%d (%d) {
["port"]=>
int(%d)
["type"]=>
int(%d)
string(%d) "%r(Standalone|Mongos|RSPrimary)%r"
["hello_response"]=>
array(%d) {
%a
Expand Down
18 changes: 9 additions & 9 deletions tests/serverDescription/serverDescription-constants.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ var_dump(MongoDB\Driver\ServerDescription::TYPE_RS_GHOST);
===DONE===
<?php exit(0); ?>
--EXPECT--
int(0)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(7)
int(8)
string(7) "Unknown"
string(10) "Standalone"
string(6) "Mongos"
string(15) "PossiblePrimary"
string(9) "RSPrimary"
string(11) "RSSecondary"
string(9) "RSArbiter"
string(7) "RSOther"
string(7) "RSGhost"
===DONE===
2 changes: 1 addition & 1 deletion tests/serverDescription/serverDescription-debug-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object(MongoDB\Driver\ServerDescription)#%d (%d) {
["port"]=>
int(%d)
["type"]=>
int(%d)
string(%d) "%r(Standalone|Mongos|RSPrimary)%r"
["hello_response"]=>
array(%d) {
%a
Expand Down
6 changes: 3 additions & 3 deletions tests/serverDescription/serverDescription-getType-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ $manager = create_test_manager();
$server = $manager->selectServer(new MongoDB\Driver\ReadPreference('primary'));
$type = $server->getServerDescription()->getType();

var_dump(in_array($type, $expected_types));
var_dump($type);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
bool(true)
--EXPECTF--
string(%d) "%r(Standalone|Mongos|RSPrimary)%r"
===DONE===
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ echo var_export($server->getServerDescription(), true), "\n";
MongoDB\Driver\ServerDescription::__set_state(array(
'host' => '%s',
'port' => %d,
'type' => %d,
'type' => '%r(Standalone|Mongos|RSPrimary)%r',
'hello_response' =>
array (
%a
Expand Down