PHPUnit davalarını nesneleri birden çok yöntem yapılandırma

1 Cevap php

PHP ve PHPUnit bir davalarını nesnesi oluşturmak için çalışıyorum. Şimdiye kadar, ben bu var:

$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);

Ignoring my hideously ambiguous object names for the minute, I understand that what I've done is
- Created a mock object, with 2 methods to configure,
- Configured the 'get_events' method to return a blank array, and
- Dropped the mock into the constructor.

Ne şimdi yapmak istiyorum ikinci yöntemini yapılandırmak, ama ben bunu nasıl açıklayan bir şey bulamıyorum. Ben böyle bir şey yapmak istiyorum

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

ya da böyle, ama bu işe yaramazsa. Bunu nasıl yapmalıyım?

Ben test etmek için yapılandırılmış birden fazla yöntemi gerekiyorsa teğet, bu, ben kötü kodumu yapılandırılmış ettik gösterir?

1 Cevap

Ben PHPUnit ile herhangi bir deneyimi yok, ama benim tahminim bu gibi bir şey olacaktır:

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

Zaten bunu denediniz mi?


Edit:

Tamam, bazı kod arama yaparak, ben size yardımcı olabilecek bazı örnekler bulundu

Bu kontrol example

Onlar bu gibi kullanmak:

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));
(...)
}

Belki sorun başka bir yerde mi?

Bu yardım varsa bana bildirin.


Edit2:

Bu deneyebilirsiniz:

$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()));