Skip to content
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

Fix reflection data mismatches #8

Merged
merged 9 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tests/*.log
tests/*.php
tests/*.sh
tests/*.out
!tests/*.inc.php

# Ignore C-related temporary files
*.o
Expand Down
31 changes: 31 additions & 0 deletions tests/check-func-refl.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

function replace_words($input, array $replace)
{
$patterns = [];
$replacements = [];

foreach ($replace as $search => $replacement) {
$patterns[] = '{\b'.preg_quote($search).'\b}';
$replacements[] = $replacement;
}

return preg_replace($patterns, $replacements, $input);
}

foreach ($functions as $phpFunctionName => $timecopFunctionName) {
echo sprintf("Checking %s vs %s\n", $phpFunctionName, $timecopFunctionName);

$phpFunction = new \ReflectionFunction($phpFunctionName);
$timecopFunction = new \ReflectionFunction($timecopFunctionName);

$timecopFunctionNormalised = replace_words($timecopFunction, [
$timecopFunction->getName() => $phpFunction->getName(),
$timecopFunction->getExtensionName() => $phpFunction->getExtensionName(),
]);

if ((string) $phpFunction !== $timecopFunctionNormalised) {
echo "php: ", $phpFunction, "\n";
echo "timecop: ", $timecopFunctionNormalised, "\n";
}
}
1 change: 1 addition & 0 deletions tests/func_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Test for timecop_mktime()
--SKIPIF--
<?php
$required_func = array("timecop_freeze", "timecop_mktime");
$required_version_less_than = '8.0';
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
Expand Down
26 changes: 26 additions & 0 deletions tests/func_002_php8.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Test for timecop_mktime() (PHP8)
--SKIPIF--
<?php
$required_func = array("timecop_freeze", "timecop_mktime");
$required_version = '8.0';
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php
timecop_freeze(172840); // "1970-01-02T16:00:40-08:00"
var_dump(timecop_mktime(4));
var_dump(timecop_mktime(4,6));
var_dump(timecop_mktime(4,6,41));
var_dump(timecop_mktime(4,6,41,1));
var_dump(timecop_mktime(4,6,41,1,1));
var_dump(timecop_mktime(12,33,20,5,3,1976));
--EXPECT--
int(129640)
int(130000)
int(130001)
int(130001)
int(43601)
int(200000000)
1 change: 1 addition & 0 deletions tests/func_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Test for timecop_gmmktime()
--SKIPIF--
<?php
$required_func = array("timecop_freeze", "timecop_gmmktime");
$required_version_less_than = '8.0';
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
Expand Down
26 changes: 26 additions & 0 deletions tests/func_003_php8.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Test for timecop_gmmktime()
--SKIPIF--
<?php
$required_func = array("timecop_freeze", "timecop_gmmktime");
$required_version = '8.0';
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php
timecop_freeze(86440); // "1970-01-02T00:00:40+00:00"
var_dump(timecop_gmmktime(12));
var_dump(timecop_gmmktime(12,6));
var_dump(timecop_gmmktime(12,6,41));
var_dump(timecop_gmmktime(12,6,41,1));
var_dump(timecop_gmmktime(12,6,41,1,1));
var_dump(timecop_gmmktime(19,33,20,5,3,1976));
--EXPECT--
int(129640)
int(130000)
int(130001)
int(130001)
int(43601)
int(200000000)
1 change: 1 addition & 0 deletions tests/func_override_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Function overrideing test for mktime()
--SKIPIF--
<?php
$required_func = array("timecop_freeze");
$required_version_less_than = '8.0';
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
Expand Down
26 changes: 26 additions & 0 deletions tests/func_override_002_php8.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Function overrideing test for mktime()
--SKIPIF--
<?php
$required_func = array("timecop_freeze");
$required_version = '8.0';
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=1
--FILE--
<?php
timecop_freeze(172840); // "1970-01-02T16:00:40-08:00"
var_dump(mktime(4));
var_dump(mktime(4,6));
var_dump(mktime(4,6,41));
var_dump(mktime(4,6,41,1));
var_dump(mktime(4,6,41,1,1));
var_dump(mktime(12,33,20,5,3,1976));
--EXPECT--
int(129640)
int(130000)
int(130001)
int(130001)
int(43601)
int(200000000)
1 change: 1 addition & 0 deletions tests/func_override_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Function overrideing test for gmmktime()
--SKIPIF--
<?php
$required_func = array("timecop_freeze");
$required_version_less_than = '8.0';
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
timecop.func_override=1
Expand Down
25 changes: 25 additions & 0 deletions tests/func_override_003_php8.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Function overrideing test for gmmktime()
--SKIPIF--
<?php
$required_func = array("timecop_freeze");
$required_version = '8.0';
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
timecop.func_override=1
--FILE--
<?php
timecop_freeze(86440); // "1970-01-02T00:00:40+00:00"
var_dump(gmmktime(12));
var_dump(gmmktime(12,6));
var_dump(gmmktime(12,6,41));
var_dump(gmmktime(12,6,41,1));
var_dump(gmmktime(12,6,41,1,1));
var_dump(gmmktime(19,33,20,5,3,1976));
--EXPECT--
int(129640)
int(130000)
int(130001)
int(130001)
int(43601)
int(200000000)
1 change: 1 addition & 0 deletions tests/issue_010.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Check for issue #10 (Timecop segfaults when set_error_handler throws an exception)
--SKIPIF--
<?php
$required_version_less_than = "8.0"; // @todo If possible find a way to trigger an exception on PHP8
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=GMT
Expand Down
2 changes: 1 addition & 1 deletion tests/issue_043_php8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ try {
echo $e->getMessage();
}
--EXPECT--
timecop_strtotime(): Argument #1 ($time) must be of type string, null given
timecop_strtotime(): Argument #1 ($datetime) must be of type string, null given
53 changes: 53 additions & 0 deletions tests/refl_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
Check reflection data for overridden methods
--SKIPIF--
<?php
$required_func = array();
$required_class = array("TimecopDateTime", "TimecopDateTimeImmutable", "ReflectionMethod");
$required_method = array();
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php

function replace_words($input, array $replace)
{
$patterns = [];
$replacements = [];

foreach ($replace as $search => $replacement) {
$patterns[] = '{\b'.preg_quote($search).'\b}';
$replacements[] = $replacement;
}

return preg_replace($patterns, $replacements, $input);
}

foreach (['DateTime', 'DateTimeImmutable'] as $className) {
foreach (['__construct', 'createFromFormat'] as $method) {
echo sprintf("Checking %1\$s::%2\$s vs Timecop%1\$s::%2\$s\n", $className, $method);

$phpMethod = new \ReflectionMethod($className, $method);
$timecopMethod = new \ReflectionMethod('Timecop'.$className, $method);

$timecopMethodNormalised = replace_words($timecopMethod, [
$timecopMethod->getName() => $phpMethod->getName(),
$timecopMethod->getExtensionName() => $phpMethod->getExtensionName(),
', overwrites '.$className => '',
', prototype '.$className => '',
]);

if ((string) $phpMethod !== $timecopMethodNormalised) {
echo "php: ", $phpMethod, "\n";
echo "timecop: ", $timecopMethodNormalised, "\n";
}
}
}

--EXPECT--
Checking DateTime::__construct vs TimecopDateTime::__construct
Checking DateTime::createFromFormat vs TimecopDateTime::createFromFormat
Checking DateTimeImmutable::__construct vs TimecopDateTimeImmutable::__construct
Checking DateTimeImmutable::createFromFormat vs TimecopDateTimeImmutable::createFromFormat
46 changes: 46 additions & 0 deletions tests/refl_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Check reflection data for overridden functions
--SKIPIF--
<?php
$required_func = array();
$required_class = array();
$required_method = array();
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php

$functions = [
'time' => 'timecop_time',
'date' => 'timecop_date',
'gmdate' => 'timecop_gmdate',
'idate' => 'timecop_idate',
'getdate' => 'timecop_getdate',
'localtime' => 'timecop_localtime',
'strtotime' => 'timecop_strtotime',
'strftime' => 'timecop_strftime',
'gmstrftime' => 'timecop_gmstrftime',
'date_create' => 'timecop_date_create',
'date_create_from_format' => 'timecop_date_create_from_format',
'date_create_immutable' => 'timecop_date_create_immutable',
'date_create_immutable_from_format' => 'timecop_date_create_immutable_from_format',
];

require __DIR__.'/check-func-refl.inc.php';

--EXPECT--
Checking time vs timecop_time
Checking date vs timecop_date
Checking gmdate vs timecop_gmdate
Checking idate vs timecop_idate
Checking getdate vs timecop_getdate
Checking localtime vs timecop_localtime
Checking strtotime vs timecop_strtotime
Checking strftime vs timecop_strftime
Checking gmstrftime vs timecop_gmstrftime
Checking date_create vs timecop_date_create
Checking date_create_from_format vs timecop_date_create_from_format
Checking date_create_immutable vs timecop_date_create_immutable
Checking date_create_immutable_from_format vs timecop_date_create_immutable_from_format
24 changes: 24 additions & 0 deletions tests/refl_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Check reflection data for overridden functions
--SKIPIF--
<?php
$required_func = array("microtime", "gettimeofday");
$required_class = array();
$required_method = array();
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php

$functions = [
'microtime' => 'timecop_microtime',
'gettimeofday' => 'timecop_gettimeofday',
];

require __DIR__.'/check-func-refl.inc.php';

--EXPECT--
Checking microtime vs timecop_microtime
Checking gettimeofday vs timecop_gettimeofday
22 changes: 22 additions & 0 deletions tests/refl_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Check reflection data for overridden functions
--SKIPIF--
<?php
$required_func = array("unixtojd");
$required_class = array();
$required_method = array();
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php

$functions = [
'unixtojd' => 'timecop_unixtojd',
];

require __DIR__.'/check-func-refl.inc.php';

--EXPECT--
Checking unixtojd vs timecop_unixtojd
25 changes: 25 additions & 0 deletions tests/refl_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Check reflection data for overridden functions (On PHP 5.6 timecop_mktime() does not implement the $is_dst parameter)
--SKIPIF--
<?php
$required_version = '7.0';
$required_func = array();
$required_class = array();
$required_method = array();
include(__DIR__."/tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php

$functions = [
'mktime' => 'timecop_mktime',
'gmmktime' => 'timecop_gmmktime',
];

require __DIR__.'/check-func-refl.inc.php';

--EXPECT--
Checking mktime vs timecop_mktime
Checking gmmktime vs timecop_gmmktime
Loading