PHPUnit | Testing json return

I am very new to testing PHPUnit and I need help if possible.

I installed the plugin in WordPress for unit testing, which is based on the PHPUnit Framework. I am currently creating a WordPress plugin using AJAX calls to interact with WordPress data.

In my plugin, I created a class that creates some add_action ('wp_ajax_actionname', array (__ CLASS__, 'functionName'))

functionName looks like this:

function functionName() { global $wpdb; if(wp_verify_nonce($_POST['s'], 'cdoCountryAjax') != false) { $zones = $wpdb->get_results( $wpdb->prepare( " SELECT zone_id AS ID, name AS Name FROM " . $wpdb->prefix . "cdo_zone WHERE country_id = %d ", $_POST['id'] ) ); header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json'); $results = array(); foreach($zones as $zone) { $results[$zone->ID] = $zone->Name; } echo json_encode($results); } die(-1); } 

The above function returns the query results returned in Object, and I echoed using the json_encode function.

The question is, how can I check the above method? Is there any way to check this?

+4
source share
2 answers

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(); // you will need cli php installed for this, on windows this would be php.exe at the front $results = exec('php tested_function_runner.php', $output); // start asserting here } 

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 .

+5
source

When you feel like this, please also take a look at the PHPUnit output testing functions, they are great.

(As complex857 said, there are many bits and parts for this problem, but for test testing, relying on this built-in PHPUnit function.)

The manual is eloquent and useful: https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.output

+5
source

Source: https://habr.com/ru/post/1487822/


All Articles