Skip to content

Commit

Permalink
Test reflection data between Timecop functions and PHP functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cs278 committed Dec 24, 2021
1 parent 0f72c64 commit cefc414
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 0 deletions.
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
92 changes: 92 additions & 0 deletions tests/check-func-refl.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

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

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

$checkFunction = function ($method) use ($timecopFunction, $phpFunction) {
if ($phpFunction->$method() !== $timecopFunction->$method()) {
echo "${method}() failed\n";
echo "php: "; var_dump($phpFunction->$method());
echo "timecop: "; var_dump($timecopFunction->$method());
echo "\n";
}
};

$checkFunction('getNumberOfParameters');
$checkFunction('getNumberOfRequiredParameters');
$checkFunction('isDeprecated');
$checkFunction('isInternal');
$checkFunction('isUserDefined');
$checkFunction('isVariadic');
$checkFunction('returnsReference');

if (PHP_MAJOR_VERSION >= 7) {
$checkFunction('hasReturnType');
}

if ($phpFunction->getNumberOfParameters() === $timecopFunction->getNumberOfParameters()) {
foreach ($phpFunction->getParameters() as $idx => $phpParameter) {
$timecopParameter = $timecopFunction->getParameters()[$idx];

$checkParameter = function ($method) use ($timecopParameter, $phpParameter) {
if ($phpParameter->$method() !== $timecopParameter->$method()) {
echo "Parameter {$phpParameter->getName()} ${method}() failed\n";
echo "php: "; var_dump($phpParameter->$method());
echo "timecop: "; var_dump($timecopParameter->$method());
echo "\n";
}
};

$checkParameter('getName');
$checkParameter('allowsNull');
$checkParameter('isDefaultValueAvailable');
$checkParameter('isOptional');

if (PHP_MAJOR_VERSION >= 7) {
$checkParameter('hasType');

$phpParameterType = $phpParameter->getType();
$timecopParameterType = $timecopParameter->getType();
$getClass = function (\ReflectionType $type = null) {
return $type === null ? '' : get_class($type);
};

$allowsNull = function (\ReflectionType $type = null) {
return $type === null ? null : $type->allowsNull();
};

if ($getClass($phpParameterType) !== $getClass($timecopParameterType)) {
echo "Parameter {$phpParameter->getName()} Type()::class failed\n";
echo "php: "; var_dump($getClass($phpParameterType));
echo "timecop: "; var_dump($getClass($timecopParameterType));
echo "\n";
}

if ($allowsNull($phpParameterType) !== $allowsNull($timecopParameterType)) {
echo "Parameter {$phpParameter->getName()} getType()->allowsNull() failed\n";
echo "php: "; var_dump($allowsNull($phpParameterType));
echo "timecop: "; var_dump($allowsNull($timecopParameterType));
echo "\n";
}
}
}
} else {
echo "Mismatched number of parameters...\n";
echo "{$phpFunction->getName()}() parameters:\n";

foreach ($phpFunction->getParameters() as $phpParameter) {
echo "\t- {$phpParameter->getName()}\n";
}

echo "{$timecopFunction->getName()}() parameters:\n";

foreach ($timecopFunction->getParameters() as $timecopParameter) {
echo "\t- {$timecopParameter->getName()}\n";
}

echo "\n";
}
}
120 changes: 120 additions & 0 deletions tests/refl_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
--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

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);

$checkMethod = function ($method) use ($timecopMethod, $phpMethod) {
if ($phpMethod->$method() !== $timecopMethod->$method()) {
echo "${method}() failed\n";
echo "php: "; var_dump($phpMethod->$method());
echo "timecop: "; var_dump($timecopMethod->$method());
echo "\n";
}
};

$checkMethod('isAbstract');
$checkMethod('isConstructor');
$checkMethod('isDestructor');
$checkMethod('isFinal');
$checkMethod('isPrivate');
$checkMethod('isProtected');
$checkMethod('isPublic');
$checkMethod('isStatic');
$checkMethod('getNumberOfParameters');
$checkMethod('getNumberOfRequiredParameters');
$checkMethod('isDeprecated');
$checkMethod('isInternal');
$checkMethod('isUserDefined');
$checkMethod('isVariadic');
$checkMethod('returnsReference');

if (PHP_MAJOR_VERSION >= 7) {
$checkMethod('hasReturnType');
}

if ($phpMethod->getNumberOfParameters() === $timecopMethod->getNumberOfParameters()) {
foreach ($phpMethod->getParameters() as $idx => $phpParameter) {
$timecopParameter = $timecopMethod->getParameters()[$idx];

$checkParameter = function ($method) use ($timecopParameter, $phpParameter) {
if ($phpParameter->$method() !== $timecopParameter->$method()) {
echo "Parameter {$phpParameter->getName()}${method}() failed\n";
echo "php: "; var_dump($phpParameter->$method());
echo "timecop: "; var_dump($timecopParameter->$method());
echo "\n";
}
};

$checkParameter('getName');
$checkParameter('allowsNull');
$checkParameter('isDefaultValueAvailable');
$checkParameter('isOptional');

if (PHP_MAJOR_VERSION >= 7) {
$checkParameter('hasType');

$phpParameterType = $phpParameter->getType();
$timecopParameterType = $timecopParameter->getType();
$getClass = function (\ReflectionType $type = null) {
return $type === null ? '' : get_class($type);
};

$allowsNull = function (\ReflectionType $type = null) {
return $type === null ? null : $type->allowsNull();
};

if ($getClass($phpParameterType) !== $getClass($timecopParameterType)) {
echo "Parameter {$phpParameter->getName()} Type()::class failed\n";
echo "php: "; var_dump($getClass($phpParameterType));
echo "timecop: "; var_dump($getClass($timecopParameterType));
echo "\n";
}

if ($allowsNull($phpParameterType) !== $allowsNull($timecopParameterType)) {
echo "Parameter {$phpParameter->getName()} getType()->allowsNull() failed\n";
echo "php: "; var_dump($allowsNull($phpParameterType));
echo "timecop: "; var_dump($allowsNull($timecopParameterType));
echo "\n";
}
}
}
} else {
echo "Mismatched number of parameters...\n";
echo "{$phpMethod->getName()}() parameters:\n";

foreach ($phpMethod->getParameters() as $phpParameter) {
echo "\t- {$phpParameter->getName()}\n";
}

echo "{$timecopMethod->getName()}() parameters:\n";

foreach ($timecopMethod->getParameters() as $timecopParameter) {
echo "\t- {$timecopParameter->getName()}\n";
}

echo "\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
50 changes: 50 additions & 0 deletions tests/refl_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--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',
'mktime' => 'timecop_mktime',
'gmmktime' => 'timecop_gmmktime',
'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 mktime vs timecop_mktime
Checking gmmktime vs timecop_gmmktime
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

0 comments on commit cefc414

Please sign in to comment.