Skip to content

Commit 071a3c3

Browse files
author
Steve Seear
committed
call_user_func_array() tests
1 parent b099463 commit 071a3c3

File tree

3 files changed

+452
-0
lines changed

3 files changed

+452
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
call_user_func_array() passes by reference if the array element is referenced, regardless of function signature.
3+
--FILE--
4+
<?php
5+
6+
function by_val($arg) {
7+
$arg = 'changed';
8+
}
9+
10+
function by_ref(&$arg) {
11+
$arg = 'changed';
12+
}
13+
14+
echo "------ Calling by_val() with unreferenced argument ------\n";
15+
$arg = array('original');
16+
call_user_func_array('by_val', $arg);
17+
var_dump($arg);
18+
19+
echo "------ Calling by_ref() with unreferenced argument ------\n";
20+
$arg = array('original');
21+
call_user_func_array('by_ref', $arg);
22+
var_dump($arg);
23+
24+
echo "------ Calling by_val() with referenced argument ------\n";
25+
$arg = array('original');
26+
$ref = &$arg[0];
27+
call_user_func_array('by_val', $arg);
28+
var_dump($arg);
29+
30+
echo "------ Calling by_ref() with referenced argument ------\n";
31+
$arg = array('original');
32+
$ref = &$arg[0];
33+
call_user_func_array('by_ref', $arg);
34+
var_dump($arg);
35+
36+
?>
37+
--EXPECTF--
38+
------ Calling by_val() with unreferenced argument ------
39+
array(1) {
40+
[0]=>
41+
string(8) "original"
42+
}
43+
------ Calling by_ref() with unreferenced argument ------
44+
array(1) {
45+
[0]=>
46+
string(8) "original"
47+
}
48+
------ Calling by_val() with referenced argument ------
49+
array(1) {
50+
[0]=>
51+
&string(7) "changed"
52+
}
53+
------ Calling by_ref() with referenced argument ------
54+
array(1) {
55+
[0]=>
56+
&string(7) "changed"
57+
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
--TEST--
2+
Test call_user_func_array() function : first parameter variation
3+
--FILE--
4+
<?php
5+
/* Prototype : mixed call_user_func_array(string function_name, array parameters)
6+
* Description: Call a user function which is the first parameter with the arguments contained in array
7+
* Source code: ext/standard/basic_functions.c
8+
* Alias to functions:
9+
*/
10+
11+
echo "*** Testing call_user_func_array() : usage variation ***\n";
12+
13+
// Define error handler
14+
function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
15+
if (error_reporting() != 0) {
16+
// report non-silenced errors
17+
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
18+
}
19+
}
20+
set_error_handler('test_error_handler');
21+
22+
// Initialise function arguments not being substituted (if any)
23+
$parameters = array(1, 2);
24+
25+
//get an unset variable
26+
$unset_var = 10;
27+
unset ($unset_var);
28+
29+
// define some classes
30+
class classWithToString
31+
{
32+
public function __toString() {
33+
return "Class A object";
34+
}
35+
}
36+
37+
class classWithoutToString
38+
{
39+
}
40+
41+
// heredoc string
42+
$heredoc = <<<EOT
43+
hello world
44+
EOT;
45+
46+
// add arrays
47+
$index_array = array (1, 2, 3);
48+
$assoc_array = array ('one' => 1, 'two' => 2);
49+
50+
//array of values to iterate over
51+
$inputs = array(
52+
53+
// int data
54+
'int 0' => 0,
55+
'int 1' => 1,
56+
'int 12345' => 12345,
57+
'int -12345' => -2345,
58+
59+
// float data
60+
'float 10.5' => 10.5,
61+
'float -10.5' => -10.5,
62+
'float 12.3456789000e10' => 12.3456789000e10,
63+
'float -12.3456789000e10' => -12.3456789000e10,
64+
'float .5' => .5,
65+
66+
// array data
67+
'empty array' => array(),
68+
'int indexed array' => $index_array,
69+
'associative array' => $assoc_array,
70+
'nested arrays' => array('foo', $index_array, $assoc_array),
71+
72+
// null data
73+
'uppercase NULL' => NULL,
74+
'lowercase null' => null,
75+
76+
// boolean data
77+
'lowercase true' => true,
78+
'lowercase false' =>false,
79+
'uppercase TRUE' =>TRUE,
80+
'uppercase FALSE' =>FALSE,
81+
82+
// empty data
83+
'empty string DQ' => "",
84+
'empty string SQ' => '',
85+
86+
// object data
87+
'instance of classWithToString' => new classWithToString(),
88+
'instance of classWithoutToString' => new classWithoutToString(),
89+
90+
// undefined data
91+
'undefined var' => @$undefined_var,
92+
93+
// unset data
94+
'unset var' => @$unset_var,
95+
);
96+
97+
// loop through each element of the array for function_name
98+
99+
foreach($inputs as $key =>$value) {
100+
echo "\n--$key--\n";
101+
var_dump( call_user_func_array($value, $parameters) );
102+
};
103+
104+
?>
105+
===DONE===
106+
--EXPECTF--
107+
*** Testing call_user_func_array() : usage variation ***
108+
109+
--int 0--
110+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '0' was given, %s(%d)
111+
NULL
112+
113+
--int 1--
114+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '1' was given, %s(%d)
115+
NULL
116+
117+
--int 12345--
118+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '12345' was given, %s(%d)
119+
NULL
120+
121+
--int -12345--
122+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '-2345' was given, %s(%d)
123+
NULL
124+
125+
--float 10.5--
126+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '10.5' was given, %s(%d)
127+
NULL
128+
129+
--float -10.5--
130+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '-10.5' was given, %s(%d)
131+
NULL
132+
133+
--float 12.3456789000e10--
134+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '123456789000' was given, %s(%d)
135+
NULL
136+
137+
--float -12.3456789000e10--
138+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '-123456789000' was given, %s(%d)
139+
NULL
140+
141+
--float .5--
142+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '0.5' was given, %s(%d)
143+
NULL
144+
145+
--empty array--
146+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, 'Array' was given, %s(%d)
147+
NULL
148+
149+
--int indexed array--
150+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, 'Array' was given, %s(%d)
151+
NULL
152+
153+
--associative array--
154+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, 'Array' was given, %s(%d)
155+
NULL
156+
157+
--nested arrays--
158+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, 'Array' was given, %s(%d)
159+
NULL
160+
161+
--uppercase NULL--
162+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '' was given, %s(%d)
163+
NULL
164+
165+
--lowercase null--
166+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '' was given, %s(%d)
167+
NULL
168+
169+
--lowercase true--
170+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '1' was given, %s(%d)
171+
NULL
172+
173+
--lowercase false--
174+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '' was given, %s(%d)
175+
NULL
176+
177+
--uppercase TRUE--
178+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '1' was given, %s(%d)
179+
NULL
180+
181+
--uppercase FALSE--
182+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '' was given, %s(%d)
183+
NULL
184+
185+
--empty string DQ--
186+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '' was given, %s(%d)
187+
NULL
188+
189+
--empty string SQ--
190+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '' was given, %s(%d)
191+
NULL
192+
193+
--instance of classWithToString--
194+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, 'Class A object' was given, %s(%d)
195+
NULL
196+
197+
--instance of classWithoutToString--
198+
Error: 4096 - Object of class classWithoutToString could not be converted to string, %s(%d)
199+
Error: 8 - Object of class classWithoutToString to string conversion, %s(%d)
200+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, 'Object' was given, %s(%d)
201+
NULL
202+
203+
--undefined var--
204+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '' was given, %s(%d)
205+
NULL
206+
207+
--unset var--
208+
Error: 2 - call_user_func_array(): First argument is expected to be a valid callback, '' was given, %s(%d)
209+
NULL
210+
===DONE===

0 commit comments

Comments
 (0)