1
0
mirror of https://github.com/php/php-src.git synced 2026-04-28 02:33:17 +02:00

Basic tests for function_exists() and get_defined_functions(). Tested on Windows, Linux and Linux 64 bit.

This commit is contained in:
andy wharmby
2009-06-14 13:49:19 +00:00
parent 9e6779cfac
commit e4ba76cdec
5 changed files with 299 additions and 0 deletions
+39
View File
@@ -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===
+37
View File
@@ -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: Wrong parameter count for function_exists() in %s on line %d
NULL
Too few arguments
Warning: Wrong parameter count for function_exists() in %s on line %d
NULL
===Done===
+135
View File
@@ -0,0 +1,135 @@
--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 --
Notice: Array to string conversion in %s on line %d
bool(false)
-- Iteration 11 --
Notice: Array to string conversion in %s on line %d
bool(false)
-- Iteration 12 --
Notice: Array to string conversion in %s on line %d
bool(false)
-- 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 --
bool(false)
-- Iteration 21 --
bool(false)
-- Iteration 22 --
bool(false)
===Done===
@@ -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===
@@ -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: Wrong parameter count for get_defined_functions() in %s on line %d
NULL
===Done===