8

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?

2 Answers 2

14

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()));
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like that is indeed the correct way. Check my edit. Maybe there's something else wrong? If you share some more complete piece of code, maybe others or I would be able to help.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.