gpt4 book ai didi

methods - PHPUnit mocks - 调用断言方法

转载 作者:行者123 更新时间:2023-12-04 02:05:22 26 4
gpt4 key购买 nike

我是 phpunit 的新手,并且已经阅读了关于模拟对象的文档,但不是很清楚。

我正在尝试编写一个简单的测试来断言类中的方法被调用。使用以下代码,我正在测试调用 Client::exchangeArray 时,会调用 Client::getInputFilter。

class Client implements InputFilterAwareInterface
{

public function getInputFilter() {
if(!$this->_inputFilter){
$inputFactory = new InputFactory();
$inputFilter = new InputFilter();

$inputFilter->add($inputFactory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array(
'name' => 'Int'
)
)
)));

$inputFilter->add($inputFactory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
),
array(
'name' => 'StripNewLines'
),
array(
'name' => 'Alpha'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100
)
)
)
)));

$inputFilter->add($inputFactory->createInput(array(
'name' => 'surname',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100
)
)
)
)));

$inputFilter->add($inputFactory->createInput(array(
'name' => 'email',
'required' => false,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 150
)
),
array(
'name' => 'EmailAddress'
)
)
)));

$this->_inputFilter = $inputFilter;
}
return $this->_inputFilter;
}

public function exchangeArray($data){
$inputFilter = $this->getInputFilter();
$inputFilter->setData($data);
if(!$inputFilter->isValid()){
throw new \Exception('Invalid client data');
}

$cleanValues = $inputFilter->getValues();

$this->_id = (isset($cleanValues['id']) ? $cleanValues['id'] : null);
$this->_name = (isset($cleanValues['name']) ? $cleanValues['name'] : null);
$this->_surname = (isset($cleanValues['surname']) ? $cleanValues['surname'] : null);
$this->_email = (isset($cleanValues['email']) ? $cleanValues['email'] : null);
}
}

这是我的测试用例:
public function testExchangeArrayCallsInputFilter(){
$data = array('id' => 54,
'name' => 'john',
'surname' => 'doe',
'email' => 'john.doe@domain.com'
);

$mock = $this->getMock('Client', array('exchangeArray'));
$mock->expects($this->once())
->method('getInputFilter');
$mock->exchangeArray($data);
}

...我收到以下错误:

Expectation failed for method name is equal to when invoked 1 time(s). Method was expected to be called 1 times, actually called 0 times.



我哪里错了?

最佳答案

这完全取决于您想要测试什么以及您想要模拟什么。根据您的测试名称,我假设您想要测试 exchangeArray方法。
getMock方法将您想要模拟的方法的第二个参数名称作为第二个参数名称。这意味着他们永远不会被调用。

所以,如果你想测试 exchangeArray方法和模拟getInputFilter您应该在第二个参数中传递“getInputFilter”,如下所示:

$mock = $this->getMock('Client', array('getInputFilter'));
$mock->expects($this->once())
->method('getInputFilter');
$mock->exchangeArray($data);

不过要小心。你没有告诉你的模拟返回任何东西,所以它会返回空值。这意味着您将在 exchangeArray 的第二行出现 fatal error 方法(试图在非对象上调用方法)。您应该准备一些伪造的过滤器对象来处理,例如:
// $preparedFilterObject = ...
$mock = $this->getMock('Client', array('getInputFilter'));
$mock->expects($this->once())
->method('getInputFilter')
->will($this->returnValue($preparedFilterObject);
$mock->exchangeArray($data);

如果你想调用“真实” getInputFilter方法 - 那么你就不能模拟这个方法。

关于methods - PHPUnit mocks - 调用断言方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16101614/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com