Question

In a function that I want to test I have the following check

if (!is_callable(array($object, $methodName))) {
    throw new \InvalidArgumentException(
        sprintf(
            'Unable to call method %s::%s() on object $%s',
             get_class($object),
             $methodName,
             $objectName
        )
    );
}

How can I test the exception ?

How can I make a MOCKERY object containing a method that is not callable or is maybe a property ? I am not sure.

Was it helpful?

Solution

You could simply instanciate a empty StdClass object.

public function testCallable()
{
    $object = new \StdClass();

    $object2 = \Mockery::mock('\StdClass')
        ->shouldReceive('myMethod')
        ->andReturn('foo')
        ->getMock();

    $this->assertFalse(is_callable(array($object, 'myMethod')));

    $this->assertTrue(is_callable(array($object2, 'myMethod')));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top