Skip to content
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
27 changes: 13 additions & 14 deletions src/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,15 +599,7 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
{
bson_t child;

if (Z_TYPE_P(object) != IS_OBJECT || instanceof_function(Z_OBJCE_P(object), zend_standard_class_def TSRMLS_CC)) {
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding as-if was stdclass");
bson_append_document_begin(bson, key, key_len, &child);
zval_to_bson(object, flags, &child, NULL TSRMLS_CC);
bson_append_document_end(bson, &child);
return;
}

if (instanceof_function(Z_OBJCE_P(object), php_phongo_type_ce TSRMLS_CC)) {
if (Z_TYPE_P(object) == IS_OBJECT && instanceof_function(Z_OBJCE_P(object), php_phongo_type_ce TSRMLS_CC)) {
if (instanceof_function(Z_OBJCE_P(object), php_phongo_serializable_ce TSRMLS_CC)) {
zval *obj_data = NULL;
bson_t child;
Expand Down Expand Up @@ -660,7 +652,7 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
if (instanceof_function(Z_OBJCE_P(object), php_phongo_objectid_ce TSRMLS_CC)) {
bson_oid_t oid;

mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding _id");
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding ObjectId");
php_phongo_objectid_get_id(object, &oid TSRMLS_CC);
bson_append_oid(bson, key, key_len, &oid);
return;
Expand All @@ -687,10 +679,10 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
}
if (instanceof_function(Z_OBJCE_P(object), php_phongo_javascript_ce TSRMLS_CC)) {
if (php_phongo_javascript_has_scope(object TSRMLS_CC)) {
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript w/scope");
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript with scope");
bson_append_code(bson, key, key_len, php_phongo_javascript_get_javascript(object TSRMLS_CC));
} else {
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript wo/scope");
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript without scope");
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));
}
return;
Expand All @@ -716,11 +708,14 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
bson_append_minkey(bson, key, key_len);
return;
}

phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Unexpected %s instance: %s", php_phongo_type_ce->name, Z_OBJCE_P(object)->name);
return;
}

/* Even if we don't know how to encode the object, ensure that we at least
* create an empty BSON document. */
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding document");
bson_append_document_begin(bson, key, key_len, &child);
zval_to_bson(object, flags, &child, NULL TSRMLS_CC);
bson_append_document_end(bson, &child);
}
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)
Expand Down Expand Up @@ -810,6 +805,10 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
}
}

break;
} else if (instanceof_function(Z_OBJCE_P(data), php_phongo_type_ce TSRMLS_CC)) {
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "%s cannot be serialized as a root element", Z_OBJCE_P(data)->name);

break;
}
/* break intentionally omitted */
Expand Down
30 changes: 30 additions & 0 deletions tests/bson/bson-fromPHP-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
BSON\fromPHP(): Encoding non-Persistable objects as a document
--INI--
date.timezone=America/Los_Angeles
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
use MongoDB\BSON as BSON;

require_once __DIR__ . "/../utils/basic.inc";

class MyDocument {
private $foo = 1;
protected $bar = 2;
public $baz = 3;
}

$s = fromPHP(new MyDocument);
echo "Test ", toJSON($s), "\n";
hex_dump($s);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Test { "foo" : 1, "bar" : 2, "baz" : 3 }
0 : 20 00 00 00 10 66 6f 6f 00 01 00 00 00 10 62 61 [ ....foo......ba]
10 : 72 00 02 00 00 00 10 62 61 7a 00 03 00 00 00 00 [r......baz......]
===DONE===
48 changes: 48 additions & 0 deletions tests/bson/bson-fromPHP-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
BSON\fromPHP(): Encoding non-Persistable objects as a document field value
--INI--
date.timezone=America/Los_Angeles
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
use MongoDB\BSON as BSON;

require_once __DIR__ . "/../utils/basic.inc";

class MyDocument {
private $foo = 1;
protected $bar = 2;
public $baz = 3;
}

$tests = array(
array(new BSON\UTCDateTime('1416445411987')),
array('x' => new BSON\UTCDateTime('1416445411987')),
array(new MyDocument),
array('x' => new MyDocument),
);

