...and a bit more ;)
- easy mocking
- make any changes revertible
- revert changes automatically
There are several classes that will do the work:
- For class methods:
MockedClassMethod
to mock any class with closureUndefinedClassMethod
to make method disappear
- For class constants:
MockedClassConstant
UndefinedClassConstant
- For global constants:
MockedGlobalConstant
UndefinedGlobalConstant
- For functions (global or Namespaced):
MockedFunction
UndefinedFunction
composer require --dev qratorlabs/smocky
There is a workaround that ensures that any (defined) children of class, which method is mocking, have its own method, defined by user or mocked (by Smocky - closure that calls parent).
Example for code that will fail without this workaround following code will end up with Segmentation fault: 11
class Base
{
public static function methodName()
{
return 'something';
}
}
class Child extends Base
{
}
// simulating PHPUnit test case
(new class('test') extends \PHPUnit\Framework\TestCase {
public function test(): void
{
// child should instanced (or loaded any other way)
$child = new Child();
// mocking method of base class
$method = new MockedClassMethod(Base::class, 'methodName');
// at least one call should be made
Base::methodName();
}
})->run();
All changes are made revertible by using internal storage and __destruct
methods.
Thing to keep in mind before using:
- Mocking anything will hit memory consumption (ex. to preserve changes)
- Mocking methods will hit performance (a bit)
- To mock static class we must check (and mock) children of mocking class