From: andy wharmby <wharmby@php.net> Date: Sun, 14 Jun 2009 13:49:18 +0000 (+0000) Subject: Basic tests for function_exists() and get_defined_functions(). Tested on Windows... X-Git-Tag: php-5.4.0alpha1~191^2~3329 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fada257def560fdea281a16ce4e2569fd3444f9f;p=php Basic tests for function_exists() and get_defined_functions(). Tested on Windows, Linux and Linux 64 bit. --- diff --git a/Zend/tests/function_exists_basic.phpt b/Zend/tests/function_exists_basic.phpt new file mode 100644 index 0000000000..469e3d8c9a --- /dev/null +++ b/Zend/tests/function_exists_basic.phpt @@ -0,0 +1,39 @@ +--TEST-- +function_exists function : basic functionality +--FILE-- +<?php +/* + * proto bool function_exists(string function_name) + * Function is implemented in Zend/zend_builtin_functions.c +*/ + +echo "*** Testing function_exists() : basic functionality ***\n"; + +echo "Internal function: "; +var_dump(function_exists('function_exists')); + +echo "User defined function: "; +function f() {} +var_dump(function_exists('f')); + +echo "Case sensitivity: "; +var_dump(function_exists('F')); + +echo "Non existent function: "; +var_dump(function_exists('g')); + +echo "Method: "; +Class C { + static function f() {} +} +var_dump(function_exists('C::f')); +?> +===Done=== +--EXPECT-- +*** Testing function_exists() : basic functionality *** +Internal function: bool(true) +User defined function: bool(true) +Case sensitivity: bool(true) +Non existent function: bool(false) +Method: bool(false) +===Done=== diff --git a/Zend/tests/function_exists_error.phpt b/Zend/tests/function_exists_error.phpt new file mode 100644 index 0000000000..c95dc0718f --- /dev/null +++ b/Zend/tests/function_exists_error.phpt @@ -0,0 +1,37 @@ +--TEST-- +Test function_exists() function : error conditions +--INI-- +--FILE-- +<?php +/* + * proto bool function_exists(string function_name) + * Function is implemented in Zend/zend_builtin_functions.c +*/ + +echo "*** Testing function_exists() : error conditions ***\n"; + +$arg_0 = "ABC"; +$extra_arg = 1; + +echo "\nToo many arguments\n"; +var_dump(function_exists($arg_0, $extra_arg)); + +echo "\nToo few arguments\n"; +var_dump(function_exists()); + +?> +===Done=== +--EXPECTF-- +*** Testing function_exists() : error conditions *** + +Too many arguments + +Warning: function_exists() expects exactly 1 parameter, 2 given in %s on line %d +NULL + +Too few arguments + +Warning: function_exists() expects exactly 1 parameter, 0 given in %s on line %d +NULL +===Done=== + diff --git a/Zend/tests/function_exists_variation1.phpt b/Zend/tests/function_exists_variation1.phpt new file mode 100644 index 0000000000..b7da99fe1b --- /dev/null +++ b/Zend/tests/function_exists_variation1.phpt @@ -0,0 +1,138 @@ +--TEST-- +Test function_exists() function : usage variations - test values for $str argument +--FILE-- +<?php + +/* + * proto bool function_exists(string function_name) + * Function is implemented in Zend/zend_builtin_functions.c +*/ + +echo "*** Testing function_exists() function: with unexpected inputs for 'str' argument ***\n"; + +//get an unset variable +$unset_var = 'string_val'; +unset($unset_var); + +//defining a class +class sample { + public function __toString() { + return "sample object"; + } +} + +//getting the resource +$file_handle = fopen(__FILE__, "r"); + +// array with different values for $str +$inputs = array ( + + // integer values + 0, + 1, + 255, + 256, + PHP_INT_MAX, + -PHP_INT_MAX, + + // float values + 10.5, + -20.5, + 10.1234567e10, + + // array values + array(), + array(0), + array(1, 2), + + // boolean values + true, + false, + TRUE, + FALSE, + + // null values + NULL, + null, + + // objects + new sample(), + + // resource + $file_handle, + + // undefined variable + @$undefined_var, + + // unset variable + @$unset_var +); + +// loop through with each element of the $inputs array to test function_exists() function +$count = 1; +foreach($inputs as $input) { + echo "-- Iteration $count --\n"; + var_dump( function_exists($input) ); + $count ++; +} + +fclose($file_handle); //closing the file handle + +?> +===Done=== +--EXPECTF-- +*** Testing function_exists() function: with unexpected inputs for 'str' argument *** +-- Iteration 1 -- +bool(false) +-- Iteration 2 -- +bool(false) +-- Iteration 3 -- +bool(false) +-- Iteration 4 -- +bool(false) +-- Iteration 5 -- +bool(false) +-- Iteration 6 -- +bool(false) +-- Iteration 7 -- +bool(false) +-- Iteration 8 -- +bool(false) +-- Iteration 9 -- +bool(false) +-- Iteration 10 -- + +Warning: function_exists() expects parameter 1 to be string (Unicode or binary), array given in %s on line %d +NULL +-- Iteration 11 -- + +Warning: function_exists() expects parameter 1 to be string (Unicode or binary), array given in %s on line %d +NULL +-- Iteration 12 -- + +Warning: function_exists() expects parameter 1 to be string (Unicode or binary), array given in %s on line %d +NULL +-- Iteration 13 -- +bool(false) +-- Iteration 14 -- +bool(false) +-- Iteration 15 -- +bool(false) +-- Iteration 16 -- +bool(false) +-- Iteration 17 -- +bool(false) +-- Iteration 18 -- +bool(false) +-- Iteration 19 -- +bool(false) +-- Iteration 20 -- + +Warning: function_exists() expects parameter 1 to be string (Unicode or binary), resource given in %s on line %d +NULL +-- Iteration 21 -- +bool(false) +-- Iteration 22 -- +bool(false) +===Done=== + \ No newline at end of file diff --git a/Zend/tests/get_defined_functions_basic.phpt b/Zend/tests/get_defined_functions_basic.phpt new file mode 100644 index 0000000000..a849e32c78 --- /dev/null +++ b/Zend/tests/get_defined_functions_basic.phpt @@ -0,0 +1,59 @@ +--TEST-- +get_defined_functions() function : basic functionality +--FILE-- +<?php + +/* Prototype : array get_defined_functions ( void ) + * Description: Gets an array of all defined functions. + * Source code: Zend/zend_builtin_functions.c +*/ + +echo "*** Testing get_defined_functions() : basic functionality ***\n"; + +function foo() {} + +// mixed case function +function HelloWorld() {} + +Class C { + function f1() {} + static function f2() {} +} + +$func = get_defined_functions(); + +if (!is_array($func)) { + echo "TEST FAILED: return type not an array\n"; +} + + +if (!is_array($func["internal"])) { + echo "TEST FAILED: no element in result array with key 'internal'\n"; +} + +$internal = $func["internal"]; + +//check for a few core functions +if (!in_array("cos", $internal) || !in_array("strlen", $internal)) { + echo "TEST FAILED: missing elements from 'internal' array\n"; + var_dump($internal); +} + +if (!is_array($func["user"])) { + echo "TEST FAILED: no element in result array with key 'user'\n"; +} + +$user = $func["user"]; +if (count($user) == 2 && in_array("foo", $user) && in_array("helloworld", $user)) { + echo "TEST PASSED\n"; +} else { + echo "TEST FAILED: missing elements from 'user' array\n"; + var_dump($user); +} + +?> +===Done=== +--EXPECT-- +*** Testing get_defined_functions() : basic functionality *** +TEST PASSED +===Done=== diff --git a/Zend/tests/get_defined_functions_error.phpt b/Zend/tests/get_defined_functions_error.phpt new file mode 100644 index 0000000000..fcb01e031e --- /dev/null +++ b/Zend/tests/get_defined_functions_error.phpt @@ -0,0 +1,29 @@ +--TEST-- +Test get_defined_functions() function : error conditions +--FILE-- +<?php + +/* Prototype : array get_defined_functions ( void ) + * Description: Gets an array of all defined functions. + * Source code: Zend/zend_builtin_functions.c +*/ + + +echo "*** Testing get_defined_functions() : error conditions ***\n"; + + +echo "\n-- Testing get_defined_functions() function with more than expected no. of arguments --\n"; +$extra_arg = 10; +var_dump( get_defined_functions($extra_arg) ); + +?> +===Done=== +--EXPECTF-- +*** Testing get_defined_functions() : error conditions *** + +-- Testing get_defined_functions() function with more than expected no. of arguments -- + +Warning: get_defined_functions() expects exactly 0 parameters, 1 given in %s on line %d +NULL + +===Done===