foreach ($tests as $document) {
$s = fromPHP($document);
echo "Test ", toJSON($s), "\n";
hex_dump($s);
}

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Test { "0" : { "$date" : 1416445411987 } }
0 : 10 00 00 00 09 30 00 93 c2 b9 ca 49 01 00 00 00 [.....0.....I....]
Test { "x" : { "$date" : 1416445411987 } }
0 : 10 00 00 00 09 78 00 93 c2 b9 ca 49 01 00 00 00 [.....x.....I....]
Test { "0" : { "foo" : 1, "bar" : 2, "baz" : 3 } }
0 : 28 00 00 00 03 30 00 20 00 00 00 10 66 6f 6f 00 [(....0. ....foo.]
10 : 01 00 00 00 10 62 61 72 00 02 00 00 00 10 62 61 [.....bar......ba]
20 : 7a 00 03 00 00 00 00 00 [z.......]
Test { "x" : { "foo" : 1, "bar" : 2, "baz" : 3 } }
0 : 28 00 00 00 03 78 00 20 00 00 00 10 66 6f 6f 00 [(....x. ....foo.]
10 : 01 00 00 00 10 62 61 72 00 02 00 00 00 10 62 61 [.....bar......ba]
20 : 7a 00 03 00 00 00 00 00 [z.......]
===DONE===
32 changes: 32 additions & 0 deletions tests/bson/bson-fromPHP_error-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--o
BSON\fromPHP(): Encoding unknown Type objects as a document field value
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
use MongoDB\BSON as BSON;

require_once __DIR__ . "/../utils/basic.inc";

class UnknownType implements BSON\Type {}

$tests = array(
array(new UnknownType()),
array('x' => new UnknownType()),
);

foreach ($tests as $document) {
echo throws(function() use ($document) {
fromPHP($document);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Unexpected %SBSON\Type instance: UnknownType
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Unexpected %SBSON\Type instance: UnknownType
===DONE===
54 changes: 54 additions & 0 deletions tests/bson/bson-fromPHP_error-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
BSON\fromPHP(): Encoding non-Serializable Type objects as a root element
--INI--
date.timezone=America/Los_Angeles
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
use MongoDB\BSON as BSON;

require_once __DIR__ . "/../utils/basic.inc";

class UnknownType implements BSON\Type {}

$tests = array(
new UnknownType,
new BSON\Binary('foobar', BSON\Binary::TYPE_GENERIC),
new BSON\Javascript('function foo(bar) {var baz = bar; var bar = foo; return bar; }'),
new BSON\MinKey,
new BSON\MaxKey,
new BSON\ObjectId,
new BSON\Regex('regexp', 'i'),
new BSON\Timestamp(1234, 5678),
new BSON\UTCDateTime('1416445411987'),
);

foreach ($tests as $document) {
echo throws(function() use ($document) {
fromPHP($document);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
UnknownType cannot be serialized as a root element
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
%S\BSON\Binary cannot be serialized as a root element
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
%S\BSON\Javascript cannot be serialized as a root element
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
%S\BSON\MinKey cannot be serialized as a root element
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
%S\BSON\MaxKey cannot be serialized as a root element
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
%S\BSON\ObjectID cannot be serialized as a root element
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
%S\BSON\Regex cannot be serialized as a root element
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
%S\BSON\Timestamp cannot be serialized as a root element
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
%S\BSON\UTCDateTime cannot be serialized as a root element
===DONE===
13 changes: 1 addition & 12 deletions tests/bson/bson-utcdatetime-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ $query = new MongoDB\Driver\Query(array('_id' => 1));
$cursor = $manager->executeQuery(NS, $query);
$results = iterator_to_array($cursor);

$datetime = $utcdatetime->toDateTime();
var_dump($datetime->format(DATE_RSS));
var_dump((string) $utcdatetime);

$tests = array(
array($utcdatetime),
array($results[0]->x),
array($datetime),
);

foreach($tests as $n => $test) {
Expand All @@ -40,9 +35,7 @@ foreach($tests as $n => $test) {
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
string(31) "Thu, 20 Nov 2014 01:03:31 +0000"
string(13) "1416445411987"
--EXPECT--
Test#0 { "0" : { "$date" : 1416445411987 } }
string(37) "{ "0" : { "$date" : 1416445411987 } }"
string(37) "{ "0" : { "$date" : 1416445411987 } }"
Expand All @@ -51,8 +44,4 @@ Test#1 { "0" : { "$date" : 1416445411987 } }
string(37) "{ "0" : { "$date" : 1416445411987 } }"
string(37) "{ "0" : { "$date" : 1416445411987 } }"
bool(true)
Test#2 { "0" : { } }
string(14) "{ "0" : { } }"
string(14) "{ "0" : { } }"
bool(false)
===DONE===
22 changes: 22 additions & 0 deletions tests/bson/bson-utcdatetime-todatetime-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
BSON BSON\UTCDateTime::toDateTime()
--INI--
date.timezone=America/Los_Angeles
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
--FILE--
<?php
use MongoDB\BSON as BSON;

require_once __DIR__ . "/../utils/basic.inc";

$utcdatetime = new BSON\UTCDateTime("1416445411987");
$datetime = $utcdatetime->toDateTime();
var_dump($datetime->format(DATE_RSS));

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
string(31) "Thu, 20 Nov 2014 01:03:31 +0000"
===DONE===
21 changes: 21 additions & 0 deletions tests/bson/bson-utcdatetime-tostring-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
BSON BSON\UTCDateTime::__toString()
--INI--
date.timezone=America/Los_Angeles
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
--FILE--
<?php
use MongoDB\BSON as BSON;

require_once __DIR__ . "/../utils/basic.inc";

$utcdatetime = new BSON\UTCDateTime("1416445411987");
var_dump((string) $utcdatetime);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
string(13) "1416445411987"
===DONE===