Skip to content

Commit 0d30de6

Browse files
committed
Make toJSON() tests consistent with toCanonicalJSON() and toRelaxedJSON()
1 parent 65f5091 commit 0d30de6

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

tests/bson/bson-toJSON-001.phpt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ require_once __DIR__ . '/../utils/tools.php';
77

88
$tests = [
99
[],
10-
[ 'foo' => 'bar' ],
11-
[ 'foo' => [ 1, 2, 3 ]],
12-
[ 'foo' => [ 'bar' => 1 ]],
10+
[ 'null' => null ],
11+
[ 'boolean' => true ],
12+
[ 'string' => 'foo' ],
13+
[ 'integer' => 123 ],
14+
[ 'double' => 1.0, ],
15+
/* Note: toJSON() does not properly handle NAN and INF values.
16+
* toCanonicalJSON() or toRelaxedJSON() should be used instead. */
17+
[ 'nan' => NAN ],
18+
[ 'pos_inf' => INF ],
19+
[ 'neg_inf' => -INF ],
20+
[ 'array' => [ 'foo', 'bar' ]],
21+
[ 'document' => [ 'foo' => 'bar' ]],
1322
];
1423

1524
foreach ($tests as $value) {
@@ -22,7 +31,14 @@ foreach ($tests as $value) {
2231
<?php exit(0); ?>
2332
--EXPECT--
2433
{ }
25-
{ "foo" : "bar" }
26-
{ "foo" : [ 1, 2, 3 ] }
27-
{ "foo" : { "bar" : 1 } }
34+
{ "null" : null }
35+
{ "boolean" : true }
36+
{ "string" : "foo" }
37+
{ "integer" : 123 }
38+
{ "double" : 1.0 }
39+
{ "nan" : nan }
40+
{ "pos_inf" : inf }
41+
{ "neg_inf" : -inf }
42+
{ "array" : [ "foo", "bar" ] }
43+
{ "document" : { "foo" : "bar" } }
2844
===DONE===

tests/bson/bson-toJSON-002.phpt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ MongoDB\BSON\toJSON(): Encoding extended JSON types
66
require_once __DIR__ . '/../utils/tools.php';
77

88
$tests = [
9-
['_id' => new MongoDB\BSON\ObjectId('56315a7c6118fd1b920270b1')],
10-
['binary' => new MongoDB\BSON\Binary('foo', MongoDB\BSON\Binary::TYPE_GENERIC)],
11-
['date' => new MongoDB\BSON\UTCDateTime(1445990400000)],
12-
['timestamp' => new MongoDB\BSON\Timestamp(MongoDB\BSON\Binary::TYPE_GENERIC, 1446084619)],
13-
['regex' => new MongoDB\BSON\Regex('pattern', 'i')],
14-
['minkey' => new MongoDB\BSON\MinKey],
15-
['maxkey' => new MongoDB\BSON\MaxKey],
16-
['long' => 1234],
9+
[ '_id' => new MongoDB\BSON\ObjectId('56315a7c6118fd1b920270b1') ],
10+
[ 'binary' => new MongoDB\BSON\Binary('foo', MongoDB\BSON\Binary::TYPE_GENERIC) ],
11+
[ 'date' => new MongoDB\BSON\UTCDateTime(1445990400000) ],
12+
[ 'timestamp' => new MongoDB\BSON\Timestamp(1234, 5678) ],
13+
[ 'regex' => new MongoDB\BSON\Regex('pattern', 'i') ],
14+
[ 'code' => new MongoDB\BSON\Javascript('function() { return 1; }') ],
15+
[ 'code_ws' => new MongoDB\BSON\Javascript('function() { return a; }', ['a' => 1]) ],
16+
[ 'minkey' => new MongoDB\BSON\MinKey ],
17+
[ 'maxkey' => new MongoDB\BSON\MaxKey ],
1718
];
1819

1920
foreach ($tests as $value) {
@@ -28,9 +29,10 @@ foreach ($tests as $value) {
2829
{ "_id" : { "$oid" : "56315a7c6118fd1b920270b1" } }
2930
{ "binary" : { "$binary" : "Zm9v", "$type" : "00" } }
3031
{ "date" : { "$date" : 1445990400000 } }
31-
{ "timestamp" : { "$timestamp" : { "t" : 1446084619, "i" : 0 } } }
32+
{ "timestamp" : { "$timestamp" : { "t" : 5678, "i" : 1234 } } }
3233
{ "regex" : { "$regex" : "pattern", "$options" : "i" } }
34+
{ "code" : { "$code" : "function() { return 1; }" } }
35+
{ "code_ws" : { "$code" : "function() { return a; }", "$scope" : { "a" : 1 } } }
3336
{ "minkey" : { "$minKey" : 1 } }
3437
{ "maxkey" : { "$maxKey" : 1 } }
35-
{ "long" : 1234 }
3638
===DONE===

tests/bson/bson-toJSON_error-001.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ require_once __DIR__ . '/../utils/tools.php';
88
/* We can't really test for bson_iter_init() failure within bson_as_json(),
99
* since bson_reader_read() already checks that the buffer is at least 5 bytes.
1010
*/
11-
$invalidBson = array(
11+
$tests = [
1212
'',
1313
str_repeat(fromJSON('{"x": "y"}'), 2),
14-
);
14+
];
1515

16-
foreach ($invalidBson as $bson) {
16+
foreach ($tests as $bson) {
1717
echo throws(function() use ($bson) {
1818
toJSON($bson);
1919
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";

tests/bson/bson-toJSON_error-002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ MongoDB\BSON\toJSON(): BSON decoding exceptions for malformed documents
55

66
require_once __DIR__ . '/../utils/tools.php';
77

8-
$tests = array(
8+
$tests = [
99
pack('Vx', 4), // Empty document with invalid length (too small)
1010
pack('Vx', 6), // Empty document with invalid length (too large)
11-
);
11+
];
1212

1313
foreach ($tests as $bson) {
1414
echo throws(function() use ($bson) {

tests/bson/bson-toJSON_error-003.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ MongoDB\BSON\toJSON(): BSON decoding exceptions for bson_as_json() failure
55

66
require_once __DIR__ . '/../utils/tools.php';
77

8-
$tests = array(
8+
$tests = [
99
// Invalid UTF-8 characters (i.e. 0xFE, 0xFF) in field name
1010
pack('VCCCxVa*xx', 17, 2, 254, 255, 3, 'bar'),
1111
/* Note: we don't use a three-character string in the underflow case, as
@@ -17,7 +17,7 @@ $tests = array(
1717
*/
1818
pack('VCa*xVa*xx', 17, 1, 'foo', 3, 'ab'), // Invalid field type (underflow)
1919
pack('VCa*xVa*xx', 20, 1, 'foo', 6, 'abcde'), // Invalid field type (overflow)
20-
);
20+
];
2121

2222
foreach ($tests as $bson) {
2323
echo throws(function() use ($bson) {

0 commit comments

Comments
 (0)