methods - PHPUnit mocks - 调用断言方法

标签 methods mocking phpunit

我是 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/

相关文章:

php - 如何真正对 Symfony 表单进行单元测试?

ios - 从另一个 ViewController2.m 文件调用在 ViewController1.m 中声明的方法

ruby-on-rails - Rails 3 如何使用 link_to 更改数据库中 bool 值的值?

unit-testing - 模拟 Grails 单元测试的方法

java - 如何在 Java 中读取和存储 API 调用/响应

docker - vagrant 上的 PHP docker 容器中的 PhpStorm 和 PHPUnit

java - "return this"的含义

JAva 获取类变量 : Multiple methods or one Method with switch

java - 如何模拟 super 方法调用

php - 模拟类时出现 Mockery TypeError