From 5597dfb16c9afbe2230156dab3a57a5ba541a38c Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 11 Sep 2024 13:04:46 +0200 Subject: [PATCH 1/7] Refactor UTCDateTime initialisation logic --- src/BSON/UTCDateTime.c | 50 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/BSON/UTCDateTime.c b/src/BSON/UTCDateTime.c index 8cb153f84..b5da44165 100644 --- a/src/BSON/UTCDateTime.c +++ b/src/BSON/UTCDateTime.c @@ -179,36 +179,36 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct) return; } - if (Z_TYPE_P(milliseconds) == IS_OBJECT) { - if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_interface_ce())) { - php_phongo_utcdatetime_init_from_date(intern, Z_PHPDATE_P(milliseconds)); - } else { - phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of DateTimeInterface, %s given", ZSTR_VAL(Z_OBJCE_P(milliseconds)->name)); + switch (Z_TYPE_P(milliseconds)) { + case IS_OBJECT: + if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_interface_ce())) { + php_phongo_utcdatetime_init_from_date(intern, Z_PHPDATE_P(milliseconds)); + } else { + phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of DateTimeInterface, %s given", ZSTR_VAL(Z_OBJCE_P(milliseconds)->name)); + } + return; + + case IS_LONG: + php_phongo_utcdatetime_init(intern, Z_LVAL_P(milliseconds)); + return; + + case IS_DOUBLE: { + char tmp[24]; + int tmp_len; + + tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", Z_DVAL_P(milliseconds) > 0 ? floor(Z_DVAL_P(milliseconds)) : ceil(Z_DVAL_P(milliseconds))); + + php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len); } - return; - } - - if (Z_TYPE_P(milliseconds) == IS_LONG) { - php_phongo_utcdatetime_init(intern, Z_LVAL_P(milliseconds)); - return; - } - if (Z_TYPE_P(milliseconds) == IS_DOUBLE) { - char tmp[24]; - int tmp_len; + return; - tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", Z_DVAL_P(milliseconds) > 0 ? floor(Z_DVAL_P(milliseconds)) : ceil(Z_DVAL_P(milliseconds))); - - php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len); - return; - } - - if (Z_TYPE_P(milliseconds) != IS_STRING) { - phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds)); - return; + case IS_STRING: + php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds)); + return; } - php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds)); + phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds)); } static PHP_METHOD(MongoDB_BSON_UTCDateTime, __set_state) From d0d718e88b052ceb25e25b46ea384344c2fe4476 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 11 Sep 2024 13:26:37 +0200 Subject: [PATCH 2/7] PHPC-2443: Deprecate passing string values to UTCDateTime constructor --- src/BSON/UTCDateTime.c | 2 ++ tests/bson/bson-utcdatetime-008.phpt | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 tests/bson/bson-utcdatetime-008.phpt diff --git a/src/BSON/UTCDateTime.c b/src/BSON/UTCDateTime.c index b5da44165..216a298cb 100644 --- a/src/BSON/UTCDateTime.c +++ b/src/BSON/UTCDateTime.c @@ -204,6 +204,8 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct) return; case IS_STRING: + php_error_docref(NULL, E_DEPRECATED, "Creating a %s instance with a string is deprecated and be removed in ext-mongodb 2.0", ZSTR_VAL(php_phongo_utcdatetime_ce->name)); + php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds)); return; } diff --git a/tests/bson/bson-utcdatetime-008.phpt b/tests/bson/bson-utcdatetime-008.phpt new file mode 100644 index 000000000..3214f1cd3 --- /dev/null +++ b/tests/bson/bson-utcdatetime-008.phpt @@ -0,0 +1,21 @@ +--TEST-- +MongoDB\BSON\UTCDateTime construction from 64-bit integer as string +--FILE-- + +===DONE=== + +--EXPECTF-- +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 %S +object(MongoDB\BSON\UTCDateTime)#%d (%d) { + ["milliseconds"]=> + string(13) "1416445411987" +} +===DONE=== From 3a8f90b7ced6bc8aaf2c4e874772e3a741f5ebde Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 11 Sep 2024 13:26:52 +0200 Subject: [PATCH 3/7] PHPC-2443: Accept Int64 instances in UTCDateTime constructor --- src/BSON/UTCDateTime.c | 4 +++- src/BSON/UTCDateTime.stub.php | 4 ++-- src/BSON/UTCDateTime_arginfo.h | 4 ++-- tests/bson/bson-utcdatetime-009.phpt | 20 ++++++++++++++++++++ 4 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 tests/bson/bson-utcdatetime-009.phpt diff --git a/src/BSON/UTCDateTime.c b/src/BSON/UTCDateTime.c index 216a298cb..599fa0161 100644 --- a/src/BSON/UTCDateTime.c +++ b/src/BSON/UTCDateTime.c @@ -183,8 +183,10 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct) case IS_OBJECT: if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_interface_ce())) { php_phongo_utcdatetime_init_from_date(intern, Z_PHPDATE_P(milliseconds)); + } else if (instanceof_function(Z_OBJCE_P(milliseconds), php_phongo_int64_ce)) { + php_phongo_utcdatetime_init(intern, Z_INT64_OBJ_P(milliseconds)->integer); } else { - phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of DateTimeInterface, %s given", ZSTR_VAL(Z_OBJCE_P(milliseconds)->name)); + phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of %s or %s, %s given", ZSTR_VAL(php_date_get_interface_ce()->name), ZSTR_VAL(php_phongo_int64_ce->name), ZSTR_VAL(Z_OBJCE_P(milliseconds)->name)); } return; diff --git a/src/BSON/UTCDateTime.stub.php b/src/BSON/UTCDateTime.stub.php index ac9143a33..575a70641 100644 --- a/src/BSON/UTCDateTime.stub.php +++ b/src/BSON/UTCDateTime.stub.php @@ -10,9 +10,9 @@ final class UTCDateTime implements UTCDateTimeInterface, \JsonSerializable, Type, \Serializable { #if PHP_VERSION_ID >= 80000 - final public function __construct(int|string|float|\DateTimeInterface|null $milliseconds = null) {} + final public function __construct(int|string|float|\DateTimeInterface|Int64|null $milliseconds = null) {} #else - /** @param int|string|float|\DateTimeInterface|null $milliseconds */ + /** @param int|string|float|\DateTimeInterface|Int64|null $milliseconds */ final public function __construct($milliseconds = null) {} #endif diff --git a/src/BSON/UTCDateTime_arginfo.h b/src/BSON/UTCDateTime_arginfo.h index df008acda..581ef5fb6 100644 --- a/src/BSON/UTCDateTime_arginfo.h +++ b/src/BSON/UTCDateTime_arginfo.h @@ -1,9 +1,9 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 2b7ac84585a74a210af3cb2061541977cc309e2d */ + * Stub hash: 11f44ab06f1806ec74876a6034c6fd75672fe2a8 */ #if PHP_VERSION_ID >= 80000 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_BSON_UTCDateTime___construct, 0, 0, 0) - ZEND_ARG_OBJ_TYPE_MASK(0, milliseconds, DateTimeInterface, MAY_BE_LONG|MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_NULL, "null") + ZEND_ARG_OBJ_TYPE_MASK(0, milliseconds, DateTimeInterface|MongoDB\\BSON\\Int64, MAY_BE_LONG|MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_NULL, "null") ZEND_END_ARG_INFO() #endif diff --git a/tests/bson/bson-utcdatetime-009.phpt b/tests/bson/bson-utcdatetime-009.phpt new file mode 100644 index 000000000..f17bc0e35 --- /dev/null +++ b/tests/bson/bson-utcdatetime-009.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\BSON\UTCDateTime construction from Int64 object +--FILE-- + +===DONE=== + +--EXPECTF-- +object(MongoDB\BSON\UTCDateTime)#%d (%d) { + ["milliseconds"]=> + string(13) "1416445411987" +} +===DONE=== From 7a1b7db621c1df8c9d10e2d0e164a20e1731bb1f Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 11 Sep 2024 13:52:20 +0200 Subject: [PATCH 4/7] Use Int64 instances in tests --- tests/bson/bson-fromPHP-003.phpt | 4 +-- tests/bson/bson-fromPHP_error-003.phpt | 2 +- tests/bson/bson-toPHP-004.phpt | 2 +- tests/bson/bson-utcdatetime-001.phpt | 14 ++++---- tests/bson/bson-utcdatetime-002.phpt | 4 +-- tests/bson/bson-utcdatetime-clone-001.phpt | 2 +- tests/bson/bson-utcdatetime-clone-002.phpt | 2 +- .../bson-utcdatetime-get_properties-001.phpt | 2 +- .../bson-utcdatetime-get_properties-002.phpt | 2 +- tests/bson/bson-utcdatetime-int-size-001.phpt | 30 ----------------- tests/bson/bson-utcdatetime-int-size-002.phpt | 32 ------------------- .../bson-utcdatetime-serialization-003.phpt | 6 ++-- .../bson/bson-utcdatetime-todatetime-001.phpt | 2 +- .../bson/bson-utcdatetime-todatetime-002.phpt | 2 +- ...n-utcdatetime-todatetimeimmutable-001.phpt | 2 +- ...n-utcdatetime-todatetimeimmutable-002.phpt | 2 +- tests/bson/bson-utcdatetime-tostring-001.phpt | 2 +- tests/bson/bson-utcdatetime_error-001.phpt | 2 +- tests/bson/bson-utcdatetime_error-003.phpt | 9 +++++- tests/bson/bson-utcdatetimeinterface-001.phpt | 2 +- tests/bson/bug0631.phpt | 2 +- .../writeresult-getupsertedids-002.phpt | 2 +- 22 files changed, 37 insertions(+), 92 deletions(-) delete mode 100644 tests/bson/bson-utcdatetime-int-size-001.phpt delete mode 100644 tests/bson/bson-utcdatetime-int-size-002.phpt diff --git a/tests/bson/bson-fromPHP-003.phpt b/tests/bson/bson-fromPHP-003.phpt index 38df0c17e..85fc850b2 100644 --- a/tests/bson/bson-fromPHP-003.phpt +++ b/tests/bson/bson-fromPHP-003.phpt @@ -12,8 +12,8 @@ class MyDocument { } $tests = array( - array(new MongoDB\BSON\UTCDateTime('1416445411987')), - array('x' => new MongoDB\BSON\UTCDateTime('1416445411987')), + array(new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987'))), + array('x' => new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987'))), array(new MyDocument), array('x' => new MyDocument), ); diff --git a/tests/bson/bson-fromPHP_error-003.phpt b/tests/bson/bson-fromPHP_error-003.phpt index b6f355ec8..195dccf0a 100644 --- a/tests/bson/bson-fromPHP_error-003.phpt +++ b/tests/bson/bson-fromPHP_error-003.phpt @@ -16,7 +16,7 @@ $tests = array( new MongoDB\BSON\ObjectId, new MongoDB\BSON\Regex('regexp', 'i'), new MongoDB\BSON\Timestamp(1234, 5678), - new MongoDB\BSON\UTCDateTime('1416445411987'), + new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987')), ); foreach ($tests as $document) { diff --git a/tests/bson/bson-toPHP-004.phpt b/tests/bson/bson-toPHP-004.phpt index 1bf4f1b81..c8e5497f8 100644 --- a/tests/bson/bson-toPHP-004.phpt +++ b/tests/bson/bson-toPHP-004.phpt @@ -29,7 +29,7 @@ $tests = [ new MongoDB\BSON\ObjectId('586c18d86118fd6c9012dec1'), new MongoDB\BSON\Regex('foo'), new MongoDB\BSON\Timestamp(1234, 5678), - new MongoDB\BSON\UTCDateTime('1483479256924'), + new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1483479256924')), ]; foreach ($tests as $value) { diff --git a/tests/bson/bson-utcdatetime-001.phpt b/tests/bson/bson-utcdatetime-001.phpt index 73bf8922e..0520981b2 100644 --- a/tests/bson/bson-utcdatetime-001.phpt +++ b/tests/bson/bson-utcdatetime-001.phpt @@ -11,7 +11,7 @@ require_once __DIR__ . "/../utils/basic.inc"; $manager = create_test_manager(); -$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987"); +$utcdatetime = new MongoDB\BSON\UTCDateTime(2147483647); $bulk = new MongoDB\Driver\BulkWrite(); $bulk->insert(array('_id' => 1, 'x' => $utcdatetime)); @@ -38,12 +38,12 @@ foreach($tests as $n => $test) { ===DONE=== --EXPECT-- -Test#0 { "0" : { "$date" : { "$numberLong" : "1416445411987" } } } -string(59) "{ "0" : { "$date" : { "$numberLong" : "1416445411987" } } }" -string(59) "{ "0" : { "$date" : { "$numberLong" : "1416445411987" } } }" +Test#0 { "0" : { "$date" : { "$numberLong" : "2147483647" } } } +string(56) "{ "0" : { "$date" : { "$numberLong" : "2147483647" } } }" +string(56) "{ "0" : { "$date" : { "$numberLong" : "2147483647" } } }" bool(true) -Test#1 { "0" : { "$date" : { "$numberLong" : "1416445411987" } } } -string(59) "{ "0" : { "$date" : { "$numberLong" : "1416445411987" } } }" -string(59) "{ "0" : { "$date" : { "$numberLong" : "1416445411987" } } }" +Test#1 { "0" : { "$date" : { "$numberLong" : "2147483647" } } } +string(56) "{ "0" : { "$date" : { "$numberLong" : "2147483647" } } }" +string(56) "{ "0" : { "$date" : { "$numberLong" : "2147483647" } } }" bool(true) ===DONE=== diff --git a/tests/bson/bson-utcdatetime-002.phpt b/tests/bson/bson-utcdatetime-002.phpt index 6da9fd291..cd5d83080 100644 --- a/tests/bson/bson-utcdatetime-002.phpt +++ b/tests/bson/bson-utcdatetime-002.phpt @@ -3,7 +3,7 @@ MongoDB\BSON\UTCDateTime debug handler --FILE-- - %rint\(|string\(13\) "|%r1416445411987%r"|\)%r + %rint\(|string\(10\) "|%r2147483647%r"|\)%r } ===DONE=== diff --git a/tests/bson/bson-utcdatetime-clone-001.phpt b/tests/bson/bson-utcdatetime-clone-001.phpt index 1a516c367..ac7e05111 100644 --- a/tests/bson/bson-utcdatetime-clone-001.phpt +++ b/tests/bson/bson-utcdatetime-clone-001.phpt @@ -8,7 +8,7 @@ MongoDB\BSON\UTCDateTime can be cloned (PHP < 8.2) require_once __DIR__ . "/../utils/basic.inc"; -$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987"); +$utcdatetime = new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987')); $clone = clone $utcdatetime; diff --git a/tests/bson/bson-utcdatetime-clone-002.phpt b/tests/bson/bson-utcdatetime-clone-002.phpt index a7834ae59..b9e8922c1 100644 --- a/tests/bson/bson-utcdatetime-clone-002.phpt +++ b/tests/bson/bson-utcdatetime-clone-002.phpt @@ -8,7 +8,7 @@ MongoDB\BSON\UTCDateTime can be cloned (PHP >= 8.2) require_once __DIR__ . "/../utils/basic.inc"; -$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987"); +$utcdatetime = new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987')); $clone = clone $utcdatetime; diff --git a/tests/bson/bson-utcdatetime-get_properties-001.phpt b/tests/bson/bson-utcdatetime-get_properties-001.phpt index ab4dd66d9..572884228 100644 --- a/tests/bson/bson-utcdatetime-get_properties-001.phpt +++ b/tests/bson/bson-utcdatetime-get_properties-001.phpt @@ -3,7 +3,7 @@ MongoDB\BSON\UTCDateTime get_properties handler (get_object_vars) --FILE-- $value) { var_dump($key); diff --git a/tests/bson/bson-utcdatetime-int-size-001.phpt b/tests/bson/bson-utcdatetime-int-size-001.phpt deleted file mode 100644 index be2eaf9d7..000000000 --- a/tests/bson/bson-utcdatetime-int-size-001.phpt +++ /dev/null @@ -1,30 +0,0 @@ ---TEST-- -MongoDB\BSON\UTCDateTime integer parsing from string ---INI-- -date.timezone=UTC -error_reporting=-1 -dislay_errors=1 ---FILE-- -toDateTime()); - -?> -===DONE=== - ---EXPECTF-- -object(MongoDB\BSON\UTCDateTime)#%d (1) { - ["milliseconds"]=> - string(13) "1416445411987" -} -object(DateTime)#%d (3) { - ["date"]=> - string(26) "2014-11-20 01:03:31.987000" - ["timezone_type"]=> - int(1) - ["timezone"]=> - string(6) "+00:00" -} -===DONE=== diff --git a/tests/bson/bson-utcdatetime-int-size-002.phpt b/tests/bson/bson-utcdatetime-int-size-002.phpt deleted file mode 100644 index d303c9574..000000000 --- a/tests/bson/bson-utcdatetime-int-size-002.phpt +++ /dev/null @@ -1,32 +0,0 @@ ---TEST-- -MongoDB\BSON\UTCDateTime integer parsing from number (64-bit) ---SKIPIF-- - ---INI-- -date.timezone=UTC -error_reporting=-1 -dislay_errors=1 ---FILE-- -toDateTime()); - -?> -===DONE=== - ---EXPECTF-- -object(MongoDB\BSON\UTCDateTime)#%d (1) { - ["milliseconds"]=> - string(13) "1416445411987" -} -object(DateTime)#%d (3) { - ["date"]=> - string(26) "2014-11-20 01:03:31.987000" - ["timezone_type"]=> - int(1) - ["timezone"]=> - string(6) "+00:00" -} -===DONE=== diff --git a/tests/bson/bson-utcdatetime-serialization-003.phpt b/tests/bson/bson-utcdatetime-serialization-003.phpt index 941057adf..c15c7548f 100644 --- a/tests/bson/bson-utcdatetime-serialization-003.phpt +++ b/tests/bson/bson-utcdatetime-serialization-003.phpt @@ -4,9 +4,9 @@ MongoDB\BSON\UTCDateTime serialization (__serialize and __unserialize) toDateTime(); var_dump(get_class($datetime)); var_dump($datetime->format(DATE_RSS)); diff --git a/tests/bson/bson-utcdatetime-todatetime-002.phpt b/tests/bson/bson-utcdatetime-todatetime-002.phpt index e003cd188..f127f19fe 100644 --- a/tests/bson/bson-utcdatetime-todatetime-002.phpt +++ b/tests/bson/bson-utcdatetime-todatetime-002.phpt @@ -5,7 +5,7 @@ date.timezone=UTC --FILE-- toDateTime(); var_dump(get_class($datetime)); echo $datetime->format('U.u'), "\n"; diff --git a/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt b/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt index 4d7bd8385..3e17543fb 100644 --- a/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt +++ b/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt @@ -5,7 +5,7 @@ date.timezone=America/Los_Angeles --FILE-- toDateTimeImmutable(); var_dump(get_class($datetime)); var_dump($datetime->format(DATE_RSS)); diff --git a/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt b/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt index b959d0bc1..8ccf140c9 100644 --- a/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt +++ b/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt @@ -5,7 +5,7 @@ date.timezone=UTC --FILE-- toDateTimeImmutable(); var_dump(get_class($datetime)); echo $datetime->format('U.u'), "\n"; diff --git a/tests/bson/bson-utcdatetime-tostring-001.phpt b/tests/bson/bson-utcdatetime-tostring-001.phpt index 53b9fdd82..b88fbe867 100644 --- a/tests/bson/bson-utcdatetime-tostring-001.phpt +++ b/tests/bson/bson-utcdatetime-tostring-001.phpt @@ -3,7 +3,7 @@ MongoDB\BSON\UTCDateTime::__toString() --FILE-- diff --git a/tests/bson/bson-utcdatetime_error-001.phpt b/tests/bson/bson-utcdatetime_error-001.phpt index 54534fbf6..7dc837507 100644 --- a/tests/bson/bson-utcdatetime_error-001.phpt +++ b/tests/bson/bson-utcdatetime_error-001.phpt @@ -14,5 +14,5 @@ echo throws(function() { --EXPECT-- OK: Got MongoDB\Driver\Exception\InvalidArgumentException -Expected instance of DateTimeInterface, stdClass given +Expected instance of DateTimeInterface or MongoDB\BSON\Int64, stdClass given ===DONE=== diff --git a/tests/bson/bson-utcdatetime_error-003.phpt b/tests/bson/bson-utcdatetime_error-003.phpt index 760eff8b2..e0bd72a8a 100644 --- a/tests/bson/bson-utcdatetime_error-003.phpt +++ b/tests/bson/bson-utcdatetime_error-003.phpt @@ -26,13 +26,20 @@ echo throws(function() { ?> ===DONE=== ---EXPECT-- +--EXPECTF-- +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 in %S OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "1234.5678" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization + +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 in %S OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "9223372036854775808" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization + +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 in %S OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "-9223372036854775809" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization + +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 in %S OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "18446744073709551615" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization ===DONE=== diff --git a/tests/bson/bson-utcdatetimeinterface-001.phpt b/tests/bson/bson-utcdatetimeinterface-001.phpt index 26c31b2fb..babd2f798 100644 --- a/tests/bson/bson-utcdatetimeinterface-001.phpt +++ b/tests/bson/bson-utcdatetimeinterface-001.phpt @@ -3,7 +3,7 @@ MongoDB\BSON\UTCDateTimeInterface is implemented by MongoDB\BSON\UTCDateTime --FILE-- diff --git a/tests/bson/bug0631.phpt b/tests/bson/bug0631.phpt index c07f95863..3099076cd 100644 --- a/tests/bson/bug0631.phpt +++ b/tests/bson/bug0631.phpt @@ -7,7 +7,7 @@ date.timezone=UTC require_once __DIR__ . '/../utils/basic.inc'; -$utcdatetime = new MongoDB\BSON\UTCDateTime('1466540755123'); +$utcdatetime = new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1466540755123')); $datetime = $utcdatetime->toDateTime(); $s = serialize($datetime); diff --git a/tests/writeResult/writeresult-getupsertedids-002.phpt b/tests/writeResult/writeresult-getupsertedids-002.phpt index 786fa0956..713a8c212 100644 --- a/tests/writeResult/writeresult-getupsertedids-002.phpt +++ b/tests/writeResult/writeresult-getupsertedids-002.phpt @@ -25,7 +25,7 @@ $tests = [ new MongoDB\BSON\MinKey, new MongoDB\BSON\ObjectId('586c18d86118fd6c9012dec1'), new MongoDB\BSON\Timestamp(1234, 5678), - new MongoDB\BSON\UTCDateTime('1483479256924'), + new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1483479256924')), ]; $manager = create_test_manager(); From d3c90a667b81843948a93206d17f27c568df4b7a Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 12 Sep 2024 09:02:42 +0200 Subject: [PATCH 5/7] Use 64-bit value in UTCDateTime tests --- tests/bson/bson-utcdatetime-001.phpt | 14 +++++++------- tests/bson/bson-utcdatetime-002.phpt | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/bson/bson-utcdatetime-001.phpt b/tests/bson/bson-utcdatetime-001.phpt index 0520981b2..7175e4004 100644 --- a/tests/bson/bson-utcdatetime-001.phpt +++ b/tests/bson/bson-utcdatetime-001.phpt @@ -11,7 +11,7 @@ require_once __DIR__ . "/../utils/basic.inc"; $manager = create_test_manager(); -$utcdatetime = new MongoDB\BSON\UTCDateTime(2147483647); +$utcdatetime = new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987')); $bulk = new MongoDB\Driver\BulkWrite(); $bulk->insert(array('_id' => 1, 'x' => $utcdatetime)); @@ -38,12 +38,12 @@ foreach($tests as $n => $test) { ===DONE=== --EXPECT-- -Test#0 { "0" : { "$date" : { "$numberLong" : "2147483647" } } } -string(56) "{ "0" : { "$date" : { "$numberLong" : "2147483647" } } }" -string(56) "{ "0" : { "$date" : { "$numberLong" : "2147483647" } } }" +Test#0 { "0" : { "$date" : { "$numberLong" : "1416445411987" } } } +string(59) "{ "0" : { "$date" : { "$numberLong" : "1416445411987" } } }" +string(59) "{ "0" : { "$date" : { "$numberLong" : "1416445411987" } } }" bool(true) -Test#1 { "0" : { "$date" : { "$numberLong" : "2147483647" } } } -string(56) "{ "0" : { "$date" : { "$numberLong" : "2147483647" } } }" -string(56) "{ "0" : { "$date" : { "$numberLong" : "2147483647" } } }" +Test#1 { "0" : { "$date" : { "$numberLong" : "1416445411987" } } } +string(59) "{ "0" : { "$date" : { "$numberLong" : "1416445411987" } } }" +string(59) "{ "0" : { "$date" : { "$numberLong" : "1416445411987" } } }" bool(true) ===DONE=== diff --git a/tests/bson/bson-utcdatetime-002.phpt b/tests/bson/bson-utcdatetime-002.phpt index cd5d83080..27db72e4c 100644 --- a/tests/bson/bson-utcdatetime-002.phpt +++ b/tests/bson/bson-utcdatetime-002.phpt @@ -3,7 +3,7 @@ MongoDB\BSON\UTCDateTime debug handler --FILE-- - %rint\(|string\(10\) "|%r2147483647%r"|\)%r + string(13) "1416445411987" } ===DONE=== From 4852ea0ef305ace6c37da423353515e070a4f3be Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 12 Sep 2024 09:03:15 +0200 Subject: [PATCH 6/7] Fix missing word in deprecation message --- src/BSON/UTCDateTime.c | 2 +- tests/bson/bson-utcdatetime-008.phpt | 2 +- tests/bson/bson-utcdatetime_error-003.phpt | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/BSON/UTCDateTime.c b/src/BSON/UTCDateTime.c index 599fa0161..3f2925552 100644 --- a/src/BSON/UTCDateTime.c +++ b/src/BSON/UTCDateTime.c @@ -206,7 +206,7 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct) return; case IS_STRING: - php_error_docref(NULL, E_DEPRECATED, "Creating a %s instance with a string is deprecated and be removed in ext-mongodb 2.0", ZSTR_VAL(php_phongo_utcdatetime_ce->name)); + php_error_docref(NULL, E_DEPRECATED, "Creating a %s instance with a string is deprecated and will be removed in ext-mongodb 2.0", ZSTR_VAL(php_phongo_utcdatetime_ce->name)); php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds)); return; diff --git a/tests/bson/bson-utcdatetime-008.phpt b/tests/bson/bson-utcdatetime-008.phpt index 3214f1cd3..d39e02088 100644 --- a/tests/bson/bson-utcdatetime-008.phpt +++ b/tests/bson/bson-utcdatetime-008.phpt @@ -13,7 +13,7 @@ var_dump($utcdatetime); ===DONE=== --EXPECTF-- -Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 %S +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s object(MongoDB\BSON\UTCDateTime)#%d (%d) { ["milliseconds"]=> string(13) "1416445411987" diff --git a/tests/bson/bson-utcdatetime_error-003.phpt b/tests/bson/bson-utcdatetime_error-003.phpt index e0bd72a8a..6eb1fa741 100644 --- a/tests/bson/bson-utcdatetime_error-003.phpt +++ b/tests/bson/bson-utcdatetime_error-003.phpt @@ -27,19 +27,19 @@ echo throws(function() { ===DONE=== --EXPECTF-- -Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 in %S +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "1234.5678" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization -Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 in %S +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "9223372036854775808" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization -Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 in %S +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "-9223372036854775809" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization -Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and be removed in ext-mongodb 2.0 in %S +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "18446744073709551615" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization ===DONE=== From 151409ffb97ffd4aa5a2608f8d57dcb3c8247d1b Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 12 Sep 2024 09:08:59 +0200 Subject: [PATCH 7/7] Extract init methods for utcdatetime_t --- src/BSON/UTCDateTime.c | 48 ++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/BSON/UTCDateTime.c b/src/BSON/UTCDateTime.c index 3f2925552..8a732bfbc 100644 --- a/src/BSON/UTCDateTime.c +++ b/src/BSON/UTCDateTime.c @@ -104,6 +104,35 @@ static bool php_phongo_utcdatetime_init_from_date(php_phongo_utcdatetime_t* inte return true; } +static bool php_phongo_utcdatetime_init_from_object(php_phongo_utcdatetime_t* intern, zend_object* object) +{ + if (instanceof_function(object->ce, php_date_get_interface_ce())) { + php_phongo_utcdatetime_init_from_date(intern, php_date_obj_from_obj(object)); + + return true; + } + + if (instanceof_function(object->ce, php_phongo_int64_ce)) { + php_phongo_utcdatetime_init(intern, php_int64_fetch_object(object)->integer); + + return true; + } + + phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of %s or %s, %s given", ZSTR_VAL(php_date_get_interface_ce()->name), ZSTR_VAL(php_phongo_int64_ce->name), ZSTR_VAL(object->ce->name)); + + return false; +} + +static bool php_phongo_utcdatetime_init_from_double(php_phongo_utcdatetime_t* intern, double milliseconds) +{ + char tmp[24]; + int tmp_len; + + tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", milliseconds > 0 ? floor(milliseconds) : ceil(milliseconds)); + + return php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len); +} + static HashTable* php_phongo_utcdatetime_get_properties_hash(phongo_compat_object_handler_type* object, bool is_temp) { php_phongo_utcdatetime_t* intern; @@ -181,28 +210,15 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct) switch (Z_TYPE_P(milliseconds)) { case IS_OBJECT: - if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_interface_ce())) { - php_phongo_utcdatetime_init_from_date(intern, Z_PHPDATE_P(milliseconds)); - } else if (instanceof_function(Z_OBJCE_P(milliseconds), php_phongo_int64_ce)) { - php_phongo_utcdatetime_init(intern, Z_INT64_OBJ_P(milliseconds)->integer); - } else { - phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of %s or %s, %s given", ZSTR_VAL(php_date_get_interface_ce()->name), ZSTR_VAL(php_phongo_int64_ce->name), ZSTR_VAL(Z_OBJCE_P(milliseconds)->name)); - } + php_phongo_utcdatetime_init_from_object(intern, Z_OBJ_P(milliseconds)); return; case IS_LONG: php_phongo_utcdatetime_init(intern, Z_LVAL_P(milliseconds)); return; - case IS_DOUBLE: { - char tmp[24]; - int tmp_len; - - tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", Z_DVAL_P(milliseconds) > 0 ? floor(Z_DVAL_P(milliseconds)) : ceil(Z_DVAL_P(milliseconds))); - - php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len); - } - + case IS_DOUBLE: + php_phongo_utcdatetime_init_from_double(intern, Z_DVAL_P(milliseconds)); return; case IS_STRING: