Setting $_POST for filter_input_array(INPUT_POST) in phpunit test
If the input to filter_input_array
can only be set by the initial request, and not changed at run time, then the only way to still test it is to have a your base test proxy to another test script by making an HTTP request with the right POST data and processing the response.
main_test.php:
<?php
$data = array(
'testname' => 'yourtestname',
'some_post_var' => 'some_value'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/proxy_test.php");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if ($response == 'pass') {
// test passed
}
proxy_test.php
<?php
$test_name $_POST['testname']; // what test to run?
$response = run_test($test_name); // run test that uses filter_input_array
if ($response) {
echo "pass"; // used by main_test.php to check if test passed
} else {
echo "fail";
}
It seems like this is a limitation of PHP, filter_input_array()
does not allow a $_POST
array modified at runtime. See this bug for some more information. The workaround is probably to use one of the other filter functions and pass in the $_POST
array yourself.