PHPUnit has mocking tools built in, but they have the following limitations:
⚠️ Limitation: final, private, and static methods
Please note that final, private, and static methods cannot be stubbed or mocked. They are ignored by PHPUnit’s test double functionality and retain their original behavior except for static 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.
PHPUnit has mocking tools built in, but they have the following limitations:
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
Methodstrait 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.