Search code examples
phpmockingphpunitmockery

Mockery and method_exists


I need to mock an existence of a method of a mocked class with Mockery, but when I use method_exists to check I have a false result:

$mock = m::mock('ClassA')->makePartial();
$mock->shouldReceive('getMethod')->andReturn('A');
var_dump(method_exists($mock, 'getMethod'));

Result:

bool(false)

How to make it works?


Solution

  • getMethod must not exist in ClassA for method_exists to fail.

    When mocking an existing class, method_exists will return true for any method that actually exists in that class.

    When the method does not exist, since shouldReceive will not actually define a method but will use overloading (__call), it will still return false.