PHPackages                             webxid/phpunitsandbox - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. webxid/phpunitsandbox

ActiveLibrary[Testing &amp; Quality](/categories/testing)

webxid/phpunitsandbox
=====================

This package helps to make a mockups for unit-testing

0.3.0(4y ago)03Apache-2.0PHPPHP ^5.4|^7|^8

Since Feb 4Pushed 4y agoCompare

[ Source](https://github.com/webxid/PHPUnitSandbox)[ Packagist](https://packagist.org/packages/webxid/phpunitsandbox)[ RSS](/packages/webxid-phpunitsandbox/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

PHPUnit Sandbox
===============

[](#phpunit-sandbox)

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

*Source*

---

Install
-------

[](#install)

PHPUnitSandbox does not require any special installation.

**Requirements**

- 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

```
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();
```

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

---

How To Use
----------

[](#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() {}`.

**Example**

```
// 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();
});
```

Code examples
-------------

[](#code-examples)

### Mock up a static method and an object method

[](#mock-up-a-static-method-and-an-object-method)

```
UnitSandbox::mockClass('DB')
    ->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

[](#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
            ->execute();
    }
}
```

```
// Get result of TestClass::init();
$result = UnitSandbox::execute(function () {
    return \TestClass::init();
});

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

---

### Spy class

[](#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;

```
UnitSandbox::spyClass('\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](./tests/ExampleUnitTest.php) \*

---

Issues
------

[](#issues)

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

```
UnitSandbox::init()
   ->debugMode(true, false);
```

---

License
-------

[](#license)

**PHPUnitSandbox** is licensed under the Apache v2.0.

---

Version log
-----------

[](#version-log)

v0.3

- Make composer lib
- Minor fixes

v0.2

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

v0.1

- Methods mock up features

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1564d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/bb89540178734b2c6a79f0550f4e496db917a7f8938854a965226508deb8e16f?d=identicon)[webxid](/maintainers/webxid)

---

Top Contributors

[![webxid](https://avatars.githubusercontent.com/u/3855639?v=4)](https://github.com/webxid "webxid (9 commits)")

---

Tags

functionrunkitsandboxphp sandboxmockupTest Suite

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/webxid-phpunitsandbox/health.svg)

```
[![Health](https://phpackages.com/badges/webxid-phpunitsandbox/health.svg)](https://phpackages.com/packages/webxid-phpunitsandbox)
```

###  Alternatives

[antecedent/patchwork

Method redefinition (monkey-patching) functionality for PHP.

45018.7M35](/packages/antecedent-patchwork)[php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock relies on PHP's namespace fallback policy. No further extension is needed.

36918.1M98](/packages/php-mock-php-mock)[brain/monkey

Mocking utility for PHP functions and WordPress plugin API

33412.5M350](/packages/brain-monkey)[php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

1718.2M399](/packages/php-mock-php-mock-phpunit)[php-mock/php-mock-mockery

Mock built-in PHP functions (e.g. time()) with Mockery. This package relies on PHP's namespace fallback policy. No further extension is needed.

392.1M96](/packages/php-mock-php-mock-mockery)[php-mock/php-mock-integration

Integration package for PHP-Mock

1410.5M3](/packages/php-mock-php-mock-integration)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
