-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHPC-2350: Implement JSON methods for PackedArray
- Loading branch information
Showing
11 changed files
with
385 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
MongoDB\BSON\PackedArray::fromJSON(): Decoding JSON | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils/basic.inc'; | ||
|
||
$tests = [ | ||
'[]', | ||
'[ 1, 2, 3 ]', | ||
'[[ 1, 2, 3 ]]', | ||
'[{ "bar": 1 }]', | ||
]; | ||
|
||
foreach ($tests as $json) { | ||
printf("Test %s\n", $json); | ||
$bson = MongoDB\BSON\PackedArray::fromJSON($json); | ||
hex_dump((string) $bson); | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECT-- | ||
Test [] | ||
0 : 05 00 00 00 00 [.....] | ||
Test [ 1, 2, 3 ] | ||
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......] | ||
Test [[ 1, 2, 3 ]] | ||
0 : 22 00 00 00 04 30 00 1a 00 00 00 10 30 00 01 00 ["....0......0...] | ||
10 : 00 00 10 31 00 02 00 00 00 10 32 00 03 00 00 00 [...1......2.....] | ||
20 : 00 00 [..] | ||
Test [{ "bar": 1 }] | ||
0 : 16 00 00 00 03 30 00 0e 00 00 00 10 62 61 72 00 [.....0......bar.] | ||
10 : 01 00 00 00 00 00 [......] | ||
===DONE=== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--TEST-- | ||
MongoDB\BSON\PackedArray::fromJSON(): Decoding extended JSON types | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils/basic.inc'; | ||
|
||
$tests = [ | ||
'[{ "$oid": "56315a7c6118fd1b920270b1" }]', | ||
'[{ "$binary": "Zm9v", "$type": "00" }]', | ||
'[{ "$date": "2015-10-28T00:00:00Z" }]', | ||
'[{ "$timestamp": { "t": 1446084619, "i": 0 }}]', | ||
'[{ "$regex": "pattern", "$options": "i" }]', | ||
'[{ "$undefined": true }]', | ||
'[{ "$minKey": 1 }]', | ||
'[{ "$maxKey": 1 }]', | ||
'[{ "$numberLong": "1234" }]', | ||
]; | ||
|
||
foreach ($tests as $json) { | ||
printf("Test %s\n", $json); | ||
$bson = MongoDB\BSON\PackedArray::fromJSON($json); | ||
hex_dump((string) $bson); | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECT-- | ||
Test [{ "$oid": "56315a7c6118fd1b920270b1" }] | ||
0 : 14 00 00 00 07 30 00 56 31 5a 7c 61 18 fd 1b 92 [.....0.V1Z|a....] | ||
10 : 02 70 b1 00 [.p..] | ||
Test [{ "$binary": "Zm9v", "$type": "00" }] | ||
0 : 10 00 00 00 05 30 00 03 00 00 00 00 66 6f 6f 00 [.....0......foo.] | ||
Test [{ "$date": "2015-10-28T00:00:00Z" }] | ||
0 : 10 00 00 00 09 30 00 00 80 be ab 50 01 00 00 00 [.....0.....P....] | ||
Test [{ "$timestamp": { "t": 1446084619, "i": 0 }}] | ||
0 : 10 00 00 00 11 30 00 00 00 00 00 0b 80 31 56 00 [.....0.......1V.] | ||
Test [{ "$regex": "pattern", "$options": "i" }] | ||
0 : 12 00 00 00 0b 30 00 70 61 74 74 65 72 6e 00 69 [.....0.pattern.i] | ||
10 : 00 00 [..] | ||
Test [{ "$undefined": true }] | ||
0 : 08 00 00 00 06 30 00 00 [.....0..] | ||
Test [{ "$minKey": 1 }] | ||
0 : 08 00 00 00 ff 30 00 00 [.....0..] | ||
Test [{ "$maxKey": 1 }] | ||
0 : 08 00 00 00 7f 30 00 00 [.....0..] | ||
Test [{ "$numberLong": "1234" }] | ||
0 : 10 00 00 00 12 30 00 d2 04 00 00 00 00 00 00 00 [.....0..........] | ||
===DONE=== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--TEST-- | ||
MongoDB\BSON\PackedArray::fromJSON(): Decoding JSON object | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils/basic.inc'; | ||
|
||
$bson = MongoDB\BSON\PackedArray::fromJSON('{ "foo": "bar" }'); | ||
var_dump($bson); | ||
hex_dump((string) $bson); | ||
echo $bson->toCanonicalExtendedJSON(), PHP_EOL; | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
object(MongoDB\BSON\PackedArray)#%d (%d) { | ||
["data"]=> | ||
string(24) "EgAAAAJmb28ABAAAAGJhcgAA" | ||
["value"]=> | ||
array(1) { | ||
[0]=> | ||
string(3) "bar" | ||
} | ||
} | ||
0 : 12 00 00 00 02 66 6f 6f 00 04 00 00 00 62 61 72 [.....foo.....bar] | ||
10 : 00 00 [..] | ||
[ "bar" ] | ||
===DONE=== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
MongoDB\BSON\PackedArray::fromJSON(): invalid JSON | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils/basic.inc'; | ||
|
||
throws(function() { | ||
MongoDB\BSON\PackedArray::fromJSON('foo'); | ||
}, 'MongoDB\Driver\Exception\UnexpectedValueException'); | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECT-- | ||
OK: Got MongoDB\Driver\Exception\UnexpectedValueException | ||
===DONE=== |
41 changes: 41 additions & 0 deletions
41
tests/bson/bson-packedarray-toCanonicalExtendedJSON-001.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
MongoDB\BSON\PackedArray::toCanonicalExtendedJSON(): Encoding JSON | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils/basic.inc'; | ||
|
||
$tests = [ | ||
[], | ||
[ null ], | ||
[ true ], | ||
[ 'foo' ], | ||
[ 123 ], | ||
[ 1.0, ], | ||
[ NAN ], | ||
[ INF ], | ||
[ -INF ], | ||
[ [ 'foo', 'bar' ]], | ||
[ [ 'foo' => 'bar' ]], | ||
]; | ||
|
||
foreach ($tests as $value) { | ||
echo MongoDB\BSON\PackedArray::fromPHP($value)->toCanonicalExtendedJSON(), "\n"; | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECT-- | ||
[ ] | ||
[ null ] | ||
[ true ] | ||
[ "foo" ] | ||
[ { "$numberInt" : "123" } ] | ||
[ { "$numberDouble" : "1.0" } ] | ||
[ { "$numberDouble" : "NaN" } ] | ||
[ { "$numberDouble" : "Infinity" } ] | ||
[ { "$numberDouble" : "-Infinity" } ] | ||
[ [ "foo", "bar" ] ] | ||
[ { "foo" : "bar" } ] | ||
===DONE=== |
Oops, something went wrong.