Skip to content
Closed
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: 27 additions & 0 deletions tests/bson/bson-document-duplicate-keys-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
MongoDB\BSON\Document: Encoding duplicate keys
--DESCRIPTION--
BSON corpus spec prose test #2.1 (encoding)
https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#21-test-encoding
--FILE--
<?php

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

/* PHPC does not expose a BSON builder API. PHP arrays and objects cannot hold
* duplicate keys, so the duplicate keys below are collapsed by PHP itself
* before reaching the driver. */
$document = MongoDB\BSON\Document::fromPHP(['foo' => 1, 'foo' => 2]);

echo "PHP collapses duplicate keys, keeping the last value\n";
echo $document->toRelaxedExtendedJSON(), "\n";
var_dump((string) $document === hex2bin('0e00000010666f6f000200000000'));

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
PHP collapses duplicate keys, keeping the last value
{ "foo" : 2 }
bool(true)
===DONE===
79 changes: 79 additions & 0 deletions tests/bson/bson-document-duplicate-keys-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--TEST--
MongoDB\BSON\Document: Decoding BSON with duplicate keys
--DESCRIPTION--
BSON corpus spec prose test #2.2 (decoding)
https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#22-test-decoding
--FILE--
<?php

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

/* Duplicate keys are not validated by the driver. This test documents how each
* decoding API behaves. Bytes represent BSON for { "foo": 1, "foo": 2 } */
$document = MongoDB\BSON\Document::fromBSON(hex2bin('1700000010666f6f000100000010666f6f000200000000'));

echo "Iteration returns both elements in order\n";
foreach ($document as $key => $value) {
var_dump($key, $value);
}

echo "\nget() and offsetGet() return the first value\n";
var_dump($document->get('foo'));
var_dump($document['foo']);
var_dump($document->has('foo'));

echo "\ntoPHP() keeps the last value for object type maps\n";
var_dump($document->toPHP());
var_dump($document->toPHP(['root' => 'object']));

echo "\ntoPHP() keeps the last value for array type maps\n";
var_dump($document->toPHP(['root' => 'array']));

echo "\ntoPHP() with the bson type map preserves duplicates\n";
$bsonDocument = $document->toPHP(['root' => 'bson']);
var_dump($bsonDocument instanceof MongoDB\BSON\Document);
echo $bsonDocument->toRelaxedExtendedJSON(), "\n";

echo "\nExtended JSON preserves duplicates\n";
echo $document->toRelaxedExtendedJSON(), "\n";
echo $document->toCanonicalExtendedJSON(), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Iteration returns both elements in order
string(3) "foo"
int(1)
string(3) "foo"
int(2)

get() and offsetGet() return the first value
int(1)
int(1)
bool(true)

toPHP() keeps the last value for object type maps
object(stdClass)#%d (1) {
["foo"]=>
int(2)
}
object(stdClass)#%d (1) {
["foo"]=>
int(2)
}

toPHP() keeps the last value for array type maps
array(1) {
["foo"]=>
int(2)
}

toPHP() with the bson type map preserves duplicates
bool(true)
{ "foo" : 1, "foo" : 2 }

Extended JSON preserves duplicates
{ "foo" : 1, "foo" : 2 }
{ "foo" : { "$numberInt" : "1" }, "foo" : { "$numberInt" : "2" } }
===DONE===
42 changes: 42 additions & 0 deletions tests/bson/bson-document-duplicate-keys-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
MongoDB\BSON\Document: Round-trip of BSON with duplicate keys
--DESCRIPTION--
BSON corpus spec prose test #2.3 (round-trip)
https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#23-test-round-trip
--FILE--
<?php

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

/* Bytes represent BSON for { "foo": 1, "foo": 2 } */
$bson = hex2bin('1700000010666f6f000100000010666f6f000200000000');
$document = MongoDB\BSON\Document::fromBSON($bson);

echo "Raw BSON is preserved\n";
var_dump((string) $document === $bson);

echo "\nRound-trip through Extended JSON preserves duplicates\n";
$fromJson = MongoDB\BSON\Document::fromJSON($document->toRelaxedExtendedJSON());
echo $fromJson->toRelaxedExtendedJSON(), "\n";
var_dump((string) $fromJson === $bson);

echo "\nRound-trip through PHP keeps the last value\n";
$roundTrip = MongoDB\BSON\Document::fromPHP($document->toPHP());
echo $roundTrip->toRelaxedExtendedJSON(), "\n";
var_dump((string) $roundTrip === $bson);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Raw BSON is preserved
bool(true)

Round-trip through Extended JSON preserves duplicates
{ "foo" : 1, "foo" : 2 }
bool(true)

Round-trip through PHP keeps the last value
{ "foo" : 2 }
bool(false)
===DONE===
27 changes: 27 additions & 0 deletions tests/bson/bson-document-duplicate-keys-004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
MongoDB\BSON\Document: Parsing Extended JSON with duplicate keys
--DESCRIPTION--
BSON corpus spec prose test #2.4 (parsing Extended JSON)
https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#24-test-parsing-extended-json
--FILE--
<?php

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

/* Bytes represent BSON for { "foo": 1, "foo": 2 } */
$expected = hex2bin('1700000010666f6f000100000010666f6f000200000000');

$document = MongoDB\BSON\Document::fromJSON('{ "foo": 1, "foo": 2 }');

echo "Duplicate keys are preserved in order\n";
echo $document->toRelaxedExtendedJSON(), "\n";
var_dump((string) $document === $expected);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Duplicate keys are preserved in order
{ "foo" : 1, "foo" : 2 }
bool(true)
===DONE===
Loading