Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit error "undefined index : HTTP_HOST"

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?

like image 556
Geek Avatar asked Jul 20 '13 09:07

Geek


3 Answers

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.

like image 177
Reese Avatar answered Nov 18 '22 07:11

Reese


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.

like image 21
user2601913 Avatar answered Nov 18 '22 07:11

user2601913


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.

like image 2
Don F Avatar answered Nov 18 '22 07:11

Don F