Search code examples
phpunit-testingphpspec

How to test parent call method in a class for phpspec


I have a class with parent call method in it. I want to make sure that parent have been called so that when I edit tested method and remove that line it will fail tests

My class:

public function myMethod($object)
{
    parent::myMethod($object);
    ..
}

My test (spec):

/*
 * @param \Example\Entity\MyEntity $myEntity
 */
function it_cat_call_my_method_example($myEntity)
{
    $this->myMethod($myEntity)->shouldParentHaveBeenCalled(); // what to do here?
}

Is that possible?


Solution

  • This is not possible in the way you describe.

    What would the benefit of testing a call to parent be? You presumably want to test whether some behaviour the parent implements happens, so call that directly.

    Put another way, if I rewrite my class to not call parent and instead implement the same functionality, I don't think the test should fail.