-
-
Notifications
You must be signed in to change notification settings - Fork 3
Description
PHPUnit has mocking tools built in, but they have the following limitations:
⚠️ Limitation: final, private, and static methodsPlease note that
final
,private
, andstatic
methods cannot be stubbed or mocked. They are ignored by PHPUnit’s test double functionality and retain their original behavior except forstatic
methods that will be replaced by a method throwing a\PHPUnit\Framework\MockObject\BadMethodCallException
exception.
In PHP 7.x, we can get around this by using anonymous classes, but users still supporting PHP 5.6 are stuck.
To get around this, a Methods
trait should be added with the following methods:
defineMethod(string $class, string $method, callable $function, string $visibility = 'public', bool $static = false)
redefineMethod(string $class, string $method, callable $function)
deleteMethod(string $class, string $method)
Careful attention should be paid to the visibility of methods, as well as whether or not they should be treated as static — Reflection will likely be used to handle these, as well as streamlining the definition of method arguments within Runkit.