Is it possible, using PHPUnit mock objects, to expect a call to a magic __call() method? Is it possible, using PHPUnit mock objects, to expect a call to a magic __call() method? symfony symfony

Is it possible, using PHPUnit mock objects, to expect a call to a magic __call() method?


PHPUnit's getMock() takes a second argument, an array with the names of methods to be mocked.If you include a method name in this array, the mock object will contain a method with that name, which expects() and friends will work on.

This applies even for methods that are not defined in the "real" class, so something like the following should do the trick:

$mockPageRepository = $this->getMock('PageRepository', array('findOneBySlug'));

Keep in mind that you'll have to explicitly include any other methods that also need to be mocked, since only the methods named in the array are defined for the mock object.


I found better way to do this. Still not perfect, but you don't have to specify all mocking class methods by hand, only magic methods:

    $methods = get_class_methods(PageRepository::class);    $methods[] = 'findOneBySlug';    $pageRepositoryMock = $this->getMockBuilder(PageRepository::class)        ->disableOriginalConstructor()        ->disableOriginalClone()        ->disableArgumentCloning()        ->disallowMockingUnknownTypes()        ->setMethods($methods)        ->getMock();