Jimmy Kane
Jimmy Kane

Reputation: 16845

PHP unit-testing how to mock a method NOT to be callable

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.

Upvotes: 0

Views: 2169

Answers (1)

Bram Gerritsen
Bram Gerritsen

Reputation: 7238

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')));
}

Upvotes: 2

Related Questions