Currently ad hoc mocks are specified like this:
mock(
null, // $types
[
'methodA' => function () {}
]
);
partialMock(
null, // $types
null, // $arguments
[
'methodA' => function () {}
]
);
But since the first argument to mock() (i.e. $types) already accepts a variety of inputs, it could probably accept definition values directly. They could be distinguished from arrays of multiple types by checking whether the $types has sequential keys:
mock(
[
'methodA' => function () {}
]
);
partialMock(
[
'methodA' => function () {}
]
);
This would probably make both mock() and, particularly, partialMock() more intuitive.
The other factors in this equation are mocking of anonymous classes and generic objects, and "mocks that forward to a real instance" (#39).
I recently implemented support for anonymous classes in a branch. It involved accepting an instance of an anonymous class as a type, and doing the following:
- Implicitly mocking each type extended by, or implemented by, the anonymous class.
- Adding custom methods for each of the anonymous class' public methods, whose callback simply points back to the anonymous class instance's original public method.
Afterwards, I realised that this almost does what #39 is trying to achieve. Perhaps there's a solution that will cover all these problems, whilst making the interface clearer.
Currently ad hoc mocks are specified like this:
But since the first argument to
mock()(i.e.$types) already accepts a variety of inputs, it could probably accept definition values directly. They could be distinguished from arrays of multiple types by checking whether the$typeshas sequential keys:This would probably make both
mock()and, particularly,partialMock()more intuitive.The other factors in this equation are mocking of anonymous classes and generic objects, and "mocks that forward to a real instance" (#39).
I recently implemented support for anonymous classes in a branch. It involved accepting an instance of an anonymous class as a type, and doing the following:
Afterwards, I realised that this almost does what #39 is trying to achieve. Perhaps there's a solution that will cover all these problems, whilst making the interface clearer.