PHPackages                             tototoshi/staticmock - 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. tototoshi/staticmock

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

tototoshi/staticmock
====================

A mockery-like DSL to replace static method in test.

4.0.1(5y ago)441.1M↓16.6%11[4 PRs](https://github.com/tototoshi/staticmock/pulls)BSD-3-ClausePHPCI failing

Since Oct 8Pushed 1y ago5 watchersCompare

[ Source](https://github.com/tototoshi/staticmock)[ Packagist](https://packagist.org/packages/tototoshi/staticmock)[ RSS](/packages/tototoshi-staticmock/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (26)Used By (0)

StaticMock
==========

[](#staticmock)

[![CI](https://github.com/tototoshi/staticmock/actions/workflows/ci.yml/badge.svg)](https://github.com/tototoshi/staticmock/actions/workflows/ci.yml)

A mockery-like DSL to replace static methods in tests.

```
$mock = StaticMock::mock('FooService');
$mock
    ->shouldReceive('find')
    ->with(1)
    ->once()
    ->andReturn('Something');
$mock->assert();
```

Motivation
----------

[](#motivation)

Mockery () provides nice interfaces to create mock objects. But as for static methods, Mockery needs an alias class and we can't create a mock object in one shot with his easy DSL.

StaticMock provides Mockery-like DSL for static methods. StaticMock depends on [runkit7](https://github.com/runkit7/runkit7) extension or [uopz](https://github.com/krakjoe/uopz) extension and rewrites static methods temporarily at run-time.

Requirements
------------

[](#requirements)

- PHP 7.3 and [runkit7/runkit7](https://github.com/runkit7/runkit7) 1.0.11
- PHP &gt;= 7.3 and [runkit7/runkit7](https://github.com/runkit7/runkit7) &gt;= 4.0.0a3
- PHP &gt;= 7.3 and [krakjoe/uopz](https://github.com/krakjoe/uopz)

### About runkit7 settings

[](#about-runkit7-settings)

Please note that the extension name differs depending on the version of runkit7. In the past it was `extension=runkit.so`, but nowadays it is `extension=runkit7.so`

Install
-------

[](#install)

composer.json

```
{
    "require-dev": {
        "tototoshi/staticmock": "4.0.2"
    }
}
```

Example
-------

[](#example)

Imagine that you are writing tests for `User` class such as this.

### Stubbing and Mocking

[](#stubbing-and-mocking)

```
class User
{

    private $email;

    public function __construct($email)
    {
        $this->email = $email;
    }

    public function getFeed()
    {
        $g_feed = GooglePlusClient::getFeed($this->email, 1);
        $f_feed = FacebookClient::getFeed($this->email, 1);
        return array_merge($g_feed, $f_feed);
    }

}

class GooglePlusClient
{

    public static function getFeed($email, $limit)
    {
        // send a request to Google
    }
}

class FacebookClient
{

    public static function getFeed($email, $limit)
    {
        // send a request to Facebook
    }
}
```

`User` class has a `getFeed` method. This method aggregates user's feeds from Google+ and Facebook. It depends on `GooglePlusClient` and `FacebookClient` to fetch feeds from their API. We sometimes want stubs for `GooglePlusClient` and `FacebookClient` to write tests for the `User` class. Our goal is only to ensure that `User` class can correctly aggregate feeds from APIs. The behavior of `GooglePlusClient` and `FacebookClient` is out of our heads now.

The problem is `GooglePlusClient::getFeed` and `FacebookClient::getFeed` are static methods. If they were instance methods, we could manage their dependencies and inject stubs of them to `User` class. But since they are static methods, we can't do that.

`StaticMock` solved the problem by replacing the methods temporarily at run-time. It provides an easy DSL for replacing methods. All you need to learn is only a few methods.

- Declare the methods we want to replace with `StaticMock::mock` and `shouldReceive`.
- The return value of the method is defined with `andReturn`.

See below. In this example, `GooglePlusClient::getFeed` and `FacebookClient::getFeed` are changed to return `array("From Google+")` and `array("From Facebook")`.

```
class UserTest extends \PHPUnit\Framework\TestCase
{

    public function testGetFeed()
    {
        $gmock = StaticMock::mock('GooglePlusClient');
        $fmock = StaticMock::mock('FacebookClient');
        $gmock->shouldReceive('getFeed')->andReturn(array("From Google+"));
        $fmock->shouldReceive('getFeed')->andReturn(array("From Facebook"));

        $user = new User('foo@example.com');
        $this->assertEquals(array('From Google+', 'From Facebook'), $user->getFeed());
    }

}
```

`StaticMock` also has some methods to act as mock objects.

- `never()`, `once()`, `twice()` and `times($times)` are used to check how many times they are called.
- `with` and `withNthArg` are used to check what arguments are passed when they are called.

```
class UserTest extends \PHPUnit\Framework\TestCase
{

    public function testGetFeed()
    {
        $user = new User('foo@example.com');

        $gmock = StaticMock::mock('GooglePlusClient');
        $fmock = StaticMock::mock('FacebookClient');
        $gmock
            ->shouldReceive('getFeed')
            ->once()
            ->with('foo@example.com', 1)
            ->andReturn(array("From Google+"));
        $fmock
            ->shouldReceive('getFeed')
            ->once()
            ->with('foo@example.co', 1)
            ->andReturn(array("From Facebook"));
        $this->assertEquals(array('From Google+', 'From Facebook'), $user->getFeed());
        $gmock->assert();
        $fmock->assert();
    }

}
```

### Common pitfalls

[](#common-pitfalls)

Assigning a mock variable (`$mock = StaticMock::mock('MyClass')`) is required since StaticMock is implemented with constructor and destructor magic. The methods are replaced when the instance of `Mock` class is created by `StaticMock::mock` and reverted when the instance goes out of scope.

So, the following code doesn't work as you expect.

```
class UserTest extends \PHPUnit\Framework\TestCase
{

    public function testGetFeed()
    {
        StaticMock::mock('GooglePlusClient')
            ->shouldReceive('getFeed')
            ->andReturn(array("From Google+"));
        StaticMock::mock('FacebookClient::getFeed')
           ->andReturn(array("From Facebook"));
        $user = new User('foo@example.com');
        $this->assertEquals(array('From Google+', 'From Facebook'), $user->getFeed());
    }

}
```

### Replacing method implementation

[](#replacing-method-implementation)

`andImplement` is useful to change the behavior of the method.

See below again. We are writing a test for `User::register` this time but we don't want to send an email every time running the test.

```
class User
{

    private $email;

    public function __construct($email)
    {
        $this->email = $email;
    }

    public function register()
    {
        $this->save();
        Mailer::send($this->email, 'Welcome to StaticMock');
    }

    private function save()
    {
        echo 'save!';
    }

}

class Mailer
{

    public static function send($email, $body)
    {
        // send mail
    }

}
```

Pass an anonymous function like below. The Email will not be sent and only a short line will be printed on your console.

```
class UserTest extends \PHPUnit\Framework\TestCase
{

    public function testRegister()
    {
        $mock = StaticMock::mock('Mailer');
        $mock->shouldReceive('send')->andImplement(function () {
            echo "send email";
        });

        $user = new User('foo@example.com');
        $user->register();
    }

}
```

With PHPUnit
------------

[](#with-phpunit)

You can easily define custom PHPUnit assertion. See below.

```
use StaticMock\Mock;
use StaticMock\PHPUnit\StaticMockConstraint;

class WithPHPUnitTest extends \PHPUnit\Framework\TestCase
{

    public function assertStaticMock(Mock $mock)
    {
        $this->assertThat($mock, new StaticMockConstraint);
    }

}
```

LICENSE
-------

[](#license)

BSD 3-Clause

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity52

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 71.3% 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

Every ~146 days

Recently: every ~13 days

Total

20

Last Release

1832d ago

Major Versions

0.2.1 → 1.0.02013-11-10

1.x-dev → 2.0.02021-03-19

2.x-dev → 3.0.02021-03-19

3.3.0 → 4.0.02021-04-11

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/264469?v=4)[Toshiyuki Takahashi](/maintainers/tototoshi)[@tototoshi](https://github.com/tototoshi)

---

Top Contributors

[![tototoshi](https://avatars.githubusercontent.com/u/264469?v=4)](https://github.com/tototoshi "tototoshi (107 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (23 commits)")[![zonuexe](https://avatars.githubusercontent.com/u/822086?v=4)](https://github.com/zonuexe "zonuexe (13 commits)")[![crhg](https://avatars.githubusercontent.com/u/8174597?v=4)](https://github.com/crhg "crhg (2 commits)")[![dmnlk](https://avatars.githubusercontent.com/u/909198?v=4)](https://github.com/dmnlk "dmnlk (2 commits)")[![rtm-ctrlz](https://avatars.githubusercontent.com/u/4123596?v=4)](https://github.com/rtm-ctrlz "rtm-ctrlz (1 commits)")[![TysonAndre](https://avatars.githubusercontent.com/u/1904430?v=4)](https://github.com/TysonAndre "TysonAndre (1 commits)")[![kunit](https://avatars.githubusercontent.com/u/405750?v=4)](https://github.com/kunit "kunit (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tototoshi-staticmock/health.svg)

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

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[phpspec/phpspec

Specification-oriented BDD framework for PHP 7.1+

1.9k36.7M3.1k](/packages/phpspec-phpspec)

PHPackages © 2026

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