Skip to content

Commit cf368d1

Browse files
committed
Merge pull request #103
2 parents 98a0ba0 + 19034f9 commit cf368d1

8 files changed

+221
-26
lines changed

src/bson.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -599,15 +599,7 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
599599
{
600600
bson_t child;
601601

602-
if (Z_TYPE_P(object) != IS_OBJECT || instanceof_function(Z_OBJCE_P(object), zend_standard_class_def TSRMLS_CC)) {
603-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding as-if was stdclass");
604-
bson_append_document_begin(bson, key, key_len, &child);
605-
zval_to_bson(object, flags, &child, NULL TSRMLS_CC);
606-
bson_append_document_end(bson, &child);
607-
return;
608-
}
609-
610-
if (instanceof_function(Z_OBJCE_P(object), php_phongo_type_ce TSRMLS_CC)) {
602+
if (Z_TYPE_P(object) == IS_OBJECT && instanceof_function(Z_OBJCE_P(object), php_phongo_type_ce TSRMLS_CC)) {
611603
if (instanceof_function(Z_OBJCE_P(object), php_phongo_serializable_ce TSRMLS_CC)) {
612604
zval *obj_data = NULL;
613605
bson_t child;
@@ -660,7 +652,7 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
660652
if (instanceof_function(Z_OBJCE_P(object), php_phongo_objectid_ce TSRMLS_CC)) {
661653
bson_oid_t oid;
662654

663-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding _id");
655+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding ObjectId");
664656
php_phongo_objectid_get_id(object, &oid TSRMLS_CC);
665657
bson_append_oid(bson, key, key_len, &oid);
666658
return;
@@ -687,10 +679,10 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
687679
}
688680
if (instanceof_function(Z_OBJCE_P(object), php_phongo_javascript_ce TSRMLS_CC)) {
689681
if (php_phongo_javascript_has_scope(object TSRMLS_CC)) {
690-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript w/scope");
682+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript with scope");
691683
bson_append_code(bson, key, key_len, php_phongo_javascript_get_javascript(object TSRMLS_CC));
692684
} else {
693-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript wo/scope");
685+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript without scope");
694686
bson_append_code_with_scope(bson, key, key_len, php_phongo_javascript_get_javascript(object TSRMLS_CC), php_phongo_javascript_get_scope(object TSRMLS_CC));
695687
}
696688
return;
@@ -716,11 +708,14 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
716708
bson_append_minkey(bson, key, key_len);
717709
return;
718710
}
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;
719714
}
720715

721-
/* Even if we don't know how to encode the object, ensure that we at least
722-
* create an empty BSON document. */
716+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding document");
723717
bson_append_document_begin(bson, key, key_len, &child);
718+
zval_to_bson(object, flags, &child, NULL TSRMLS_CC);
724719
bson_append_document_end(bson, &child);
725720
}
726721
void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char *key, long key_len, int entry_type, zval *entry TSRMLS_DC)
@@ -810,6 +805,10 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
810805
}
811806
}
812807

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+
813812
break;
814813
}
815814
/* break intentionally omitted */

tests/bson/bson-fromPHP-002.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
BSON\fromPHP(): Encoding non-Persistable objects as a document
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 MyDocument {
14+
private $foo = 1;
15+
protected $bar = 2;
16+
public $baz = 3;
17+
}
18+
19+
$s = fromPHP(new MyDocument);
20+
echo "Test ", toJSON($s), "\n";
21+
hex_dump($s);
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECT--
27+
Test { "foo" : 1, "bar" : 2, "baz" : 3 }
28+
0 : 20 00 00 00 10 66 6f 6f 00 01 00 00 00 10 62 61 [ ....foo......ba]
29+
10 : 72 00 02 00 00 00 10 62 61 7a 00 03 00 00 00 00 [r......baz......]
30+
===DONE===

