]> granicus.if.org Git - php/blob
f1686d7acd
[php] /
1 --TEST--
2 Test array_walk_recursive() function : error conditions - callback parameters
3 --FILE--
4 <?php
5 /* Prototype  : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
6  * Description: Apply a user function to every member of an array
7  * Source code: ext/standard/array.c
8 */
9
10 /*
11  * Testing array_walk_recursive() by passing more number of parameters to callback function
12  */
13 $input = array(1);
14
15 function callback1($value, $key, $user_data ) {
16   echo "\ncallback1() invoked \n";
17 }
18
19 function callback2($value, $key, $user_data1, $user_data2) {
20   echo "\ncallback2() invoked \n";
21 }
22 echo "*** Testing array_walk_recursive() : error conditions - callback parameters ***\n";
23
24 // expected: Missing argument Warning
25 try {
26     var_dump( array_walk_recursive($input, "callback1") );
27 } catch (Throwable $e) {
28     echo "Exception: " . $e->getMessage() . "\n";
29 }
30 try {
31     var_dump( array_walk_recursive($input, "callback2", 4) );
32 } catch (Throwable $e) {
33     echo "Exception: " . $e->getMessage() . "\n";
34 }
35
36 // expected: Warning is suppressed
37 try {
38     var_dump( @array_walk_recursive($input, "callback1") );
39 } catch (Throwable $e) {
40     echo "Exception: " . $e->getMessage() . "\n";
41 }
42 try {
43     var_dump( @array_walk_recursive($input, "callback2", 4) );
44 } catch (Throwable $e) {
45     echo "Exception: " . $e->getMessage() . "\n";
46 }
47
48 echo "-- Testing array_walk_recursive() function with too many callback parameters --\n";
49 try {
50     var_dump( array_walk_recursive($input, "callback1", 20, 10) );
51 } catch (Throwable $e) {
52     echo "Exception: " . $e->getMessage() . "\n";
53 }
54
55 echo "Done";
56 ?>
57 --EXPECTF--
58 *** Testing array_walk_recursive() : error conditions - callback parameters ***
59 Exception: Too few arguments to function callback1(), 2 passed and exactly 3 expected
60 Exception: Too few arguments to function callback2(), 3 passed and exactly 4 expected
61 Exception: Too few arguments to function callback1(), 2 passed and exactly 3 expected
62 Exception: Too few arguments to function callback2(), 3 passed and exactly 4 expected
63 -- Testing array_walk_recursive() function with too many callback parameters --
64 Exception: array_walk_recursive() expects at most 3 parameters, 4 given
65 Done