17

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
  • 8,280
  • 17
  • 73
  • 137
  • Can you post every error that you get please? – N.B. Jul 20 '13 at 09:36
  • 1
    "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? – Geek Jul 20 '13 at 09:59
  • I could tell you, but googling "phpunit bootstrap" will give you tons of relevant results.. – N.B. Jul 20 '13 at 10:06
  • @N.B. Sure I'll. Thanks btw. – Geek Jul 20 '13 at 10:15

3 Answers3

35

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.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Reese
  • 1,746
  • 1
  • 17
  • 40
4

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.

  • but, it is `$_SERVER` variable. – srain Jul 20 '13 at 09:06
  • I use the same variable in same way in another test file of same project. I use netbeans and not command line. – Geek Jul 20 '13 at 09:08
  • 1
    From the manual: `HTTP_HOST - Contents of the Host: header from the current request, if there is one.`. That means there was no such header. It's the browser that sends that header. Configure your netbeans to send it when you run the tests. It's a piece information being read from the actual HTTP request, and if your client doesn't provide that information, PHP won't create the mentioned index in $_SERVER array. – user2601913 Jul 20 '13 at 09:10
  • I know I need to somehow tell netbeans about this variable. But unfortunately, I don't know how. Someone told me to do it by adding parameters in .ini file. Can you look at my previous question about adding parameter to use during tests. http://stackoverflow.com/questions/17759916/adding-parameters-to-ini-file-to-use-in-test-files-phpunit – Geek Jul 20 '13 at 09:14
  • 1
    INI file can't help you here. The manual says that those variables are created by the web server. That means you **have** to provide proper HTTP request that the server can use to create those. If you don't have that index in $_SERVER array, simply add it manually in your test files? I don't use netbeans so I can't really tell you what to do to configure it properly. It's apparently not performing any kind of HTTP request, it seems it's just using the CLI to run your test files. – user2601913 Jul 20 '13 at 09:18
  • That is what I am doing. The code in my question is of test file only. Is there any other work around? – Geek Jul 20 '13 at 09:22
2

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.

Don F
  • 1,963
  • 2
  • 15
  • 18