I have declared a HTTP_HOST as shown below.
public function testReadUser() {
$_SERVER['HTTP_HOST'] = "x.y";
.
.
.
}
Inspite of this, phpunit gives undefined index error. Why is it?
In your phpunit.xml
file, you can set server variables. Add the php
element under the phpunit
root:
<phpunit>
<php>
<server name='HTTP_HOST' value='http://localhost' />
</php>
</phpunit>
See the docs for more information.
It gives you that error because you're running the tests trough command line interface(CLI). CLI can't obtain that information because there are no requests coming in via HTTP.
You can declare the value (needed by the method your testing) in your test method.
For example:
function testMethod(){
$_SERVER['yourvar']='yourvalue';
...your code making the request via phpunit to the method you are testing
}
By declaring $_SERVER in your test method it will be available to the method you are testing. It works for $_POST and $_GET as well if you need those values those values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With