mercredi 5 août 2020

Prophecy - Expect that no method will be called on an object

right now I'm using the following code in PHPUnit to expect that no method is called on a mock:

$object = $this->createMock(ClassA:class);
$object->expects($this->never())->method($this->anything());

So far I haven't found a way to achieve the same result in Prophecy. I only have been able to test assumptions on specific methods so far and not on all methods like in the sample above.

Currently I'm using the following custom assertion to test whether no method has been called. Prophecy's ObjectProphecy exposes a method to get all calls to a specific function so I'm using reflection to get all methods on a class and than each call for each method. If the array of calls is empty afterwards, I know that no method has been called. The method now looks like this:

public function assertNoMethodHasBeenCalled(ObjectProphecy $prophecy, string $className)
{
    $methods = get_class_methods($className);

    $calls = [];
    foreach ($methods as $method) {
        $reflectionMethod = new \ReflectionMethod($className, $method);
        $numArgs = count($reflectionMethod->getParameters());
        $calls = array_merge(
            $calls,
            $prophecy->findProphecyMethodCalls(
                $method,
                new ArgumentsWildcard(array_fill(0, $numArgs, Argument::any()))
            )
        );
    }

    $this->assertEmpty($calls);
}

It's working for my limited sample size so far but I'm not happy with it. I feel like there should be an easier way to achieve the same result.

Aucun commentaire:

Enregistrer un commentaire