PHPackages                             facebook/fbmock - 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. facebook/fbmock

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

facebook/fbmock
===============

FBMock is a PHP mocking framework designed to be simple and easy to use.

18218.3k31[1 issues](https://github.com/facebook/FBMock/issues)PHP

Since Sep 9Pushed 7y ago27 watchersCompare

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

READMEChangelogDependenciesVersions (3)Used By (0)

FBMock
======

[](#fbmock)

FBMock is a PHP mocking framework designed to be simple and easy to use.

Unlike other mocking frameworks, FBMock is basically just stubs with spies. Instead of using a custom DSL and relying on opaque tear-down verification, FBMock encourages developers to program simple return behaviors and only use spies when appropriate.

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

[](#requirements)

- HHVM or PHP 5.4+

Note: the framework's tests use PHPUnit but PHPUnit is not necessary for using FBMock.

Usage
-----

[](#usage)

Include init.php which sets up the autoloader

```
require_once YOUR_FBMOCKS_DIR.'/init.php'
```

### Install using Composer (optional)

[](#install-using-composer-optional)

To install this package via composer, just add the package to `require` and start using it.

```
{
    "require": {
        "facebook/fbmock": "*@dev"
    }
}
```

### Creating a mock

[](#creating-a-mock)

```
mock('Foo')
```

Also, you can mock an interface in the same manner:

```
mock('IFoo')
```

### Stubbing

[](#stubbing)

By default, all methods return null. Helper methods for configuring return values are prefixed with 'mock'.

```
mock('Foo')->mockReturn('bar', 'return value here');
```

Sometimes, you need more control than mockReturn:

```
mock('Foo')->mockImplementation(
    'bar',
    function ($bar_type) {
        return $bar_type == 1;
    }
);
```

For other helpers (mockThrow, mockReturnThis, etc.) see Mock.php

### Spying

[](#spying)

```
$mock_foo = mock('Foo');
$mock->mockReturn('bar', 1);

$code_under_test->doSomething($mock_foo);

// Returns array of calls
$mock_foo->mockGetCalls('bar');
```

If you are using PHPUnit, you can add FBMock\_AssertionHelpers to your base PHPUnit\_TestCase to get some spying helpers:

```
$this->assertCalledOnce($mock_foo, 'bar');
$this->assertCalledOnce($mock_foo, 'bar', $expected_params_as_array); // param assertion is optional
$this->assertCalls($mock_foo, 'bar', $expected_params_for_first_call, $expected_params_for_second_call, ...);
```

### Tips

[](#tips)

#### Use multiset capabilities to improve legibility

[](#use-multiset-capabilities-to-improve-legibility)

```
mock('Foo')->mockReturn(array(
    'bar' => 1,
    'get' => 'data',
    'run' => true,
))->mockReturnThis('set', 'addID', 'removeID');
```

#### Utilize the fluent interface for concise mock setup.

[](#utilize-the-fluent-interface-for-concise-mock-setup)

```
mock('Foo')
    ->mockReturn('bar', 1)
    ->mockThrow('other_method')
    ->mockImplementation('another_method', function () { /* ... */ })
    ->mockReturnThis('setData');

```

#### Use strict mocks when it's critical a method isn't called

[](#use-strict-mocks-when-its-critical-a-method-isnt-called)

If any unmocked method is called, the mock will throw.

```
$db_conn = strict_mock('DBConnection')->mockReturn('read', $data);

// ...in code under test
$db_conn->write($some_data); // Throws FBMock_MockFrameworkException
```

### Customizing

[](#customizing)

See CustomConfig-sample.php for instructions on customizing FBMock for your needs.

### HHVM-only features

[](#hhvm-only-features)

- Mock final classes and classes with final methods
- Mock internal classes

### Community

[](#community)

We have a [mailing list](http://groups.google.com/group/fbmock). If you're using FBMock, send us an email to say hi!

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity39

Limited adoption so far

Community24

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

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

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3757713?v=4)[Joel Marcey](/maintainers/JoelMarcey)[@JoelMarcey](https://github.com/JoelMarcey)

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

---

Top Contributors

[![elynde](https://avatars.githubusercontent.com/u/522374?v=4)](https://github.com/elynde "elynde (24 commits)")[![rdohms](https://avatars.githubusercontent.com/u/94331?v=4)](https://github.com/rdohms "rdohms (3 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (1 commits)")[![Daniel15](https://avatars.githubusercontent.com/u/91933?v=4)](https://github.com/Daniel15 "Daniel15 (1 commits)")[![flarnie](https://avatars.githubusercontent.com/u/1114467?v=4)](https://github.com/flarnie "flarnie (1 commits)")[![sch](https://avatars.githubusercontent.com/u/109822?v=4)](https://github.com/sch "sch (1 commits)")

### Embed Badge

![Health badge](/badges/facebook-fbmock/health.svg)

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

###  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)
