在PHPUnit模拟对象中配置多个方法

在PHPUnit模拟对象中配置多个方法,php,phpunit,Php,Phpunit,我正在尝试用PHP和PHPUnit创建一个模拟对象。到目前为止,我有: $object = $this->getMock('object', array('set_properties', 'get_events'), array(), 'object_test',

我正在尝试用PHP和PHPUnit创建一个模拟对象。到目前为止,我有:

$object = $this->getMock('object',
                         array('set_properties',
                               'get_events'),
                         array(),
                         'object_test',
                         null);

$object
    ->expects($this->once())
    ->method('get_events')
    ->will($this->returnValue(array()));

$mo = new multiple_object($object);
暂时忽略我那令人毛骨悚然的模糊对象名称,我明白我所做的是
-创建了一个模拟对象,需要配置两个方法,
-将“获取事件”方法配置为返回空白数组,并且
-将模拟放入构造函数中

我现在想做的是配置第二个方法,但我找不到任何解释如何做到这一点的内容。我想做一些像

$object
    ->expects($this->once())
    ->method('get_events')
    ->will($this->returnValue(array()))
    ->expects($this->once())
    ->method('set_properties')
    ->with($this->equalTo(array()))
或者类似的,但这不起作用。我该怎么做


切题地说,如果我需要配置多个方法进行测试,这是否表明我的代码结构很差?

我对PHPUnit没有任何经验,但我的猜测是这样的:

$object
  ->expects($this->once())
  ->method('get_events')
  ->will($this->returnValue(array()));
$object
  ->expects($this->once())
  ->method('set_properties')
  ->with($this->equalTo(array()));
public function testMailForUidOrMail()
{
    $ldap = $this->getMock('Horde_Kolab_Server_ldap', array('_getAttributes',
                                                            '_search', '_count',
                                                            '_firstEntry'));
    $ldap->expects($this->any())
        ->method('_getAttributes')
        ->will($this->returnValue(array (
                                      'mail' =>
                                      array (
                                          'count' => 1,
                                          0 => 'wrobel@example.org',
                                      ),
                                      0 => 'mail',
                                      'count' => 1)));
    $ldap->expects($this->any())
        ->method('_search')
        ->will($this->returnValue('cn=Gunnar Wrobel,dc=example,dc=org'));
    $ldap->expects($this->any())
        ->method('_count')
        ->will($this->returnValue(1));
    $ldap->expects($this->any())
        ->method('_firstEntry')
        ->will($this->returnValue(1));
(...)
}
你已经试过了吗


编辑:

好的,通过一些代码搜索,我找到了一些可能对您有所帮助的示例

检查这个

他们这样使用它:

$object
  ->expects($this->once())
  ->method('get_events')
  ->will($this->returnValue(array()));
$object
  ->expects($this->once())
  ->method('set_properties')
  ->with($this->equalTo(array()));
public function testMailForUidOrMail()
{
    $ldap = $this->getMock('Horde_Kolab_Server_ldap', array('_getAttributes',
                                                            '_search', '_count',
                                                            '_firstEntry'));
    $ldap->expects($this->any())
        ->method('_getAttributes')
        ->will($this->returnValue(array (
                                      'mail' =>
                                      array (
                                          'count' => 1,
                                          0 => 'wrobel@example.org',
                                      ),
                                      0 => 'mail',
                                      'count' => 1)));
    $ldap->expects($this->any())
        ->method('_search')
        ->will($this->returnValue('cn=Gunnar Wrobel,dc=example,dc=org'));
    $ldap->expects($this->any())
        ->method('_count')
        ->will($this->returnValue(1));
    $ldap->expects($this->any())
        ->method('_firstEntry')
        ->will($this->returnValue(1));
(...)
}
也许你的问题在别的地方

如果有帮助,请告诉我


编辑2:

你能试试这个吗

$object = $this->getMock('object', array('set_properties','get_events'));

$object
  ->expects($this->once())
  ->method('get_events')
  ->will($this->returnValue(array()));
$object
  ->expects($this->once())
  ->method('set_properties')
  ->with($this->equalTo(array()));

看来这确实是正确的方法。检查我的编辑。也许还有别的问题?如果您分享一些更完整的代码,也许其他人或我可以提供帮助。