Skip to content
Closed
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
8 changes: 4 additions & 4 deletions Zend/tests/is_a.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ function __autoload($name) {
class BASE {
}

interface INT {
interface I {
}

class A extends BASE implements INT {
class A extends BASE implements I {
}

$a = new A;
var_dump(is_a($a, "B1"));
var_dump(is_a($a, "A"));
var_dump(is_a($a, "BASE"));
var_dump(is_a($a, "INT"));
var_dump(is_a($a, "I"));
var_dump(is_subclass_of($a, "B2"));
var_dump(is_subclass_of($a, "A"));
var_dump(is_subclass_of($a, "BASE"));
var_dump(is_subclass_of($a, "INT"));
var_dump(is_subclass_of($a, "I"));

var_dump(is_subclass_of("X1", "X2"));
?>
Expand Down
20 changes: 20 additions & 0 deletions Zend/tests/typehints/scalar_aliases.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Scalar type hint aliases
--FILE--
<?php

function foo(integer $a, boolean $b) {
var_dump($a, $b);
}

function bar(int $a, bool $b) {
var_dump($a, $b);
}

foo(1, true);
bar(1, true);
--EXPECT--
int(1)
bool(true)
int(1)
bool(true)
241 changes: 241 additions & 0 deletions Zend/tests/typehints/scalar_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
--TEST--
Scalar type hint basics
--FILE--
<?php

$errnames = [
E_NOTICE => 'E_NOTICE',
E_WARNING => 'E_WARNING',
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR'
];
set_error_handler(function (int $errno, string $errmsg, string $file, int $line) use ($errnames) {
echo "$errnames[$errno]: $errmsg on line $line\n";
return true;
});

$functions = [
'int' => function (int $i) { return $i; },
'float' => function (float $f) { return $f; },
'string' => function (string $s) { return $s; },
'bool' => function (bool $b) { return $b; }
];

class Stringable {
public function __toString() {
return "foobar";
}
}

$values = [
1,
"1",
1.0,
1.5,
"1a",
"a",
"",
PHP_INT_MAX,
NAN,
TRUE,
FALSE,
NULL,
[],
new StdClass,
new Stringable,
fopen("data:text/plain,foobar", "r")
];

foreach ($functions as $type => $function) {
echo PHP_EOL, "Testing '$type' typehint:", PHP_EOL;
foreach ($values as $value) {
echo "*** Trying ";
var_dump($value);
var_dump($function($value));
}
}
--EXPECTF--

Testing 'int' typehint:
*** Trying int(1)
int(1)
*** Trying string(1) "1"
int(1)
*** Trying float(1)
int(1)
*** Trying float(1.5)
int(1)
*** Trying string(2) "1a"
E_NOTICE: A non well formed numeric value encountered on line %d
int(1)
*** Trying string(1) "a"
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, string given, called in %s on line %d and defined on line %d
string(1) "a"
*** Trying string(0) ""
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, string given, called in %s on line %d and defined on line %d
string(0) ""
*** Trying int(%d)
int(%d)
*** Trying float(NAN)
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, float given, called in %s on line %d and defined on line %d
float(NAN)
*** Trying bool(true)
int(1)
*** Trying bool(false)
int(0)
*** Trying NULL
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, null given, called in %s on line %d and defined on line %d
NULL
*** Trying array(0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, array given, called in %s on line %d and defined on line %d
array(0) {
}
*** Trying object(stdClass)#%s (0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, object given, called in %s on line %d and defined on line %d
object(stdClass)#%s (0) {
}
*** Trying object(Stringable)#%s (0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, object given, called in %s on line %d and defined on line %d
object(Stringable)#%s (0) {
}
*** Trying resource(%d) of type (stream)
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, resource given, called in %s on line %d and defined on line %d
resource(%d) of type (stream)

Testing 'float' typehint:
*** Trying int(1)
float(1)
*** Trying string(1) "1"
float(1)
*** Trying float(1)
float(1)
*** Trying float(1.5)
float(1.5)
*** Trying string(2) "1a"
E_NOTICE: A non well formed numeric value encountered on line %d
float(1)
*** Trying string(1) "a"
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type float, string given, called in %s on line %d and defined on line %d
string(1) "a"
*** Trying string(0) ""
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type float, string given, called in %s on line %d and defined on line %d
string(0) ""
*** Trying int(%d)
float(%s)
*** Trying float(NAN)
float(NAN)
*** Trying bool(true)
float(1)
*** Trying bool(false)
float(0)
*** Trying NULL
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type float, null given, called in %s on line %d and defined on line %d
NULL
*** Trying array(0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type float, array given, called in %s on line %d and defined on line %d
array(0) {
}
*** Trying object(stdClass)#%s (0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type float, object given, called in %s on line %d and defined on line %d
object(stdClass)#%s (0) {
}
*** Trying object(Stringable)#%s (0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type float, object given, called in %s on line %d and defined on line %d
object(Stringable)#%s (0) {
}
*** Trying resource(%d) of type (stream)
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type float, resource given, called in %s on line %d and defined on line %d
resource(%d) of type (stream)

Testing 'string' typehint:
*** Trying int(1)
string(1) "1"
*** Trying string(1) "1"
string(1) "1"
*** Trying float(1)
string(1) "1"
*** Trying float(1.5)
string(3) "1.5"
*** Trying string(2) "1a"
string(2) "1a"
*** Trying string(1) "a"
string(1) "a"
*** Trying string(0) ""
string(0) ""
*** Trying int(%d)
string(%d) "%d"
*** Trying float(NAN)
string(3) "NAN"
*** Trying bool(true)
string(1) "1"
*** Trying bool(false)
string(0) ""
*** Trying NULL
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type string, null given, called in %s on line %d and defined on line %d
NULL
*** Trying array(0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type string, array given, called in %s on line %d and defined on line %d
array(0) {
}
*** Trying object(stdClass)#%s (0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type string, object given, called in %s on line %d and defined on line %d
object(stdClass)#%s (0) {
}
*** Trying object(Stringable)#%s (0) {
}
string(6) "foobar"
*** Trying resource(%d) of type (stream)
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type string, resource given, called in %s on line %d and defined on line %d
resource(%d) of type (stream)

