Skip to content

Commit 37810d4

Browse files
committed
PHPC-410: object_to_bson() should throw on unsupported BSON\Type objects
1 parent 8ab4acc commit 37810d4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/bson.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
708708
bson_append_minkey(bson, key, key_len);
709709
return;
710710
}
711+
712+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Unexpected %s instance: %s", php_phongo_type_ce->name, Z_OBJCE_P(object)->name);
713+
return;
711714
}
712715

713716
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding document");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--o
2+
BSON\fromPHP(): Encoding unknown Type objects as a document field value
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
use MongoDB\BSON as BSON;
8+
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
class UnknownType implements BSON\Type {}
12+
13+
$tests = array(
14+
array(new UnknownType()),
15+
array('x' => new UnknownType()),
16+
);
17+
18+
foreach ($tests as $document) {
19+
echo throws(function() use ($document) {
20+
fromPHP($document);
21+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
22+
}
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECTF--
28+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
29+
Unexpected %SBSON\Type instance: UnknownType
30+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
31+
Unexpected %SBSON\Type instance: UnknownType
32+
===DONE===

0 commit comments

Comments
 (0)