1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
--TEST--
BSON encoding: Encoding objects into BSON representation
--FILE--
<?php
require_once __DIR__ . '/../utils/tools.php';
class AssociativeArray implements MongoDB\BSON\Serializable, MongoDB\BSON\Unserializable
{
public function bsonSerialize()
{
return array("random" => "class", "data");
}
public function bsonUnserialize(array $data)
{
echo __METHOD__, "() was called with data:\n";
var_dump($data);
}
}
class NumericArray implements MongoDB\BSON\Serializable, MongoDB\BSON\Unserializable
{
public function bsonSerialize()
{
return array(1, 2, 3);
}
public function bsonUnserialize(array $data)
{
echo __METHOD__, "() was called with data:\n";
var_dump($data);
}
}
echo "Testing top-level AssociativeArray:\n";
$bson = fromPHP(new AssociativeArray);
echo toJSON($bson), "\n";
echo "Encoded BSON:\n";
hex_dump($bson);
$value = toPHP($bson, array("root" => 'AssociativeArray'));
echo "Decoded BSON:\n";
var_dump($value);
echo "\nTesting embedded AssociativeArray:\n";
$bson = fromPHP(array('embed' => new AssociativeArray));
echo toJSON($bson), "\n";
echo "Encoded BSON:\n";
hex_dump($bson);
$value = toPHP($bson, array("document" => 'AssociativeArray'));
echo "Decoded BSON:\n";
var_dump($value);
echo "\nTesting top-level NumericArray:\n";
$bson = fromPHP(new NumericArray);
echo toJSON($bson), "\n";
echo "Encoded BSON:\n";
hex_dump($bson);
$value = toPHP($bson, array("root" => 'NumericArray'));
echo "Decoded BSON:\n";
var_dump($value);
echo "\nTesting embedded NumericArray:\n";
$bson = fromPHP(array('embed' => new NumericArray));
echo toJSON($bson), "\n";
echo "Encoded BSON:\n";
hex_dump($bson);
$value = toPHP($bson, array("array" => 'NumericArray'));
echo "Decoded BSON:\n";
var_dump($value);
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Testing top-level AssociativeArray:
{ "random" : "class", "0" : "data" }
Encoded BSON:
0 : 23 00 00 00 02 72 61 6e 64 6f 6d 00 06 00 00 00 [#....random.....]
10 : 63 6c 61 73 73 00 02 30 00 05 00 00 00 64 61 74 [class..0.....dat]
20 : 61 00 00 [a..]
AssociativeArray::bsonUnserialize() was called with data:
array(2) {
["random"]=>
string(5) "class"
[0]=>
string(4) "data"
}
Decoded BSON:
object(AssociativeArray)#%d (0) {
}
Testing embedded AssociativeArray:
{ "embed" : { "random" : "class", "0" : "data" } }
Encoded BSON:
0 : 2f 00 00 00 03 65 6d 62 65 64 00 23 00 00 00 02 [/....embed.#....]
10 : 72 61 6e 64 6f 6d 00 06 00 00 00 63 6c 61 73 73 [random.....class]
20 : 00 02 30 00 05 00 00 00 64 61 74 61 00 00 00 [..0.....data...]
AssociativeArray::bsonUnserialize() was called with data:
array(2) {
["random"]=>
string(5) "class"
[0]=>
string(4) "data"
}
Decoded BSON:
object(stdClass)#%d (1) {
["embed"]=>
object(AssociativeArray)#%d (0) {
}
}
Testing top-level NumericArray:
{ "0" : 1, "1" : 2, "2" : 3 }
Encoded BSON:
0 : 1a 00 00 00 10 30 00 01 00 00 00 10 31 00 02 00 [.....0......1...]
10 : 00 00 10 32 00 03 00 00 00 00 [...2......]
NumericArray::bsonUnserialize() was called with data:
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
Decoded BSON:
object(NumericArray)#%d (0) {
}
Testing embedded NumericArray:
{ "embed" : [ 1, 2, 3 ] }
Encoded BSON:
0 : 26 00 00 00 04 65 6d 62 65 64 00 1a 00 00 00 10 [&....embed......]
10 : 30 00 01 00 00 00 10 31 00 02 00 00 00 10 32 00 [0......1......2.]
20 : 03 00 00 00 00 00 [......]
NumericArray::bsonUnserialize() was called with data:
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
Decoded BSON:
object(stdClass)#%d (1) {
["embed"]=>
object(NumericArray)#%d (0) {
}
}
===DONE===
|