TR.
TR.

Reputation: 2215

Configuring multiple methods in PHPUnit mock objects

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?

Upvotes: 8

Views: 13998

Answers (2)

BloodhunterD
BloodhunterD

Reputation: 141

The people looking for a solution to call the "same" method on the mock object multiple times, possibly with different parameters and return values, can use @Cody A. Ray's answer from this post.

Here is the answer from the post in case the links ever become invalid:

For others who are looking to both match input parameters and provide return values for multiple calls.. this works for me:

$mock
  ->method('myMockedMethod')
  ->withConsecutive([$argA1, $argA2], [$argB1, $argB2], [$argC1, $argC2])
  ->willReturnOnConsecutiveCalls($retValue1, $retValue2, $retValue3);

Upvotes: 0

Carlos Lima
Carlos Lima

Reputation: 4182

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

Upvotes: 15

Related Questions