Skip to content

Commit 9c38811

Browse files
committed
Merge pull request #109
2 parents e30b93b + 3f8b851 commit 9c38811

7 files changed

+101
-18
lines changed

src/MongoDB/WriteConcern.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,18 @@ PHP_METHOD(WriteConcern, __construct)
5353
{
5454
php_phongo_writeconcern_t *intern;
5555
zend_error_handling error_handling;
56-
char *wstring;
57-
int wstring_len;
56+
zval *w;
5857
long wtimeout = 0;
5958
zend_bool journal = 0;
6059
zend_bool fsync = 0;
61-
long w;
60+
6261
(void)return_value; (void)return_value_ptr; (void)return_value_used;
6362

6463

6564
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
6665
intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
6766

68-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lbb", &wstring, &wstring_len, &wtimeout, &journal, &fsync) == FAILURE) {
67+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|lbb", &w, &wtimeout, &journal, &fsync) == FAILURE) {
6968
zend_restore_error_handling(&error_handling TSRMLS_CC);
7069
return;
7170
}
@@ -74,14 +73,21 @@ PHP_METHOD(WriteConcern, __construct)
7473

7574
intern->write_concern = mongoc_write_concern_new();
7675

77-
if (IS_LONG == is_numeric_string(wstring, wstring_len, &w, NULL, 0)) {
78-
mongoc_write_concern_set_w(intern->write_concern, w);
79-
} else {
80-
if (strcmp(wstring, PHONGO_WRITE_CONCERN_W_MAJORITY) == 0) {
76+
if (Z_TYPE_P(w) == IS_LONG) {
77+
if (Z_LVAL_P(w) < -3) {
78+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be >= -3, %ld given", Z_LVAL_P(w));
79+
return;
80+
}
81+
mongoc_write_concern_set_w(intern->write_concern, Z_LVAL_P(w));
82+
} else if (Z_TYPE_P(w) == IS_STRING) {
83+
if (strcmp(Z_STRVAL_P(w), PHONGO_WRITE_CONCERN_W_MAJORITY) == 0) {
8184
mongoc_write_concern_set_w(intern->write_concern, MONGOC_WRITE_CONCERN_W_MAJORITY);
8285
} else {
83-
mongoc_write_concern_set_wtag(intern->write_concern, wstring);
86+
mongoc_write_concern_set_wtag(intern->write_concern, Z_STRVAL_P(w));
8487
}
88+
} else {
89+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(w)));
90+
return;
8591
}
8692

8793
switch(ZEND_NUM_ARGS()) {
@@ -92,9 +98,12 @@ PHP_METHOD(WriteConcern, __construct)
9298
mongoc_write_concern_set_journal(intern->write_concern, journal);
9399
/* fallthrough */
94100
case 2:
95-
if (wtimeout > 0) {
96-
mongoc_write_concern_set_wtimeout(intern->write_concern, wtimeout);
101+
if (wtimeout < 0) {
102+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected wtimeout to be >= 0, %ld given", wtimeout);
103+
return;
97104
}
105+
106+
mongoc_write_concern_set_wtimeout(intern->write_concern, wtimeout);
98107
}
99108
}
100109
/* }}} */

tests/writeConcern/writeconcern-ctor_error-001.phpt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ MongoDB\Driver\WriteConcern construction (invalid arguments)
77
<?php
88
require_once __DIR__ . "/../utils/basic.inc";
99

10-
try {
10+
echo throws(function() {
1111
new MongoDB\Driver\WriteConcern("string", 10000, false, true, 1);
12-
} catch(InvalidArgumentException $e) {
13-
echo $e->getMessage(), "\n";
14-
}
12+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
1513

1614
?>
1715
===DONE===
1816
<?php exit(0); ?>
19-
--EXPECTF--
17+
--EXPECT--
18+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
2019
MongoDB\Driver\WriteConcern::__construct() expects at most 4 parameters, 5 given
2120
===DONE===
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern construction (invalid w type)
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+
1.0,
11+
true,
12+
array(),
13+
new stdClass,
14+
null,
15+
);
16+
17+
foreach ($tests as $test) {
18+
echo throws(function() use ($test) {
19+
new MongoDB\Driver\WriteConcern($test);
20+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
21+
}
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECTF--
27+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
28+
Expected w to be integer or string, double given
29+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
30+
Expected w to be integer or string, boolean given
31+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
32+
Expected w to be integer or string, array given
33+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
34+
Expected w to be integer or string, object given
35+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
36+
Expected w to be integer or string, %r(null|NULL)%r given
37+
===DONE===
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern construction (invalid w range)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
echo throws(function() {
10+
new MongoDB\Driver\WriteConcern(-4);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
18+
Expected w to be >= -3, -4 given
19+
===DONE===
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern construction (invalid wtimeout range)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
echo throws(function() {
10+
new MongoDB\Driver\WriteConcern(1, -1);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
18+
Expected wtimeout to be >= 0, -1 given
19+
===DONE===

tests/writeConcern/writeconcern-getw-001.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ $tests = array(
1515
1,
1616
2,
1717
'tag',
18+
'2',
1819
);
1920

2021
foreach ($tests as $test) {
@@ -34,4 +35,5 @@ int(0)
3435
int(1)
3536
int(2)
3637
string(3) "tag"
38+
string(1) "2"
3739
===DONE===

tests/writeConcern/writeconcern-getwtimeout-001.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ MongoDB\Driver\WriteConcern::getWtimeout()
77
require_once __DIR__ . "/../utils/basic.inc";
88

99
$tests = array(
10-
-1,
1110
0,
1211
1,
1312
);
@@ -26,7 +25,6 @@ var_dump($wc->getWtimeout());
2625
<?php exit(0); ?>
2726
--EXPECT--
2827
int(0)
29-
int(0)
3028
int(1)
3129
int(0)
3230
===DONE===

0 commit comments

Comments
 (0)