Skip to content

PHPC-550: Always encode ODS field when serializing Persistable documents #226

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 5 commits into from
Feb 22, 2016
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
4 changes: 1 addition & 3 deletions php_bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
typedef enum {
PHONGO_BSON_NONE = 0x00,
PHONGO_BSON_ADD_ID = 0x01,
PHONGO_BSON_RETURN_ID = 0x02,
PHONGO_BSON_ADD_ODS = 0x04,
PHONGO_BSON_ADD_CHILD_ODS = 0x08
PHONGO_BSON_RETURN_ID = 0x02
} php_phongo_bson_flags_t;

typedef enum {
Expand Down
1 change: 0 additions & 1 deletion src/MongoDB/BulkWrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ PHP_METHOD(BulkWrite, insert)
if (return_value_used) {
bson_flags |= PHONGO_BSON_RETURN_ID;
}
bson_flags |= PHONGO_BSON_ADD_ODS|PHONGO_BSON_ADD_CHILD_ODS;

bson = bson_new();
phongo_zval_to_bson(document, bson_flags, bson, &bson_out TSRMLS_CC);
Expand Down
18 changes: 7 additions & 11 deletions src/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,13 +877,11 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
#endif
bson_append_document_begin(bson, key, key_len, &child);
if (instanceof_function(Z_OBJCE_P(object), php_phongo_persistable_ce TSRMLS_CC)) {
if (flags & PHONGO_BSON_ADD_CHILD_ODS) {
#if PHP_VERSION_ID >= 70000
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name->val, Z_OBJCE_P(object)->name->len);
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name->val, Z_OBJCE_P(object)->name->len);
#else
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name, strlen(Z_OBJCE_P(object)->name));
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name, strlen(Z_OBJCE_P(object)->name));
#endif
}
}
#if PHP_VERSION_ID >= 70000
phongo_zval_to_bson(&obj_data, flags, &child, NULL TSRMLS_CC);
Expand Down Expand Up @@ -1161,15 +1159,13 @@ PHONGO_API void phongo_zval_to_bson(zval *data, php_phongo_bson_flags_t flags, b
#endif

if (instanceof_function(Z_OBJCE_P(data), php_phongo_persistable_ce TSRMLS_CC)) {
if (flags & PHONGO_BSON_ADD_ODS) {
#if PHP_VERSION_ID >= 70000
bson_append_binary(bson, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name->val, Z_OBJCE_P(data)->name->len);
zend_hash_str_del(ht_data, PHONGO_ODM_FIELD_NAME, sizeof(PHONGO_ODM_FIELD_NAME)-1);
bson_append_binary(bson, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name->val, Z_OBJCE_P(data)->name->len);
zend_hash_str_del(ht_data, PHONGO_ODM_FIELD_NAME, sizeof(PHONGO_ODM_FIELD_NAME)-1);
#else
bson_append_binary(bson, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name, strlen(Z_OBJCE_P(data)->name));
zend_hash_del(ht_data, PHONGO_ODM_FIELD_NAME, sizeof(PHONGO_ODM_FIELD_NAME));
bson_append_binary(bson, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name, strlen(Z_OBJCE_P(data)->name));
zend_hash_del(ht_data, PHONGO_ODM_FIELD_NAME, sizeof(PHONGO_ODM_FIELD_NAME));
#endif
}
}

break;
Expand Down Expand Up @@ -1459,7 +1455,7 @@ PHP_FUNCTION(fromPHP)
}

bson = bson_new();
phongo_zval_to_bson(data, PHONGO_BSON_ADD_ODS|PHONGO_BSON_ADD_CHILD_ODS, bson, NULL TSRMLS_CC);
phongo_zval_to_bson(data, PHONGO_BSON_NONE, bson, NULL TSRMLS_CC);

PHONGO_RETVAL_STRINGL((const char *) bson_get_data(bson), bson->len);
bson_destroy(bson);
Expand Down
2 changes: 2 additions & 0 deletions tests/bson/bson-ods-001.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
BSON encoding: Encoding data into BSON representation, and BSON into Extended JSON
--XFAIL--
Persistable::bsonSerialize() should not return atomic modifiers (see: PHPC-550)
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
Expand Down
2 changes: 2 additions & 0 deletions tests/bson/bson-ods-002.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
BSON encoding: Encoding data into BSON representation, and BSON into Extended JSON
--XFAIL--
Persistable::bsonSerialize() should not return atomic modifiers (see: PHPC-550)
--INI--
date.timezone=America/Los_Angeles
--SKIPIF--
Expand Down
84 changes: 84 additions & 0 deletions tests/bulk/bulkwrite-delete-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
--TEST--
MongoDB\Driver\BulkWrite::delete() should always encode __pclass for Persistable objects
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

use MongoDB\BSON as BSON;

class MyClass implements BSON\Persistable
{
private $id;
private $child;

public function __construct($id, MyClass $child = null)
{
$this->id = $id;
$this->child = $child;
}

public function bsonSerialize()
{
return [
'_id' => $this->id,
'child' => $this->child,
];
}

public function bsonUnserialize(array $data)
{
$this->id = $data['_id'];
$this->child = $data['child'];
}
}

