Skip to content

Commit 4b8ef30

Browse files
committed
PHPC-138: var_dump()ing WriteConcern
1 parent 4a20c63 commit 4b8ef30

File tree

2 files changed

+157
-8
lines changed

2 files changed

+157
-8
lines changed

src/MongoDB/WriteConcern.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
PHONGO_API zend_class_entry *php_phongo_writeconcern_ce;
4646

47+
zend_object_handlers php_phongo_handler_writeconcern;
48+
4749
#define PHONGO_WRITE_CONCERN_W_MAJORITY "majority"
4850

4951
/* {{{ proto MongoDB\Driver\WriteConcern WriteConcern::__construct(string $wstring[, integer $wtimeout[, boolean $journal[, boolean $fsync]]])
@@ -58,6 +60,7 @@ PHP_METHOD(WriteConcern, __construct)
5860
zend_bool journal = 0;
5961
zend_bool fsync = 0;
6062
long w;
63+
(void)return_value; (void)return_value_ptr; (void)return_value_used;
6164

6265

6366
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
@@ -87,12 +90,12 @@ PHP_METHOD(WriteConcern, __construct)
8790
if (fsync) {
8891
mongoc_write_concern_set_fsync(intern->write_concern, true);
8992
}
90-
// fallthrough
93+
/* fallthrough */
9194
case 3:
9295
if (journal) {
9396
mongoc_write_concern_set_journal(intern->write_concern, true);
9497
}
95-
// fallthrough
98+
/* fallthrough */
9699
case 2:
97100
if (wtimeout > 0) {
98101
mongoc_write_concern_set_wtimeout(intern->write_concern, wtimeout);
@@ -138,32 +141,61 @@ static void php_phongo_writeconcern_free_object(void *object TSRMLS_DC) /* {{{ *
138141
zend_object_value php_phongo_writeconcern_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
139142
{
140143
zend_object_value retval;
141-
php_phongo_writeconcern_t *intern;
144+
php_phongo_writeconcern_t *intern = NULL;
142145

143-
intern = (php_phongo_writeconcern_t *)emalloc(sizeof(php_phongo_writeconcern_t));
144-
memset(intern, 0, sizeof(php_phongo_writeconcern_t));
146+
intern = (php_phongo_writeconcern_t *)ecalloc(1, sizeof *intern);
145147

146148
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
147149
object_properties_init(&intern->std, class_type);
148150

149151
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_writeconcern_free_object, NULL TSRMLS_CC);
150-
retval.handlers = phongo_get_std_object_handlers();
152+
retval.handlers = &php_phongo_handler_writeconcern;
151153

152154
return retval;
153155
} /* }}} */
156+
157+
HashTable *php_phongo_writeconcern_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
158+
{
159+
zval retval = zval_used_for_init;
160+
mongoc_write_concern_t *write_concern = phongo_write_concern_from_zval(object TSRMLS_CC);
161+
char *wtag;
162+
163+
164+
*is_temp = 1;
165+
array_init(&retval);
166+
167+
wtag = (char *)mongoc_write_concern_get_wtag(write_concern);
168+
if (wtag) {
169+
add_assoc_string_ex(&retval, ZEND_STRS("w"), wtag, 1);
170+
} else {
171+
if (!mongoc_write_concern_get_wmajority(write_concern)) {
172+
add_assoc_long_ex(&retval, ZEND_STRS("w"), mongoc_write_concern_get_w(write_concern));
173+
}
174+
}
175+
add_assoc_bool_ex(&retval, ZEND_STRS("wmajority"), mongoc_write_concern_get_wmajority(write_concern));
176+
add_assoc_long_ex(&retval, ZEND_STRS("wtimeout"), mongoc_write_concern_get_wtimeout(write_concern));
177+
add_assoc_bool_ex(&retval, ZEND_STRS("fsync"), mongoc_write_concern_get_fsync(write_concern));
178+
add_assoc_bool_ex(&retval, ZEND_STRS("journal"), mongoc_write_concern_get_journal(write_concern));
179+
180+
181+
return Z_ARRVAL(retval);
182+
} /* }}} */
154183
/* }}} */
155184

156185
/* {{{ PHP_MINIT_FUNCTION */
157186
PHP_MINIT_FUNCTION(WriteConcern)
158187
{
159-
(void)type; /* We don't care if we are loaded via dl() or extension= */
188+
(void)type; (void)module_number;
160189
zend_class_entry ce;
161190

162191
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "WriteConcern", php_phongo_writeconcern_me);
163-
ce.create_object = php_phongo_writeconcern_create_object;
164192
php_phongo_writeconcern_ce = zend_register_internal_class(&ce TSRMLS_CC);
193+
php_phongo_writeconcern_ce->create_object = php_phongo_writeconcern_create_object;
165194
php_phongo_writeconcern_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
166195

196+
memcpy(&php_phongo_handler_writeconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
197+
php_phongo_handler_writeconcern.get_debug_info = php_phongo_writeconcern_get_debug_info;
198+
167199
zend_declare_class_constant_stringl(php_phongo_writeconcern_ce, ZEND_STRL("MAJORITY"), ZEND_STRL(PHONGO_WRITE_CONCERN_W_MAJORITY) TSRMLS_CC);
168200

169201
return SUCCESS;

tests/writeConcern/writeconcern-001.phpt

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ $w9 = new MongoDB\Driver\WriteConcern("string", 3000, true, false);
2525
$w10= new MongoDB\Driver\WriteConcern("string", 3000, false, true);
2626

2727

28+
var_dump($w, $w2, $w3, $w4, $w5, $w6, $w7, $w8, $w9, $w10);
2829

2930
try {
3031
new MongoDB\Driver\WriteConcern("string", 3000, false, true, 1);
@@ -36,5 +37,121 @@ try {
3637
===DONE===
3738
<?php exit(0); ?>
3839
--EXPECTF--
40+
object(MongoDB\Driver\WriteConcern)#%d (%d) {
41+
["wmajority"]=>
42+
bool(true)
43+
["wtimeout"]=>
44+
int(0)
45+
["fsync"]=>
46+
bool(false)
47+
["journal"]=>
48+
bool(false)
49+
}
50+
object(MongoDB\Driver\WriteConcern)#%d (%d) {
51+
["wmajority"]=>
52+
bool(true)
53+
["wtimeout"]=>
54+
int(1000)
55+
["fsync"]=>
56+
bool(false)
57+
["journal"]=>
58+
bool(false)
59+
}
60+
object(MongoDB\Driver\WriteConcern)#%d (%d) {
61+
["w"]=>
62+
int(2)
63+
["wmajority"]=>
64+
bool(false)
65+
["wtimeout"]=>
66+
int(0)
67+
["fsync"]=>
68+
bool(false)
69+
["journal"]=>
70+
bool(false)
71+
}
72+
object(MongoDB\Driver\WriteConcern)#%d (%d) {
73+
["w"]=>
74+
int(2)
75+
["wmajority"]=>
76+
bool(false)
77+
["wtimeout"]=>
78+
int(2000)
79+
["fsync"]=>
80+
bool(false)
81+
["journal"]=>
82+
bool(false)
83+
}
84+
object(MongoDB\Driver\WriteConcern)#%d (%d) {
85+
["w"]=>
86+
string(7) "tagname"
87+
["wmajority"]=>
88+
bool(false)
89+
["wtimeout"]=>
90+
int(0)
91+
["fsync"]=>
92+
bool(false)
93+
["journal"]=>
94+
bool(false)
95+
}
96+
object(MongoDB\Driver\WriteConcern)#%d (%d) {
97+
["w"]=>
98+
string(6) "string"
99+
["wmajority"]=>
100+
bool(false)
101+
["wtimeout"]=>
102+
int(3000)
103+
["fsync"]=>
104+
bool(false)
105+
["journal"]=>
106+
bool(false)
107+
}
108+
object(MongoDB\Driver\WriteConcern)#%d (%d) {
109+
["w"]=>
110+
string(6) "string"
111+
["wmajority"]=>
112+
bool(false)
113+
["wtimeout"]=>
114+
int(3000)
115+
["fsync"]=>
116+
bool(false)
117+
["journal"]=>
118+
bool(false)
119+
}
120+
object(MongoDB\Driver\WriteConcern)#10 (5) {
121+
["w"]=>
122+
string(6) "string"
123+
["wmajority"]=>
124+
bool(false)
125+
["wtimeout"]=>
126+
int(3000)
127+
["fsync"]=>
128+
bool(true)
129+
["journal"]=>
130+
bool(true)
131+
}
132+
object(MongoDB\Driver\WriteConcern)#11 (5) {
133+
["w"]=>
134+
string(6) "string"
135+
["wmajority"]=>
136+
bool(false)
137+
["wtimeout"]=>
138+
int(3000)
139+
["fsync"]=>
140+
bool(false)
141+
["journal"]=>
142+
bool(true)
143+
}
144+
object(MongoDB\Driver\WriteConcern)#12 (5) {
145+
["w"]=>
146+
string(6) "string"
147+
["wmajority"]=>
148+
bool(false)
149+
["wtimeout"]=>
150+
int(3000)
151+
["fsync"]=>
152+
bool(true)
153+
["journal"]=>
154+
bool(false)
155+
}
39156
MongoDB\Driver\WriteConcern::__construct() expects at most 4 parameters, 5 given
40157
===DONE===

0 commit comments

Comments
 (0)