Have you ever wished for control over the behaviour of procedural functions when writing test code? The techniques demonstrated by the code in this repository will help you seize control of your classes under test.
The included library defines a Monitor
interface, instances of which can be run()
. The client might, for example, be a scheduler that is responsible for running various monitoring jobs, and is outside the scope of these examples.
Also included are various implementations demonstrating how we might create a disk space monitor that logs warnings when disk space on a particular volume is low. The basic implementation, SimpleDiskMonitor
is not easily tested due to its direct dependency on the procedural function disk_free_space()
. Each of the other implementations demonstrate a different way to control the behaviour of disk_free_space()
in a testing scenario.
FunctionWrappingDiskMonitor
wraps the procedural function in a protected member function.WrapperDelegatingDiskMonitor
wraps the procedural function in a static function defined in a utility class.VariableFunctionInvokingDiskMonitor
replaces the invocation of the procedural function by name with an invocation by variable.CallbackInvokingDiskMonitor
replaces the invocation of the procedural function by name with an invocation usingcall_user_func()
.
Each of these implementations allow test cases to exercise the monitor's logic by controlling the result of the function that returns free disk space.
- Clone the repository.
- From the root directory of the cloned repository, run
composer install
.
- Install the repository by following the installation instructions.
- From the installed directory, run
vendor/bin/phpunit
.