Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring multiple methods in PHPUnit mock objects

Tags:

php

phpunit

I am trying to create a mock object in PHP and PHPUnit. So far, I have this:

$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.

What I'd like to do now is configure the second method, but I can't find anything explaining how to do that. I want to do something like

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

or some such, but that doesn't work. How should I do that?

Tangentially, does this indicate I've structured my code poorly, if I need to configured more than one method to test?

like image 570
TR. Avatar asked Nov 13 '09 01:11

TR.


1 Answers

I don't have any experience with PHPUnit, but my guess would be something like this:

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

Have you tried it already?


Edit:

Ok, by doing some code search, I found some examples that might help you out

Check this example

They use it like this:

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 => '[email protected]',
                                      ),
                                      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));
(...)
}

Maybe your problem is somewhere else?

Let me know if that helped.


Edit2:

Can you try this:

$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()));
like image 69
Carlos Lima Avatar answered Nov 10 '22 08:11

Carlos Lima