Reputation: 8320
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?
Upvotes: 17
Views: 13645
Reputation: 1775
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.
Upvotes: 35
Reputation: 2023
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.
Upvotes: 2
Reputation: 91
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.
Upvotes: 4