test(datetime): update datetime builtin class tests with explicit format parsing

- Replace dynamic current time test with fixed datetime format parsing
- Change DateTime::createFromFormat call to parse specific datetime string '2024-02-03 04:05:06'
- Update expected output from variable format to fixed '2024-02-03 04:05:06'
- Remove variable timestamp output and replace with predictable datetime string
- Modify static call test to use explicit datetime string instead of current time

- Add explicit string casting for Stringable objects when passed to internal string functions
- Update numeric values with explicit (string) casts before using in string functions
- Adjust test expectations to match strict type behavior for string operations
pull/47/head
韩天峰 3 weeks ago
parent f630fcf661
commit 1847b4c926
  1. 10
      tests/compiler/basic/datetime_builtin_class.phpt
  2. 4
      tests/compiler/magic_methods/invoke-and-tostring.phpt
  3. 2
      tests/compiler/static/static-call.phpt
  4. 6
      tests/compiler/string_method/to-str.phpt

@ -35,9 +35,9 @@ $date4 = new DateTime('2023-12-31');
$interval = $date3->diff($date4);
echo $interval->days . " days\n";
// Test static create methods
$now = DateTime::createFromFormat('U', time());
echo $now->format('Y-m-d H:i:s') . "\n";
// Test parsing a datetime string with an explicit format.
$parsed = DateTime::createFromFormat('Y-m-d H:i:s', '2024-02-03 04:05:06');
echo $parsed->format('Y-m-d H:i:s') . "\n";
// Test add/subtract intervals
$date->add(new DateInterval('P1D')); // Add 1 day
@ -57,8 +57,8 @@ echo $date->format('r') . "\n"; // RFC 2822 format
2024-01-01 12:00:00
2024-01-01 12:00:00 UTC
364 days
%d-%d-%d %d:%d:%d
2024-02-03 04:05:06
2024-01-02 12:00:00
2024-01-01 12:00:00
2024-01-01T12:00:00+00:00
Mon, 01 Jan 2024 12:00:00 +0000
Mon, 01 Jan 2024 12:00:00 +0000

@ -41,7 +41,9 @@ function main(): void {
echo $greet("World") . "\n";
echo $greet . "\n";
echo strlen($double) . "\n";
// TypePHP is always strict; Stringable objects require an explicit cast
// when passed to an internal function with a string parameter.
echo strlen((string) $double) . "\n";
}
?>
--EXPECT--

@ -4,6 +4,6 @@ static calls
<?php
$cls = 'DateTime';
$method = 'createFromFormat';
$now = $cls::$method('U', time());
$date = $cls::$method('Y-m-d H:i:s', '2024-02-03 04:05:06');
?>
--EXPECTF--

@ -1,12 +1,12 @@
--TEST--
any
Explicit numeric-to-string casts work with string functions in strict mode
--FILE--
<?php
ini_set('precision', 17);
$s = 12345678;
$s = (string) 12345678;
var_dump(strlen($s));
var_dump(strrev($s));
var_dump(strlen(3.1415926));
var_dump(strlen((string) 3.1415926));
?>
--EXPECT--
int(8)

Loading…
Cancel
Save