Skip to content

Commit 319d748

Browse files
committed
PHPC-410: zval_to_bson() should throw on non-Serializable Type objects
1 parent 37810d4 commit 319d748

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/bson.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,10 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
805805
}
806806
}
807807

808+
break;
809+
} else if (instanceof_function(Z_OBJCE_P(data), php_phongo_type_ce TSRMLS_CC)) {
810+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "%s cannot be serialized as a root element", Z_OBJCE_P(data)->name);
811+
808812
break;
809813
}
810814
/* break intentionally omitted */
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
BSON\fromPHP(): Encoding non-Serializable Type objects as a root element
3+
--INI--
4+
date.timezone=America/Los_Angeles
5+
--SKIPIF--
6+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
7+
--FILE--
8+
<?php
9+
use MongoDB\BSON as BSON;
10+
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
class UnknownType implements BSON\Type {}
14+
15+
$tests = array(
16+
new UnknownType,
17+
new BSON\Binary('foobar', BSON\Binary::TYPE_GENERIC),
18+
new BSON\Javascript('function foo(bar) {var baz = bar; var bar = foo; return bar; }'),
19+
new BSON\MinKey,
20+
new BSON\MaxKey,
21+
new BSON\ObjectId,
22+
new BSON\Regex('regexp', 'i'),
23+
new BSON\Timestamp(1234, 5678),
24+
new BSON\UTCDateTime('1416445411987'),
25+
);
26+
27+
foreach ($tests as $document) {
28+
echo throws(function() use ($document) {
29+
fromPHP($document);
30+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";}
31+
32+
?>
33+
===DONE===
34+
<?php exit(0); ?>
35+
--EXPECTF--
36+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
37+
UnknownType cannot be serialized as a root element
38+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
39+
%S\BSON\Binary cannot be serialized as a root element
40+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
41+
%S\BSON\Javascript cannot be serialized as a root element
42+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
43+
%S\BSON\MinKey cannot be serialized as a root element
44+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
45+
%S\BSON\MaxKey cannot be serialized as a root element
46+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
47+
%S\BSON\ObjectID cannot be serialized as a root element
48+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
49+
%S\BSON\Regex cannot be serialized as a root element
50+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
51+
%S\BSON\Timestamp cannot be serialized as a root element
52+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
53+
%S\BSON\UTCDateTime cannot be serialized as a root element
54+
===DONE===

0 commit comments

Comments
 (0)