There are two things that are not suitable for testing that you have to deal with:
The output generation is echoing. To do this, you can bind a function call inside the pair ob_start() ... ob_end_clean() to get an output that would be an echo.
Edit :
As it turned out, the library already has built-in support, see the "Testing Output" section of the manual .
Another problem you have to deal with is die(-1) at the end. You can use the set_exit_overload() function specified in the php test helpers to disable its effect, so your test process will not die with the code. This is a bit more complicated to set up (you will need a C compiler). If this does not work for you, you may be out of luck if you cannot change the code to something more test-friendly. (I'm not very familiar with wordpress, but for ajax plugins this use of die() seems to be recommended ). As a last resort, you can try running the script as a subprocess using popen() or exec() and get the result this way (you have to write a file that includes the source and calls a function that won't be tested).
Ideally, it would look something like this:
function test_some_wp_plugin_test() { // deal with the die() set_exit_overload(function() { return false; }); // set expectation on the output $expected_result = array('foo' => 'bar'); $this->expectOutputString(json_encode($expected_result)); // run function under the testing function_in_test(); }
In the worst case, it could be something like:
function test_some_wp_plugin_test() { $output = array();
And inside tested_function_runner.php :
include 'path/to/the/plugin.php'; function_under_test();
You can, of course, make this script runner more general with the parameters passed and used from $argv .
source share