1. Go to this page and download the library: Download webxid/phpunitsandbox library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
webxid / phpunitsandbox example snippets
UnitSandbox::init([
__DIR__ . '/../autoloader_1.php', // it has to be absolute route of autoloader file, and it has to be .php file
...
__DIR__ . '/../autoloader_n.php',
])
->registerAutoloader();
// Setup a mock up of a class `DB` and of the static method `DB::query()`
UnitSandbox::mockClass('DB')
->mockStaticMethod('query', UnitSandbox::SELF_INSTANCE); //return self instance
// Then call the mocked method inside sandbox
$result = UnitSandbox::execute(function () {
return \DB::query();
});
// This class should be able by autoloader
class TestClass
{
public static function init()
{
return DB::query() // the usage of the mocked class
->execute();
}
}
// Get result of TestClass::init();
$result = UnitSandbox::execute(function () {
return \TestClass::init();
});
echo json_encode($result); // returns `[1,2,3]`
class TestClass
{
private static $my_property = 'Hello world!';
public static function getProperty()
{
return static::$my_property;
}
}