views:

388

answers:

7

I would use php in console mode and create an environment to test directly my functions. In fact i would not be force to use a web browser and create a new file each time i want to test a function. I want to get in input the function in the console and then it return the result. So can someone if it is possible and how do i do?

A: 

You are going to want to read up on "Unit Testing" in a generic sense and then try and apply them to PHP.

The framework you are using (if any), the style of code, and the tests you want to run are going to determine the exact methods you need to use. Only by first understanding the concept of Unit Tests and implementing them into your coding best-practices will you be able to make progress in this regard.

Nolte Burke
A: 

Maybe i have to learn unit testing but for the moment i only want an interactive console, which allows me to test all functions one by one. In my case i have to load the wordpress functions (i know how do it with a regular .php file and then a browser to parse the file) but i don't if it is possible to do it with php cli. regards

toddoon
A: 

Perhaps i have badly explained, because i don't want to check if the functions (it is wordpress functions) i only want to see what result it returns.

toddoon
A: 

How about:

php -a

And if you compile php with readline support it'll be more fancy.

PEZ
+1  A: 

I guess you've to be more specific what kind of functions exactly. Wordpress does not provide something like that out of the box, most PHP apps won't.

I also think you're calling for trouble here when such apps aren't developed in mind for such environments.

Here's an example trying to call "current_time()" from functions.php and the attempts I had to do just to realize it won't work that way:


php -r 'require "functions.php"; var_dump(current_time("mysql"));'

gives


Fatal error: Call to undefined function apply_filters() in functions.php on line 346

Trying


php -r 'require "functions.php"; require "plugin.php"; var_dump(current_time("mysql"));'

gives


Fatal error: Call to undefined function wp_cache_get() in functions.php on line 351

Trying


php -r 'require "functions.php"; require "plugin.php"; require "cache.php"; var_dump(current_time("mysql"));'

gives


Fatal error: Call to a member function get() on a non-object in cache.php on line 93

Looking at the last error in the source I see


 function wp_cache_get($id, $flag = '') {
     global $wp_object_cache;

     return $wp_object_cache->get($id, $flag);
 }

Using global variables makes testing in other environments a PITA if not impossible.

If this is not what you're trying to do, you've to be more specific/detailed in your question.

mark
+1  A: 

I have used phpsh in the past and found it very useful. Once you start it you will need to chdir() to where your files are and then obviously require() any files containing functions you need to test. You can then just test your function calls by typing them into the shell e.g. var_dump(some_function(1, 2));

Tom Haigh
A: 

ok great thx to all and particullary mfn, because if you load wp-load.php and execute a function in works well!

toddoon
This is not an answer. Use a comment or edit your question.
Geoffrey Chetwood