Skip to content

Commit 29f6983

Browse files
committed
UTCDateTime tests
1 parent e61b29b commit 29f6983

8 files changed

+155
-4
lines changed

tests/bson/bson-utcdatetime-serialization-001.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
--TEST--
2-
MongoDB\BSON\UTCDateTime serialization
2+
MongoDB\BSON\UTCDateTime serialization (Serializable interface)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_php_version('>=', '7.4.0'); ?>
36
--FILE--
47
<?php
58

tests/bson/bson-utcdatetime-serialization-002.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
--TEST--
2-
MongoDB\BSON\UTCDateTime serialization (unserialize 32-bit data on 64-bit)
2+
MongoDB\BSON\UTCDateTime serialization (unserialize 32-bit data on 64-bit) (Serializable interface)
33
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
45
<?php if (8 !== PHP_INT_SIZE) { die('skip Only for 64-bit platform'); } ?>
6+
<?php skip_if_php_version('>=', '7.4.0'); ?>
57
--FILE--
68
<?php
79

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
MongoDB\BSON\UTCDateTime serialization (__serialize and __unserialize)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_php_version('<', '7.4.0'); ?>
6+
--FILE--
7+
<?php
8+
9+
$tests = [
10+
'0',
11+
'-1416445411987',
12+
'1416445411987',
13+
];
14+
15+
foreach ($tests as $milliseconds) {
16+
var_dump($udt = new MongoDB\BSON\UTCDateTime($milliseconds));
17+
var_dump($s = serialize($udt));
18+
var_dump(unserialize($s));
19+
echo "\n";
20+
}
21+
22+
?>
23+
===DONE===
24+
<?php exit(0); ?>
25+
--EXPECTF--
26+
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
27+
["milliseconds"]=>
28+
string(1) "0"
29+
}
30+
string(64) "O:24:"MongoDB\BSON\UTCDateTime":1:{s:12:"milliseconds";s:1:"0";}"
31+
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
32+
["milliseconds"]=>
33+
string(1) "0"
34+
}
35+
36+
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
37+
["milliseconds"]=>
38+
string(14) "-1416445411987"
39+
}
40+
string(78) "O:24:"MongoDB\BSON\UTCDateTime":1:{s:12:"milliseconds";s:14:"-1416445411987";}"
41+
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
42+
["milliseconds"]=>
43+
string(14) "-1416445411987"
44+
}
45+
46+
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
47+
["milliseconds"]=>
48+
string(13) "1416445411987"
49+
}
50+
string(77) "O:24:"MongoDB\BSON\UTCDateTime":1:{s:12:"milliseconds";s:13:"1416445411987";}"
51+
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
52+
["milliseconds"]=>
53+
string(13) "1416445411987"
54+
}
55+
56+
===DONE===
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
MongoDB\BSON\UTCDateTime serialization (unserialize 32-bit data on 64-bit) (__serialize and __unserialize)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php if (8 !== PHP_INT_SIZE) { die('skip Only for 64-bit platform'); } ?>
6+
<?php skip_if_php_version('<', '7.4.0'); ?>
7+
--FILE--
8+
<?php
9+
10+
$tests = [
11+
'0',
12+
'-1416445411987',
13+
'1416445411987',
14+
];
15+
16+
foreach ($tests as $milliseconds) {
17+
$s = sprintf('O:24:"MongoDB\BSON\UTCDateTime":1:{s:12:"milliseconds";s:%d:"%s";}', strlen($milliseconds), $milliseconds);
18+
19+
var_dump($s);
20+
var_dump(unserialize($s));
21+
echo "\n";
22+
}
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECTF--
28+
string(64) "O:24:"MongoDB\BSON\UTCDateTime":1:{s:12:"milliseconds";s:1:"0";}"
29+
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
30+
["milliseconds"]=>
31+
string(1) "0"
32+
}
33+
34+
string(78) "O:24:"MongoDB\BSON\UTCDateTime":1:{s:12:"milliseconds";s:14:"-1416445411987";}"
35+
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
36+
["milliseconds"]=>
37+
string(14) "-1416445411987"
38+
}
39+
40+
string(77) "O:24:"MongoDB\BSON\UTCDateTime":1:{s:12:"milliseconds";s:13:"1416445411987";}"
41+
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
42+
["milliseconds"]=>
43+
string(13) "1416445411987"
44+
}
45+
46+
===DONE===

tests/bson/bson-utcdatetime-serialization_error-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
MongoDB\BSON\UTCDateTime unserialization requires "milliseconds" integer or numeric string field
2+
MongoDB\BSON\UTCDateTime unserialization requires "milliseconds" integer or numeric string field (Serializable interface)
33
--FILE--
44
<?php
55

tests/bson/bson-utcdatetime-serialization_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
MongoDB\BSON\UTCDateTime unserialization requires "milliseconds" string to parse as 64-bit integer
2+
MongoDB\BSON\UTCDateTime unserialization requires "milliseconds" string to parse as 64-bit integer (Serializable interface)
33
--FILE--
44
<?php
55

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
MongoDB\BSON\UTCDateTime unserialization requires "milliseconds" integer or numeric string field (__serialize and __unserialize)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_php_version('<', '7.4.0'); ?>
6+
--FILE--
7+
<?php
8+
9+
require_once __DIR__ . '/../utils/basic.inc';
10+
11+
echo throws(function() {
12+
unserialize('O:24:"MongoDB\BSON\UTCDateTime":1:{s:12:"milliseconds";d:1;}');
13+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
14+
15+
?>
16+
===DONE===
17+
<?php exit(0); ?>
18+
--EXPECT--
19+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
20+
MongoDB\BSON\UTCDateTime initialization requires "milliseconds" integer or numeric string field
21+
===DONE===
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
MongoDB\BSON\UTCDateTime unserialization requires "milliseconds" string to parse as 64-bit integer (__serialize and __unserialize)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_php_version('<', '7.4.0'); ?>
6+
--FILE--
7+
<?php
8+
9+
require_once __DIR__ . '/../utils/basic.inc';
10+
11+
echo throws(function() {
12+
unserialize('O:24:"MongoDB\BSON\UTCDateTime":1:{s:12:"milliseconds";s:9:"1234.5678";}');
13+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
14+
15+
/* TODO: Add tests for out-of-range values once CDRIVER-1377 is resolved */
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
22+
Error parsing "1234.5678" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization
23+
===DONE===

0 commit comments

Comments
 (0)