Creating a test environment with php cli

I want to use php in console mode and create an environment to test my functions.

I don't want to be forced to use a web browser and every time I want to test a function, I create a new file.

I want to access a function in the console and then return the result.

How to do it?

Update:

Perhaps I explained it badly. I just want to see what kind of result the function returns.

Maybe I need to learn unit testing, but at the moment I only need an interactive console that allows me to test all the functions one by one.

In my case, I need to load the wordpress functions (I know how to do this with a regular file .php

and then with a browser to parse the file), but I don't if it is possible to do it with php from the command line.

+1


source to share


4 answers


I've used phpsh in the past and found it to be very helpful. Once you run it chdir()

, you will need where your files are located, and then obviously require()

any files containing the functionality you need to test. Then you can just test your function calls by injecting them into the shell, eg.var_dump(some_function(1, 2));



+1


source


I think you should be more specific about exactly which functions. Wordpress doesn't provide something like this out of the box, most PHP applications won't.

I also think that you are causing problems here when such applications are not designed for such environments.

Here's an example that tries to call "current_time ()" from functions.php, and the attempts I had to make to implement 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

      

Attempt


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

      

Attempt


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 globals makes testing in other PITA environments if not impossible.

If this is not what you are trying to do, you should be more specific / detailed in your question.

+1


source


You want to read "Unit Testing" in a general sense and then try to apply it to PHP.

The structure you use (if any), the style of the code, and the tests you want to run will determine the exact methods you need to use. Only by first understanding the concept of Unit Tests and incorporating them into your best coding practices can you make progress in this regard.

0


source


What about:

php -a

      

And if you compile php with readline support it will be more interesting.

0


source







All Articles