Skip to content

Commit 3fcece3

Browse files
committed
Add additional test coverage
1 parent 2ca1355 commit 3fcece3

4 files changed

+148
-0
lines changed

Diff for: Zend/tests/first_class_callable_dynamic.phpt

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Acquire callable through various dynamic constructs
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
public static function b($x) {
8+
return $x;
9+
}
10+
11+
public function c($x) {
12+
return $x;
13+
}
14+
}
15+
16+
$name = 'strlen';
17+
$fn = $name(...);
18+
var_dump($fn('x'));
19+
20+
$name = ['A', 'b'];
21+
$fn = $name(...);
22+
var_dump($fn(2));
23+
24+
$name = [new A, 'c'];
25+
$fn = $name(...);
26+
var_dump($fn(3));
27+
28+
$name1 = 'A';
29+
$name2 = 'b';
30+
$fn = $name1::$name2(...);
31+
var_dump($fn(4));
32+
33+
$name2 = 'c';
34+
$fn = (new A)->$name2(...);
35+
var_dump($fn(5));
36+
37+
$fn = [A::class, 'b'](...);
38+
var_dump($fn(6));
39+
40+
$o = new stdClass;
41+
$o->prop = A::b(...);
42+
($o->prop)(7);
43+
44+
$nam
45+
46+
?>
47+
--EXPECT--
48+
int(1)
49+
int(2)
50+
int(3)
51+
int(4)
52+
int(5)
53+
int(6)

Diff for: Zend/tests/first_class_callable_errors.phpt

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
Trying to acquire callable to something that's not callable
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
private static function privateMethod() {}
8+
9+
public function instanceMethod() {}
10+
}
11+
12+
try {
13+
$fn = 123;
14+
$fn(...);
15+
} catch (Error $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
try {
19+
does_not_exist(...);
20+
} catch (Error $e) {
21+
echo $e->getMessage(), "\n";
22+
}
23+
try {
24+
stdClass::doesNotExist(...);
25+
} catch (Error $e) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
try {
29+
(new stdClass)->doesNotExist(...);
30+
} catch (Error $e) {
31+
echo $e->getMessage(), "\n";
32+
}
33+
try {
34+
[new stdClass, 'doesNotExist'](...);
35+
} catch (Error $e) {
36+
echo $e->getMessage(), "\n";
37+
}
38+
try {
39+
Test::privateMethod(...);
40+
} catch (Error $e) {
41+
echo $e->getMessage(), "\n";
42+
}
43+
try {
44+
Test::instanceMethod(...);
45+
} catch (Error $e) {
46+
echo $e->getMessage(), "\n";
47+
}
48+
49+
?>
50+
--EXPECT--
51+
Value of type int is not callable
52+
Call to undefined function does_not_exist()
53+
Call to undefined method stdClass::doesNotExist()
54+
Call to undefined method stdClass::doesNotExist()
55+
Call to undefined method stdClass::doesNotExist()
56+
Call to private method Test::privateMethod() from global scope
57+
Non-static method Test::instanceMethod() cannot be called statically

Diff for: Zend/tests/first_class_callable_refs.phpt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
First class callables and references
3+
--FILE--
4+
<?php
5+
6+
function &id(&$x) {
7+
return $x;
8+
}
9+
10+
$fn = id(...);
11+
$i = 0;
12+
$i2 =& $fn($i);
13+
$i++;
14+
var_dump($i2);
15+
16+
?>
17+
--EXPECT--
18+
int(1)

Diff for: Zend/tests/first_class_callable_signature.phpt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
First class callables should retain the signature for reflection
3+
--FILE--
4+
<?php
5+
6+
function test(int $a, string &$b, Foo... $c) {}
7+
8+
echo new ReflectionFunction(test(...));
9+
10+
?>
11+
--EXPECTF--
12+
Closure [ <user> function test ] {
13+
@@ %s 3 - 3
14+
15+
- Parameters [3] {
16+
Parameter #0 [ <required> int $a ]
17+
Parameter #1 [ <required> string &$b ]
18+
Parameter #2 [ <optional> Foo ...$c ]
19+
}
20+
}

0 commit comments

Comments
 (0)