-
Notifications
You must be signed in to change notification settings - Fork 208
PHPC-536: UTCDateTime construction from DateTime or current time #336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--TEST-- | ||
BSON BSON\UTCDateTime constructor defaults to current time | ||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
$tests = [ | ||
new MongoDB\BSON\UTCDateTime, | ||
new MongoDB\BSON\UTCDateTime(null), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think "null" should just be coerced to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current behavior is consistent with your HHVM implementation. Note that DateTime's constructor also defaults to the current time if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
]; | ||
|
||
foreach ($tests as $test) { | ||
var_dump($test); | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(%d) | ||
} | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(%d) | ||
} | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--TEST-- | ||
BSON BSON\UTCDateTime construction from DateTime | ||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
$tests = [ | ||
new DateTime(), | ||
new DateTime('@1215282385'), | ||
new DateTime('2011-01-01T15:03:01.012345Z'), | ||
new DateTime('2050-11-12T13:14:15.999999Z'), | ||
]; | ||
|
||
foreach ($tests as $test) { | ||
var_dump(new MongoDB\BSON\UTCDateTime($test)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should try to round trip them back with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds like a good idea. Let's get #338 merged first in that case, since it fixes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(%d000) | ||
} | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(1215282385000) | ||
} | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(1293894181012) | ||
} | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(2551871655999) | ||
} | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
BSON BSON\UTCDateTime construction from DateTimeImmutable | ||
--SKIPIF-- | ||
<?php if (!version_compare(phpversion(), "5.5", '>=')) echo "skip >= PHP 5.5 needed\n"; ?> | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
$tests = [ | ||
new DateTimeImmutable(), | ||
new DateTimeImmutable('@1215282385'), | ||
new DateTimeImmutable('2011-01-01T15:03:01.012345Z'), | ||
new DateTimeImmutable('2050-11-12T13:14:15.999999Z'), | ||
]; | ||
|
||
foreach ($tests as $test) { | ||
var_dump(new MongoDB\BSON\UTCDateTime($test)); | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(%d000) | ||
} | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(1215282385000) | ||
} | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(1293894181012) | ||
} | ||
object(%SBSON\UTCDateTime)#%d (%d) { | ||
["milliseconds"]=> | ||
int(2551871655999) | ||
} | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
--TEST-- | ||
BSON BSON\UTCDateTime #001 error | ||
--INI-- | ||
date.timezone=America/Los_Angeles | ||
BSON BSON\UTCDateTime requires object argument to implement DateTimeInterface | ||
--SKIPIF-- | ||
<?php if (defined("HHVM_VERSION_ID")) exit("skip HHVM handles parameter parsing differently"); ?> | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"?> | ||
--FILE-- | ||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might have to put this one back in I suppose, if we find that it's still different with HHVM. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it will be a problem. Your constructor accepts a mixed type and all validation is left to you, so it should be trivial to customize the exception message here. |
||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
throws(function() { | ||
$classname = BSON_NAMESPACE . "\\UTCDateTime"; | ||
new $classname; | ||
}, "MongoDB\\Driver\\Exception\\InvalidArgumentException"); | ||
echo throws(function() { | ||
new MongoDB\BSON\UTCDateTime(new stdClass); | ||
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n"; | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
--EXPECT-- | ||
OK: Got MongoDB\Driver\Exception\InvalidArgumentException | ||
Expected instance of DateTimeInterface, stdClass given | ||
===DONE=== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you now pass it in as a non datetime class, it will not throw an error/exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still end up with an InvalidArgumentException:
In the HHVM driver, you end up casting the argument to an integer if it's neither
null
nor a DateTime instance. Would you like to intercept other objects and throw a special message?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with "Expected instance of DateTimeInterface, $classname given". We technically check for an instance of DateTime and DateTimeImmutable (on 5.5+); however, DateTimeInterface cannot be implemented by userland classes so the message is still accurate.