Search code examples
phpphpspec

Test parent-child bidirectional relations using PHPSpec


How to test with phpspec that parent-child biedirectional relations are created properly?

class ParentSpec extends ObjectBehavior
{
    function it_adds_a_reference_to_self_while_(Child $child)
    {
        $this->addChild($child);

        $child->setParent($this)->shouldBeCalled();
    }
}

Line 7 throws an error, which is obious because $this is a ParentSpec object not Parent. But I have no other ideas how to test that setParent method has been called.


Solution

  • Use getWrappedObject to get the underlying object:

    $child->setParent($this->getWrappedObject())->shouldBeCalled();
    

    http://www.phpspec.net/en/latest/cookbook/wrapped-objects.html