Skip to content

PHPC-283: UnexpectedValueException and bsonSerialize() cleanup #43

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
May 4, 2015
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
13 changes: 6 additions & 7 deletions src/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
}

if (Z_TYPE_P(obj_data) != IS_ARRAY) {
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data)));
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data)));
zval_ptr_dtor(&obj_data);

return;
Expand Down Expand Up @@ -767,7 +767,7 @@ void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char
PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *bson, bson_t **bson_out TSRMLS_DC) /* {{{ */
{
HashPosition pos;
HashTable *ht_data;
HashTable *ht_data = NULL;
zval *obj_data = NULL;

switch(Z_TYPE_P(data)) {
Expand All @@ -777,14 +777,13 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *

if (!obj_data) {
/* zend_call_method() failed */
return;
break;
}

if (Z_TYPE_P(obj_data) != IS_ARRAY) {
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data)));
zval_ptr_dtor(&obj_data);
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data)));

return;
break;
}

ht_data = HASH_OF(obj_data);
Expand All @@ -807,7 +806,7 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
return;
}

if (ht_data && ht_data->nApplyCount > 1) {
if (!ht_data || ht_data->nApplyCount > 1) {
if (obj_data) {
zval_ptr_dtor(&obj_data);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/bson/bson-encode_error-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $invalidValues = array(new stdClass, 'foo', 1, true);
foreach ($invalidValues as $invalidValue) {
try {
BSON\fromArray(new MyClass($invalidValue));
} catch (MongoDB\Driver\Exception\RuntimeException $e) {
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
echo $e->getMessage(), "\n";
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/bson/bson-encode_error-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $invalidValues = array(new stdClass, 'foo', 1, true);
foreach ($invalidValues as $invalidValue) {
try {
$bson = BSON\fromArray(array('embed' => new MyClass($invalidValue)));
} catch (MongoDB\Driver\Exception\RuntimeException $e) {
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
echo $e->getMessage(), "\n";
}
}
Expand Down