【发布时间】:2021-08-23 07:14:41
【问题描述】:
我刚接触 TDD,我已经用 PhpStorm 安装了 PHPUnit。
我有这个类和函数,我想测试IP地址匹配。
class ip_request
{
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}
我正在尝试为测试创建代码,但我不确定要输入什么。
我试过了
public function testGetRealIpAddr()
{
$expected = '127.0.0.1';
$this->assertEquals($expected, $test);
}
但是$test 显然是未定义的。
接下来我该怎么做?
给出的尝试建议
public function testGetRealIpAddr()
{
$UserIpAddress = $this
->getMockBuilder('ip_request')
->getMock();
$UserIpAddress->expects($this->once())
->method('getRealIpAddr')
->willReturn('127.0.0.1'); // Set ip address whatever you want to use
}
但我现在得到的错误是
Trying to configure method "getRealIpAddr()" which cannot be configured because it does not exist, has not been specified, is final, or is static
【问题讨论】: