73

This is what I have:

$observer = $this->getMock('SomeObserverClass', array('method'));
$observer->expects($this->once())
         ->method('method')
         ->with($this->equalTo($arg1));

But the method should take two parameters. I am only testing that the first parameter is being passed correctly (as $arg1).

How do test the second parameter?

Paige Ruten
  • 172,675
  • 36
  • 177
  • 197
Joel
  • 11,431
  • 17
  • 62
  • 72

1 Answers1

113

I believe the way to do this is:

$observer->expects($this->once())
     ->method('method')
     ->with($this->equalTo($arg1),$this->equalTo($arg2));

Or

$observer->expects($this->once())
     ->method('method')
     ->with($arg1, $arg2);

If you need to perform a different type of assertion on the 2nd arg, you can do that, too:

$observer->expects($this->once())
     ->method('method')
     ->with($this->equalTo($arg1),$this->stringContains('some_string'));

If you need to make sure some argument passes multiple assertions, use logicalAnd()

$observer->expects($this->once())
     ->method('method')
     ->with($this->logicalAnd($this->stringContains('a'), $this->stringContains('b')));
silfreed
  • 1,243
  • 1
  • 9
  • 6
  • 1
    I ran into this a couple weeks ago. Using: ->with($this->equalTo($foo, $bar) Worked for me. – ieure Dec 13 '08 at 22:40
  • 8
    @ieure The second argument to equalTo() is $delta, so that probably doesn't do what you think it does. – Nate Bundy Sep 12 '13 at 16:36