如何phpunit测试$ _SERVER ['REMOTE_ADDR']

时间:2013-10-17 16:51:10

标签: zend-framework2 phpunit

我正在运行Zf2并且在我的控制器中有行

$apiCaller = $_SERVER['REMOTE_ADDR'];

当我运行PHPUnit 3.7.27时,它会抛出以下错误,

There was 1 error:

1) MainTest\Controller\myControllerTest::testcreateActionCanBeAccessed
Undefined index: REMOTE_ADDR

/home/websites/mysite/module/Main/src/Main/Controller/myController.php:113

我如何对这条线进行单元测试?

1 个答案:

答案 0 :(得分:1)

感谢@Mike B,

将代码行更改为以下

  $servParam = $this->getRequest()->getServer();
  $apiCaller = $servParam->get('REMOTE_ADDR');