webXID - my way of samurai
namespace Home \ Libraries \ PHPUnitSandbox ;


The standard `PHPUnit` library and` Reflaction` methods do not allow to mock up a static method or property.
This library allows to set all the necessary data in the static part of a class.


PHPUnit Sandbox

This package helps to make a mockups for unit-testing in PHP v5.4 - 8.x

Source https://github.com/webxid/PHPUnitSandbox


PHPUnitSandbox does not require any special installation.


  • PHP v5.4 or later,
  • PHPUnitSandbox has to be includes directly (no autoloaders supports, including Composer),
  • PHP config has to support functions exec() and eval().

Installation steps

  1. Run composer require webxid/phpunitsandbox
  2. Register additional autoloaders, if you have any one
        __DIR__ . '/../autoloader_1.php', // it has to be absolute route of autoloader file, and it has to be .php file
        __DIR__ . '/../autoloader_n.php',

!!! Important !!! to make sandbox works correctly, the all autoloaders have to be register via UnitSandbox, otherwise you get failed. Also, please, check ./bootstrap.php

How To Use

This lib works as sandbox:

  • First, you setup a mock up of a class and of the class methods.
  • Then, you call UnitSandbox::execute(function() {}); and pass mocked class using inside the function() {}.


// Setup a mock up of a class `DB` and of the static method `DB::query()`
    ->mockStaticMethod('query', UnitSandbox::SELF_INSTANCE); //return self instance

// Then call the mocked method inside sandbox
$result = UnitSandbox::execute(function () {
    return \DB::query();

Code examples

Mock up a static method and an object method
    ->mockStaticMethod('query', UnitSandbox::SELF_INSTANCE) // returns self instance
    ->mockMethod('execute', [1,2,3]); // returns array(1,2,3)
Mocked classes are working inside another classes too
// This class should be able by autoloader
class TestClass
    public static function init()
        return DB::query() // the usage of the mocked class
// Get result of TestClass::init();
$result = UnitSandbox::execute(function () {
    return \TestClass::init();

echo json_encode($result); // returns `[1,2,3]` 

Spy class

Spy class uses to mock up a part of a class.

  1. We need a TestClass for example:

    class TestClass
    private static $my_property = 'Hello world!';
    public static function getProperty()
        return static::$my_property;
  2. Let's rewrite private property of class TestClass;

    ->defineStaticProperty('my_property', 'value');

$result_private_property = UnitSandbox::execute(function () {
    return \Spy\TestClass::getProperty();

echo $result_private_property; // it'll prints `value` instead `Hello world!`

\ Please, see all examples in ./tests/ExampleUnitTest.php


To see errors, occurred inside sandbox, needs to set up debug mode:

    ->debugMode(true, false);


PHPUnitSandbox is licensed under the Apache v2.0.

Version log


  • Make composer lib
  • Minor fixes


  • Mocked class properties defining
  • Pass parameters to mocked methods
  • Spy class logic
  • Minor fixes


  • Methods mock up features