$manager = new MongoDB\Driver\Manager(STANDALONE);

$document = new MyClass('foo', new MyClass('bar', new MyClass('baz')));

$bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert($document);
$result = $manager->executeBulkWrite(NS, $bulk);
printf("Inserted %d document(s)\n", $result->getInsertedCount());

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
var_dump($cursor->toArray());

$bulk = new MongoDB\Driver\BulkWrite();
$bulk->delete($document);
$result = $manager->executeBulkWrite(NS, $bulk);
printf("Deleted %d document(s)\n", $result->getDeletedCount());

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
var_dump($cursor->toArray());

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Inserted 1 document(s)
array(1) {
[0]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "foo"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "bar"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "baz"
["child":"MyClass":private]=>
NULL
}
}
}
}
Deleted 1 document(s)
array(0) {
}
===DONE===
71 changes: 71 additions & 0 deletions tests/bulk/bulkwrite-insert-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--TEST--
MongoDB\Driver\BulkWrite::insert() should always encode __pclass for Persistable objects
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

use MongoDB\BSON as BSON;

class MyClass implements BSON\Persistable
{
private $id;
private $child;

public function __construct($id, MyClass $child = null)
{
$this->id = $id;
$this->child = $child;
}

public function bsonSerialize()
{
return [
'_id' => $this->id,
'child' => $this->child,
];
}

public function bsonUnserialize(array $data)
{
$this->id = $data['_id'];
$this->child = $data['child'];
}
}

$manager = new MongoDB\Driver\Manager(STANDALONE);

$bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert(new MyClass('foo', new MyClass('bar', new MyClass('baz'))));
$result = $manager->executeBulkWrite(NS, $bulk);
printf("Inserted %d document(s)\n", $result->getInsertedCount());

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
var_dump($cursor->toArray());

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Inserted 1 document(s)
array(1) {
[0]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "foo"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "bar"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "baz"
["child":"MyClass":private]=>
NULL
}
}
}
}
===DONE===
108 changes: 108 additions & 0 deletions tests/bulk/bulkwrite-update-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
--TEST--
MongoDB\Driver\BulkWrite::update() should always encode __pclass for Persistable objects
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

use MongoDB\BSON as BSON;

class MyClass implements BSON\Persistable
{
private $id;
private $child;

public function __construct($id, MyClass $child = null)
{
$this->id = $id;
$this->child = $child;
}

public function bsonSerialize()
{
return [
'_id' => $this->id,
'child' => $this->child,
];
}

public function bsonUnserialize(array $data)
{
$this->id = $data['_id'];
$this->child = $data['child'];
}
}

$manager = new MongoDB\Driver\Manager(STANDALONE);

$document = new MyClass('foo', new MyClass('bar', new MyClass('baz')));

$bulk = new MongoDB\Driver\BulkWrite();
$bulk->update(
['_id' => 'foo'],
$document,
['upsert' => true]
);
$result = $manager->executeBulkWrite(NS, $bulk);
printf("Upserted %d document(s)\n", $result->getUpsertedCount());

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
var_dump($cursor->toArray());

$bulk = new MongoDB\Driver\BulkWrite();
$bulk->update(
$document,
['$set' => ['child' => new MyClass('yip', new MyClass('yap'))]]
);
$result = $manager->executeBulkWrite(NS, $bulk);
printf("Modified %d document(s)\n", $result->getModifiedCount());

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
var_dump($cursor->toArray());

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Upserted 1 document(s)
array(1) {
[0]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "foo"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "bar"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "baz"
["child":"MyClass":private]=>
NULL
}
}
}
}
Modified 1 document(s)
array(1) {
[0]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "foo"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "yip"
["child":"MyClass":private]=>
object(MyClass)#%d (%d) {
["id":"MyClass":private]=>
string(3) "yap"
["child":"MyClass":private]=>
NULL
}
}
}
}
===DONE===
2 changes: 1 addition & 1 deletion tests/bulk/write-0001.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
MongoDB\Driver\BulkWrite: #001 Variety Bulk
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
8 changes: 4 additions & 4 deletions tests/bulk/write-0002.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
MongoDB\Driver\BulkWrite: #002 Get the generated ID
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand All @@ -19,7 +19,7 @@ $w = 1;
$wtimeout = 1000;
$writeConcern = new \MongoDB\Driver\WriteConcern($w, $wtimeout);
var_dump($insertBulk);
$result = $manager->executeBulkWrite("db.collection", $insertBulk, $writeConcern);
$result = $manager->executeBulkWrite(NS, $insertBulk, $writeConcern);
var_dump($insertBulk);

assert($result instanceof \MongoDB\Driver\WriteResult);
Expand Down Expand Up @@ -50,9 +50,9 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
}
object(MongoDB\Driver\BulkWrite)#%d (%d) {
["database"]=>
string(2) "db"
string(6) "phongo"
["collection"]=>
string(10) "collection"
string(15) "bulk_write_0002"
["ordered"]=>
bool(true)
["executed"]=>
Expand Down
Loading