The idea behind "Soft Mocks" - as opposed to "hardcore" mocks that work on the level of the PHP interpreter (runkit and uopz) - is to rewrite class code on the spot so that it can be inserted in any place. It works by rewriting code on the fly during file inclusion instead of using extensions like runkit or uopz.
You can install SoftMocks via Composer:
php composer.phar require --dev badoo/soft-mocks
The thing that sets SoftMocks apart (and also limits their usage) is that they need to be initiated at the earliest phase of the app launch. It's necessary to do it this way because you can't redefine the classes and functions that are already loaded into the memory in PHP. For an example bootstrap presets, see src/bootstrap.php. For PHPUnit you should use patches form composer.json, because you should require composer autoload through SoftMocks.
SoftMocks don't rewrite the following system parts:
- it's own code;
- PHPUnit code (see
\Badoo\SoftMocks::addIgnorePath()
for details); - PHP-Parser code (see
\Badoo\SoftMocks::addIgnorePath()
for details); - already rewritten code;
- code which was loaded before SoftMocks initialization.
In order to add external dependencies (for example, vendor/autoload.php) in file, which which was loaded before SoftMocks initialization, you need to use a wrapper:
require_once (\Badoo\SoftMocks::rewrite('vendor/autoload.php'));
require_once (\Badoo\SoftMocks::rewrite('path/to/external/lib.php'));