Skip to content

Commit 3b7f42b

Browse files
committed
PHPC-196: Implement Manager getters for WC and RP
1 parent 145ddda commit 3b7f42b

File tree

3 files changed

+315
-0
lines changed

3 files changed

+315
-0
lines changed

src/MongoDB/Manager.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ PHP_METHOD(Manager, executeDelete)
275275
bson_clear(&bson);
276276
}
277277
/* }}} */
278+
/* {{{ proto MongoDB\Driver\ReadPreference Manager::getReadPreference()
279+
Returns the ReadPreference associated with this Manager */
280+
PHP_METHOD(Manager, getReadPreference)
281+
{
282+
php_phongo_manager_t *intern;
283+
(void)return_value_ptr;
284+
285+
intern = (php_phongo_manager_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
286+
287+
if (zend_parse_parameters_none() == FAILURE) {
288+
return;
289+
}
290+
291+
if (return_value_used) {
292+
php_phongo_read_preference_to_zval(return_value, mongoc_client_get_read_prefs(intern->client));
293+
}
294+
}
295+
/* }}} */
278296
/* {{{ proto MongoDB\Driver\Server[] Manager::getServers()
279297
Returns the Servers associated with this Manager */
280298
PHP_METHOD(Manager, getServers)
@@ -303,6 +321,24 @@ PHP_METHOD(Manager, getServers)
303321
}
304322
}
305323
/* }}} */
324+
/* {{{ proto MongoDB\Driver\WriteConcern Manager::getWriteConcern()
325+
Returns the WriteConcern associated with this Manager */
326+
PHP_METHOD(Manager, getWriteConcern)
327+
{
328+
php_phongo_manager_t *intern;
329+
(void)return_value_ptr;
330+
331+
intern = (php_phongo_manager_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
332+
333+
if (zend_parse_parameters_none() == FAILURE) {
334+
return;
335+
}
336+
337+
if (return_value_used) {
338+
php_phongo_write_concern_to_zval(return_value, mongoc_client_get_write_concern(intern->client));
339+
}
340+
}
341+
/* }}} */
306342
/* {{{ proto MongoDB\Driver\Server Manager::selectServers(MongoDB\Driver\ReadPreference $readPreference)
307343
Returns a suitable Server for the given $readPreference */
308344
PHP_METHOD(Manager, selectServer)
@@ -398,9 +434,15 @@ ZEND_BEGIN_ARG_INFO_EX(ai_Manager_executeDelete, 0, 0, 2)
398434
ZEND_ARG_OBJ_INFO(0, writeConcern, MongoDB\\Driver\\WriteConcern, 1)
399435
ZEND_END_ARG_INFO();
400436

437+
ZEND_BEGIN_ARG_INFO_EX(ai_Manager_getReadPreference, 0, 0, 0)
438+
ZEND_END_ARG_INFO();
439+
401440
ZEND_BEGIN_ARG_INFO_EX(ai_Manager_getServers, 0, 0, 0)
402441
ZEND_END_ARG_INFO();
403442

443+
ZEND_BEGIN_ARG_INFO_EX(ai_Manager_getWriteConcern, 0, 0, 0)
444+
ZEND_END_ARG_INFO();
445+
404446
ZEND_BEGIN_ARG_INFO_EX(ai_Manager_selectServer, 0, 0, 1)
405447
ZEND_ARG_OBJ_INFO(0, readPreference, MongoDB\\Driver\\ReadPreference, 1)
406448
ZEND_END_ARG_INFO();
@@ -413,7 +455,9 @@ static zend_function_entry php_phongo_manager_me[] = {
413455
PHP_ME(Manager, executeInsert, ai_Manager_executeInsert, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
414456
PHP_ME(Manager, executeUpdate, ai_Manager_executeUpdate, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
415457
PHP_ME(Manager, executeDelete, ai_Manager_executeDelete, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
458+
PHP_ME(Manager, getReadPreference, ai_Manager_getReadPreference, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
416459
PHP_ME(Manager, getServers, ai_Manager_getServers, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
460+
PHP_ME(Manager, getWriteConcern, ai_Manager_getWriteConcern, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
417461
PHP_ME(Manager, selectServer, ai_Manager_selectServer, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
418462
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
419463
PHP_FE_END
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::getReadPreference()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = array(
10+
array(STANDALONE, array()),
11+
array(STANDALONE . '/?readPreference=secondary', array()),
12+
array(STANDALONE, array('readPreference' => 'primaryPreferred')),
13+
array(STANDALONE . '/?readPreference=secondary', array('readPreference' => 'secondaryPreferred')),
14+
array(STANDALONE . '/?readPreference=secondary&readPreferenceTags=dc:ny,use:reports&readPreferenceTags=', array()),
15+
array(STANDALONE . '/?readPreference=secondary', array('readPreferenceTags' => array(array('dc' => 'ny', 'use' => 'reports'), array()))),
16+
array(STANDALONE . '/?readPreference=secondary&readPreferenceTags=dc:ny,use:reports', array('readPreferenceTags' => array(array('dc' => 'ca')))),
17+
);
18+
19+
foreach ($tests as $i => $test) {
20+
list($uri, $options) = $test;
21+
22+
$manager = new MongoDB\Driver\Manager($uri, $options);
23+
var_dump($manager->getReadPreference());
24+
25+
// Test for !return_value_used
26+
$manager->getReadPreference();
27+
}
28+
29+
?>
30+
===DONE===
31+
<?php exit(0); ?>
32+
--EXPECT--
33+
array(2) {
34+
["mode"]=>
35+
int(1)
36+
["tags"]=>
37+
array(0) {
38+
}
39+
}
40+
array(2) {
41+
["mode"]=>
42+
int(2)
43+
["tags"]=>
44+
array(0) {
45+
}
46+
}
47+
array(2) {
48+
["mode"]=>
49+
int(5)
50+
["tags"]=>
51+
array(0) {
52+
}
53+
}
54+
array(2) {
55+
["mode"]=>
56+
int(6)
57+
["tags"]=>
58+
array(0) {
59+
}
60+
}
61+
array(2) {
62+
["mode"]=>
63+
int(2)
64+
["tags"]=>
65+
array(2) {
66+
[0]=>
67+
array(2) {
68+
["dc"]=>
69+
string(2) "ny"
70+
["use"]=>
71+
string(7) "reports"
72+
}
73+
[1]=>
74+
array(0) {
75+
}
76+
}
77+
}
78+
array(2) {
79+
["mode"]=>
80+
int(2)
81+
["tags"]=>
82+
array(2) {
83+
[0]=>
84+
array(2) {
85+
["dc"]=>
86+
string(2) "ny"
87+
["use"]=>
88+
string(7) "reports"
89+
}
90+
[1]=>
91+
array(0) {
92+
}
93+
}
94+
}
95+
array(2) {
96+
["mode"]=>
97+
int(2)
98+
["tags"]=>
99+
array(1) {
100+
[0]=>
101+
array(1) {
102+
["dc"]=>
103+
string(2) "ca"
104+
}
105+
}
106+
}
107+
===DONE===
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::getWriteConcern()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = array(
10+
array(STANDALONE, array()),
11+
array(STANDALONE . '/?w=1', array()),
12+
array(STANDALONE . '/?w=majority', array()),
13+
array(STANDALONE, array('w' => 1, 'journal' => true)),
14+
array(STANDALONE, array('w' => 'majority', 'journal' => true)),
15+
array(STANDALONE . '/?w=majority&journal=true', array('w' => 1, 'journal' => false)),
16+
// wtimeoutms does not get applied unless w > 1, w = majority, or tag sets are used
17+
array(STANDALONE . '/?wtimeoutms=1000', array()),
18+
array(STANDALONE, array('wtimeoutms' => 1000)),
19+
array(STANDALONE . '/?w=2', array('wtimeoutms' => 1000)),
20+
array(STANDALONE . '/?w=majority', array('wtimeoutms' => 1000)),
21+
array(STANDALONE . '/?w=customTagSet', array('wtimeoutms' => 1000)),
22+
);
23+
24+
foreach ($tests as $i => $test) {
25+
list($uri, $options) = $test;
26+
27+
$manager = new MongoDB\Driver\Manager($uri, $options);
28+
var_dump($manager->getWriteConcern());
29+
30+
// Test for !return_value_used
31+
$manager->getWriteConcern();
32+
}
33+
34+
?>
35+
===DONE===
36+
<?php exit(0); ?>
37+
--EXPECT--
38+
array(4) {
39+
["wmajority"]=>
40+
bool(false)
41+
["wtimeout"]=>
42+
int(0)
43+
["fsync"]=>
44+
NULL
45+
["journal"]=>
46+
NULL
47+
}
48+
array(5) {
49+
["w"]=>
50+
int(1)
51+
["wmajority"]=>
52+
bool(false)
53+
["wtimeout"]=>
54+
int(0)
55+
["fsync"]=>
56+
NULL
57+
["journal"]=>
58+
NULL
59+
}
60+
array(5) {
61+
["w"]=>
62+
string(8) "majority"
63+
["wmajority"]=>
64+
bool(true)
65+
["wtimeout"]=>
66+
int(0)
67+
["fsync"]=>
68+
NULL
69+
["journal"]=>
70+
NULL
71+
}
72+
array(5) {
73+
["w"]=>
74+
int(1)
75+
["wmajority"]=>
76+
bool(false)
77+
["wtimeout"]=>
78+
int(0)
79+
["fsync"]=>
80+
NULL
81+
["journal"]=>
82+
bool(true)
83+
}
84+
array(5) {
85+
["w"]=>
86+
string(8) "majority"
87+
["wmajority"]=>
88+
bool(true)
89+
["wtimeout"]=>
90+
int(0)
91+
["fsync"]=>
92+
NULL
93+
["journal"]=>
94+
bool(true)
95+
}
96+
array(5) {
97+
["w"]=>
98+
int(1)
99+
["wmajority"]=>
100+
bool(false)
101+
["wtimeout"]=>
102+
int(0)
103+
["fsync"]=>
104+
NULL
105+
["journal"]=>
106+
bool(false)
107+
}
108+
array(4) {
109+
["wmajority"]=>
110+
bool(false)
111+
["wtimeout"]=>
112+
int(0)
113+
["fsync"]=>
114+
NULL
115+
["journal"]=>
116+
NULL
117+
}
118+
array(4) {
119+
["wmajority"]=>
120+
bool(false)
121+
["wtimeout"]=>
122+
int(0)
123+
["fsync"]=>
124+
NULL
125+
["journal"]=>
126+
NULL
127+
}
128+
array(5) {
129+
["w"]=>
130+
int(2)
131+
["wmajority"]=>
132+
bool(false)
133+
["wtimeout"]=>
134+
int(1000)
135+
["fsync"]=>
136+
NULL
137+
["journal"]=>
138+
NULL
139+
}
140+
array(5) {
141+
["w"]=>
142+
string(8) "majority"
143+
["wmajority"]=>
144+
bool(true)
145+
["wtimeout"]=>
146+
int(1000)
147+
["fsync"]=>
148+
NULL
149+
["journal"]=>
150+
NULL
151+
}
152+
array(5) {
153+
["w"]=>
154+
string(12) "customTagSet"
155+
["wmajority"]=>
156+
bool(false)
157+
["wtimeout"]=>
158+
int(1000)
159+
["fsync"]=>
160+
NULL
161+
["journal"]=>
162+
NULL
163+
}
164+
===DONE===

0 commit comments

Comments
 (0)