Skip to content

Commit

Permalink
Test valid return types
Browse files Browse the repository at this point in the history
  • Loading branch information
cs278 committed Jan 5, 2022
1 parent 22125c1 commit b7d7657
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 16 deletions.
43 changes: 35 additions & 8 deletions tests/func_015.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,43 @@ timecop.func_override=0
<?php
declare(strict_types=1);

foreach (['', (object) [], '1234', 4.5, true] as $input) {
// Simplified version of:
// https://github.com/symfony/polyfill/blob/6db783b3a48077f7102b6d7be6314d871eb7898c/src/Php80/Php80.php#L28-L58
function var_type($value): string
{
switch (true) {
case null === $value: return 'null';
case \is_bool($value): return 'bool';
case \is_string($value): return 'string';
case \is_array($value): return 'array';
case \is_int($value): return 'int';
case \is_float($value): return 'float';
case \is_object($value): return \get_class($value);
}

return 'unknown';
}

foreach (['', (object) [], '1234', 4.5, true, new \DateTimeImmutable(), new \DateTime(), 64] as $input) {
try {
var_dump(timecop_travel($input));
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
$message = $e->getMessage();

// Workaround exception message not including type in PHP 7 so the same test can be used for both.
if (PHP_MAJOR_VERSION === 7) {
$message = str_replace('N/A given', var_type($input).' given', $message);
}

echo $message, "\n";
}
}
--EXPECTREGEX--
timecop_travel\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|string) given
timecop_travel\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|stdClass) given
timecop_travel\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|string) given
timecop_travel\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|float) given
timecop_travel\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|bool) given
--EXPECT--
timecop_travel(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, string given
timecop_travel(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, stdClass given
timecop_travel(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, string given
timecop_travel(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, float given
timecop_travel(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, bool given
bool(true)
bool(true)
bool(true)
43 changes: 35 additions & 8 deletions tests/func_016.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,43 @@ timecop.func_override=0
<?php
declare(strict_types=1);

foreach (['', (object) [], '1234', 4.5, true] as $input) {
// Simplified version of:
// https://github.com/symfony/polyfill/blob/6db783b3a48077f7102b6d7be6314d871eb7898c/src/Php80/Php80.php#L28-L58
function var_type($value): string
{
switch (true) {
case null === $value: return 'null';
case \is_bool($value): return 'bool';
case \is_string($value): return 'string';
case \is_array($value): return 'array';
case \is_int($value): return 'int';
case \is_float($value): return 'float';
case \is_object($value): return \get_class($value);
}

return 'unknown';
}

foreach (['', (object) [], '1234', 4.5, true, new \DateTimeImmutable(), new \DateTime(), 64] as $input) {
try {
var_dump(timecop_freeze($input));
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
$message = $e->getMessage();

// Workaround exception message not including type in PHP 7 so the same test can be used for both.
if (PHP_MAJOR_VERSION === 7) {
$message = str_replace('N/A given', var_type($input).' given', $message);
}

echo $message, "\n";
}
}
--EXPECTREGEX--
timecop_freeze\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|string) given
timecop_freeze\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|stdClass) given
timecop_freeze\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|string) given
timecop_freeze\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|float) given
timecop_freeze\(\): Argument #1 \(\$timestamp\) must be of type DateTimeInterface|int, (N\/A|bool) given
--EXPECT--
timecop_freeze(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, string given
timecop_freeze(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, stdClass given
timecop_freeze(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, string given
timecop_freeze(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, float given
timecop_freeze(): Argument #1 ($timestamp) must be of type DateTimeInterface|int, bool given
bool(true)
bool(true)
bool(true)

0 comments on commit b7d7657

Please sign in to comment.