Testing 'bool' typehint:
*** Trying int(1)
bool(true)
*** Trying string(1) "1"
bool(true)
*** Trying float(1)
bool(true)
*** Trying float(1.5)
bool(true)
*** Trying string(2) "1a"
bool(true)
*** Trying string(1) "a"
bool(true)
*** Trying string(0) ""
bool(false)
*** Trying int(%d)
bool(true)
*** Trying float(NAN)
bool(true)
*** Trying bool(true)
bool(true)
*** Trying bool(false)
bool(false)
*** Trying NULL
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type boolean, null given, called in %s on line %d and defined on line %d
NULL
*** Trying array(0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type boolean, array given, called in %s on line %d and defined on line %d
array(0) {
}
*** Trying object(stdClass)#%s (0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type boolean, object given, called in %s on line %d and defined on line %d
object(stdClass)#%s (0) {
}
*** Trying object(Stringable)#%s (0) {
}
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type boolean, object given, called in %s on line %d and defined on line %d
object(Stringable)#%s (0) {
}
*** Trying resource(%d) of type (stream)
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type boolean, resource given, called in %s on line %d and defined on line %d
resource(%d) of type (stream)
55 changes: 55 additions & 0 deletions Zend/tests/typehints/scalar_none.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
Scalar type hint missing parameters
--FILE--
<?php

$errnames = [
E_NOTICE => 'E_NOTICE',
E_WARNING => 'E_WARNING',
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR'
];
set_error_handler(function (int $errno, string $errmsg, string $file, int $line) use ($errnames) {
echo "$errnames[$errno]: $errmsg on line $line\n";
return true;
});

$functions = [
'int' => function (int $i) { return $i; },
'float' => function (float $f) { return $f; },
'string' => function (string $s) { return $s; },
'bool' => function (bool $b) { return $b; },
'int nullable' => function (int $i = NULL) { return $i; },
'float nullable' => function (float $f = NULL) { return $f; },
'string nullable' => function (string $s = NULL) { return $s; },
'bool nullable' => function (bool $b = NULL) { return $b; }
];

foreach ($functions as $type => $function) {
echo "Testing $type:", PHP_EOL;
var_dump($function());
}
--EXPECTF--
Testing int:
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, none given, called in %s on line %d and defined on line %d
E_NOTICE: Undefined variable: i on line %d
NULL
Testing float:
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type float, none given, called in %s on line %d and defined on line %d
E_NOTICE: Undefined variable: f on line %d
NULL
Testing string:
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type string, none given, called in %s on line %d and defined on line %d
E_NOTICE: Undefined variable: s on line %d
NULL
Testing bool:
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type boolean, none given, called in %s on line %d and defined on line %d
E_NOTICE: Undefined variable: b on line %d
NULL
Testing int nullable:
NULL
Testing float nullable:
NULL
Testing string nullable:
NULL
Testing bool nullable:
NULL
51 changes: 51 additions & 0 deletions Zend/tests/typehints/scalar_null.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--TEST--
Scalar type hint nullability
--FILE--
<?php

$errnames = [
E_NOTICE => 'E_NOTICE',
E_WARNING => 'E_WARNING',
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR'
];
set_error_handler(function (int $errno, string $errmsg, string $file, int $line) use ($errnames) {
echo "$errnames[$errno]: $errmsg on line $line\n";
return true;
});

$functions = [
'int' => function (int $i) { return $i; },
'float' => function (float $f) { return $f; },
'string' => function (string $s) { return $s; },
'bool' => function (bool $b) { return $b; },
'int nullable' => function (int $i = NULL) { return $i; },
'float nullable' => function (float $f = NULL) { return $f; },
'string nullable' => function (string $s = NULL) { return $s; },
'bool nullable' => function (bool $b = NULL) { return $b; }
];

foreach ($functions as $type => $function) {
echo "Testing $type:", PHP_EOL;
var_dump($function(NULL));
}
--EXPECTF--
Testing int:
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type integer, null given, called in %s on line %d and defined on line %d
NULL
Testing float:
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type float, null given, called in %s on line %d and defined on line %d
NULL
Testing string:
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type string, null given, called in %s on line %d and defined on line %d
NULL
Testing bool:
E_RECOVERABLE_ERROR: Argument 1 passed to {closure}() must be of the type boolean, null given, called in %s on line %d and defined on line %d
NULL
Testing int nullable:
NULL
Testing float nullable:
NULL
Testing string nullable:
NULL
Testing bool nullable:
NULL
Loading