1

I am trying to declare an expectation that has two possible values as a parameter, based on the value I want the return value to be different.

Here is what I tried

$mock = m::mock('FooBar\ClassA');
$mock->shouldReceive('has')->with('foo')->andReturn(false);
$mock->shouldReceive('has')->with('bar')->andReturn(true);

I get this error

Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_2__FooBar_ClassA::has("bar")

I read through http://docs.mockery.io/en/latest/reference/expectations.html

but I am unable to find the answer. I am trying to resort to solving the problem with andReturnUsing which is straight-forward. Is there a way to solve the problem without using andReturnUsing?

$mock->shouldReceive('has')->andReturnUsing(function ($value) {
    switch ($value) {
    case 'foo': return false;
    case 'bar': return true;
    }
});

I also read Mockery specifying expected arguments for multiple calls

But that's the same thing I have tried, it forces validation on parameter type.

halfer
  • 19,824
  • 17
  • 99
  • 186
DevZer0
  • 13,433
  • 7
  • 27
  • 51

1 Answers1

1

ok i found the answer

$mock->shouldReceive('has')->withArgs(['foo'])->andReturnValues([false]);
$mock->shouldReceive('has')->withArgs(['bar'])->andReturnValues([true]);

since withArgs is just a wrapper around with i think it is also possible to use with, but i think the param has to be inside an array

DevZer0
  • 13,433
  • 7
  • 27
  • 51