tests/bson/bson-fromPHP-003.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
BSON\fromPHP(): Encoding non-Persistable objects as a document field value
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 MyDocument {
14+
private $foo = 1;
15+
protected $bar = 2;
16+
public $baz = 3;
17+
}
18+
19+
$tests = array(
20+
array(new BSON\UTCDateTime('1416445411987')),
21+
array('x' => new BSON\UTCDateTime('1416445411987')),
22+
array(new MyDocument),
23+
array('x' => new MyDocument),
24+
);
25+
26+
foreach ($tests as $document) {
27+
$s = fromPHP($document);
28+
echo "Test ", toJSON($s), "\n";
29+
hex_dump($s);
30+
}
31+
32+
?>
33+
===DONE===
34+
<?php exit(0); ?>
35+
--EXPECT--
36+
Test { "0" : { "$date" : 1416445411987 } }
37+
0 : 10 00 00 00 09 30 00 93 c2 b9 ca 49 01 00 00 00 [.....0.....I....]
38+
Test { "x" : { "$date" : 1416445411987 } }
39+
0 : 10 00 00 00 09 78 00 93 c2 b9 ca 49 01 00 00 00 [.....x.....I....]
40+
Test { "0" : { "foo" : 1, "bar" : 2, "baz" : 3 } }
41+
0 : 28 00 00 00 03 30 00 20 00 00 00 10 66 6f 6f 00 [(....0. ....foo.]
42+
10 : 01 00 00 00 10 62 61 72 00 02 00 00 00 10 62 61 [.....bar......ba]
43+
20 : 7a 00 03 00 00 00 00 00 [z.......]
44+
Test { "x" : { "foo" : 1, "bar" : 2, "baz" : 3 } }
45+
0 : 28 00 00 00 03 78 00 20 00 00 00 10 66 6f 6f 00 [(....x. ....foo.]
46+
10 : 01 00 00 00 10 62 61 72 00 02 00 00 00 10 62 61 [.....bar......ba]
47+
20 : 7a 00 03 00 00 00 00 00 [z.......]
48+
===DONE===
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===
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===

tests/bson/bson-utcdatetime-001.phpt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,9 @@ $query = new MongoDB\Driver\Query(array('_id' => 1));
1919
$cursor = $manager->executeQuery(NS, $query);
2020
$results = iterator_to_array($cursor);
2121

22-
$datetime = $utcdatetime->toDateTime();
23-
var_dump($datetime->format(DATE_RSS));
24-
var_dump((string) $utcdatetime);
25-
2622
$tests = array(
2723
array($utcdatetime),
2824
array($results[0]->x),
29-
array($datetime),
3025
);
3126

3227
foreach($tests as $n => $test) {
@@ -40,9 +35,7 @@ foreach($tests as $n => $test) {
4035
?>
4136
===DONE===
4237
<?php exit(0); ?>
43-
--EXPECTF--
44-
string(31) "Thu, 20 Nov 2014 01:03:31 +0000"
45-
string(13) "1416445411987"
38+
--EXPECT--
4639
Test#0 { "0" : { "$date" : 1416445411987 } }
4740
string(37) "{ "0" : { "$date" : 1416445411987 } }"
4841
string(37) "{ "0" : { "$date" : 1416445411987 } }"
@@ -51,8 +44,4 @@ Test#1 { "0" : { "$date" : 1416445411987 } }
5144
string(37) "{ "0" : { "$date" : 1416445411987 } }"
5245
string(37) "{ "0" : { "$date" : 1416445411987 } }"
5346
bool(true)
54-
Test#2 { "0" : { } }
55-
string(14) "{ "0" : { } }"
56-
string(14) "{ "0" : { } }"
57-
bool(false)
5847
===DONE===
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
BSON BSON\UTCDateTime::toDateTime()
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+
$utcdatetime = new BSON\UTCDateTime("1416445411987");
14+
$datetime = $utcdatetime->toDateTime();
15+
var_dump($datetime->format(DATE_RSS));
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECTF--
21+
string(31) "Thu, 20 Nov 2014 01:03:31 +0000"
22+
===DONE===
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
BSON BSON\UTCDateTime::__toString()
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+
$utcdatetime = new BSON\UTCDateTime("1416445411987");
14+
var_dump((string) $utcdatetime);
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECTF--
20+
string(13) "1416445411987"
21+
===DONE===

0 commit comments

Comments
 (0)