How to throw an exception on the Nth call of a mock method with Mockery
Asked Answered
C

2

11

I need to test how some code I wrote will behave when it calls a method on another class multiple times, where one of the calls will cause an exception to be thrown.

I am using Mockery to mock the class that may throw an exception.

So in my case, the method will be called three times and I need it throw an exception on the second time.

This is example of my intention but it doesn't work.

$mock = \Mockery::mock();
$mock->shouldReceive('fetch')
    ->andReturnUsing(
        function () {return true;},
        function () use ($e) {throw new \Exception();},
        function () {return false;}
    );

I was given the impression the above might work from the response in Asserting that mock throws exception · Issue #308 · mockery/mockery.

However, in practice, throwing an exception this way causes Mockery to catch the exception and throw its own BadMethodCall exception.

Cymbal answered 17/6, 2018 at 22:5 Comment(0)
C
28

I found the answer amongst the Mockery Github Issues, Mock multiple method call with return and throw.

$mock = \Mockery::mock();
$mock->shouldReceive('fetch')
    ->andReturnUsing(
        function () use () {
            static $counter = 0;

            switch ($counter++) {
                case 0:
                    return true;
                    break;
                case 1:
                    throw new \Exception();
                    break;
                default:
                    return false;
                    break;
            }
        }
    );

For those looking for a solution using PHPUnit...

$mockHydrator = $this->createMock(MyObject::class);
$mockHydrator->method('fetch')
    ->will(
        $this->onConsecutiveCalls(
            true,
            $this->throwException($e),
            false
        )
    );

This is one case where I feel PHPUnit mocks provides a better interface than Mockery.

Cymbal answered 18/6, 2018 at 8:40 Comment(0)
G
1

For PHPUnit's more recent versions you can use willReturnCallback to achieve that behaviour. Here's a versatile snippet to configure it for consequent calls :

$consecutiveReturnValues = [true, null, false];


$count = 0;
$mockHydrator->method('fetch')->willReturnCallback(
    function () use ($consecutiveReturnValues, &$count) {
        $returnVal = $consecutiveReturnValues[$count] ?? null;
        $count++;

        if ($returnVal != null) {
            return $returnVal;
        }

        // if the return value is null, we need to throw
        throw new Exception();
    }
);

You could also tweak the condition, instead of null value use the array_key_exists or alike.

Gaither answered 30/1 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.