Skip to content

Commit 3cd5857

Browse files
committed
PHPC-741: Consistent exceptions for Javascript init methods
1 parent e38c21d commit 3cd5857

6 files changed

+85
-9
lines changed

src/BSON/Javascript.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ PHONGO_API zend_class_entry *php_phongo_javascript_ce;
4646

4747
zend_object_handlers php_phongo_handler_javascript;
4848

49-
/* Initialize the object from a string and return whether it was successful. */
49+
/* Initialize the object and return whether it was successful. An exception will
50+
* be thrown on error. */
5051
static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const char *code, phongo_zpp_char_len code_len, zval *scope TSRMLS_DC)
5152
{
5253
if (scope && Z_TYPE_P(scope) != IS_OBJECT && Z_TYPE_P(scope) != IS_ARRAY && Z_TYPE_P(scope) != IS_NULL) {
54+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected scope to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(scope)));
5355
return false;
5456
}
5557

@@ -66,7 +68,8 @@ static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const ch
6668
return true;
6769
}
6870

69-
/* Initialize the object from a HashTable and return whether it was successful. */
71+
/* Initialize the object from a HashTable and return whether it was successful.
72+
* An exception will be thrown on error. */
7073
static bool php_phongo_javascript_init_from_hash(php_phongo_javascript_t *intern, HashTable *props TSRMLS_DC)
7174
{
7275
#if PHP_VERSION_ID >= 70000
@@ -86,6 +89,8 @@ static bool php_phongo_javascript_init_from_hash(php_phongo_javascript_t *intern
8689
return php_phongo_javascript_init(intern, Z_STRVAL_PP(code), Z_STRLEN_PP(code), tmp TSRMLS_CC);
8790
}
8891
#endif
92+
93+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"code\" string field", ZSTR_VAL(php_phongo_javascript_ce->name));
8994
return false;
9095
}
9196

@@ -131,9 +136,7 @@ PHP_METHOD(Javascript, __set_state)
131136
intern = Z_JAVASCRIPT_OBJ_P(return_value);
132137
props = Z_ARRVAL_P(array);
133138

134-
if (!php_phongo_javascript_init_from_hash(intern, props TSRMLS_CC)) {
135-
php_error(E_ERROR, "Invalid serialization data for Javascript object");
136-
}
139+
php_phongo_javascript_init_from_hash(intern, props TSRMLS_CC);
137140
}
138141
/* }}} */
139142

@@ -151,10 +154,7 @@ PHP_METHOD(Javascript, __wakeup)
151154
intern = Z_JAVASCRIPT_OBJ_P(getThis());
152155
props = zend_std_get_properties(getThis() TSRMLS_CC);
153156

154-
if (!php_phongo_javascript_init_from_hash(intern, props TSRMLS_CC)) {
155-
php_error(E_ERROR, "Invalid serialization data for Javascript object");
156-
} else {
157-
}
157+
php_phongo_javascript_init_from_hash(intern, props TSRMLS_CC);
158158
}
159159
/* }}} */
160160

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\BSON\Javascript unserialization requires "code" string field
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+
unserialize('O:23:"MongoDB\BSON\Javascript":1:{s:4:"code";i:0;}');
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
18+
MongoDB\BSON\Javascript initialization requires "code" string field
19+
===DONE===
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\BSON\Javascript unserialization expects optional scope to be array or object
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+
unserialize('O:23:"MongoDB\BSON\Javascript":2:{s:4:"code";s:17:"function foo() {}";s:5:"scope";s:7:"INVALID";}');
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 scope to be array or object, string 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\BSON\Javascript::__set_state() requires "code" string field
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+
MongoDB\BSON\Javascript::__set_state(['code' => 0]);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
18+
MongoDB\BSON\Javascript initialization requires "code" string field
19+
===DONE===
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\BSON\Javascript::__set_state() expects optional scope to be array or object
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+
MongoDB\BSON\Javascript::__set_state(['code' => 'function foo() {}', 'scope' => 'INVALID']);
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 scope to be array or object, string given
19+
===DONE===

0 commit comments

Comments
 (0)