PHPUnit error "undefined index : HTTP_HOST"

12,145

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
12,145

Related videos on Youtube

Geek
Author by

Geek

iOS, Android

Updated on September 14, 2022

Comments

  • Geek
    Geek over 1 year

    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?

    • Geek
      Geek almost 11 years
      "undefined index : HTTP_HOST" this was the only error statement. I used bootstrap.php file and added the line $_SERVER['HTTP_HOST'] = 'myHost';. This solved the problem. I don't know much about bootstrap.php file. Can you brief me what is the use of this file and can I pass get and post parameters through this file to test files?