PHPackages                             balint777/phockito-unit - 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. balint777/phockito-unit

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

balint777/phockito-unit
=======================

A library easing the combination of Phockito &amp; PHP Unit

1.0.0(12y ago)01MITPHPPHP &gt;=5.3.0

Since Oct 17Pushed 4y agoCompare

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

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

[![Build Status](https://camo.githubusercontent.com/d99132d850c71e7eaef2e085cfcabb2e19f7f9d4296f64616524f18bca73309e/68747470733a2f2f7472617669732d63692e6f72672f62616c69686f6f2f70686f636b69746f2d756e69742e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/balihoo/phockito-unit)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/24631855c959417f853a9e0303f632e8cd81ef066488f285d8cb47d8b8e95116/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62616c69686f6f2f70686f636b69746f2d756e69742f6261646765732f7175616c6974792d73636f72652e706e673f733d64633532306238613338613531396364326133326539383432306135353761356363623731313935)](https://scrutinizer-ci.com/g/balihoo/phockito-unit/)[![Code Coverage](https://camo.githubusercontent.com/bef9eaba62309e6023b6ea4c24e55dd5983e8f0a3e50f48d1d62ab988b9f2f8e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62616c69686f6f2f70686f636b69746f2d756e69742f6261646765732f636f7665726167652e706e673f733d61653930333237333931306561346432313462613462663635613436383164373762666664646633)](https://scrutinizer-ci.com/g/balihoo/phockito-unit/)[![Latest Stable Version](https://camo.githubusercontent.com/9ccd802d1edd029d8d58127d290381c67b1d94cd3b7f0be0fd317dc24b15d4c1/68747470733a2f2f706f7365722e707567782e6f72672f62616c69686f6f2f70686f636b69746f2d756e69742f762f737461626c652e706e67)](https://packagist.org/packages/balihoo/phockito-unit)[![Total Downloads](https://camo.githubusercontent.com/fb3c8757f31260d7ea454c48378762c6a6c060bdecb08eb0ca2363af7bcdb766/68747470733a2f2f706f7365722e707567782e6f72672f62616c69686f6f2f70686f636b69746f2d756e69742f646f776e6c6f6164732e706e67)](https://packagist.org/packages/balihoo/phockito-unit)[![Latest Unstable Version](https://camo.githubusercontent.com/4f98a3049d1fa3bf01f8dcc55a6b10af9934765c0182c7a746ac91f1f3923fcb/68747470733a2f2f706f7365722e707567782e6f72672f62616c69686f6f2f70686f636b69746f2d756e69742f762f756e737461626c652e706e67)](https://packagist.org/packages/balihoo/phockito-unit)

PhockitoUnit
============

[](#phockitounit)

PhockitoUnit exists to marry [PHP Unit](https://github.com/sebastianbergmann/phpunit/) with the [Phockito](https://github.com/hafriedlander/phockito) mocking framework in an everlasting love praised by PHP developers everywhere. It's features are rather simple:

- Automatically generate mocks that your tests require
- Automatically generate spys that your tests require
- Automatically turn on hamcrest matching

That's it!

PhockitoUnit in Action
======================

[](#phockitounit-in-action)

Here is a very classic PHP Unit test that uses Phockito to mock a dependency

```
class SomeTest extends PHPUnit_Framework_TestCase
{
  public function setUp(){
    Phockito::include_hamcrest();
  }

  function testSomeMethod(){
    /** @var SomeDependency $mockDependency **/
    $mockDependency = Phockito::mock('SomeDependency');
    Phockito::when($mockDependency->dependentMethod(anything()))->return("value");

    $instance = new ThingThatNeedsDependency($mockDependency);

    $this->assertEquals("value", $instance->methodThatUsesDependency());
  }

  function testSomeMethodWhenSomeDependencyThrows(){
    /** @var SomeDependency $mockDependency **/
    $mockDependency = Phockito::mock('SomeDependency');
    Phockito::when($mockDependency->dependentMethod(anything()))->throw(new Exception("Some error"));

    $instance = new ThingThatNeedsDependency($mockDependency);
    try{
      $instance->methodThatUsesDependency());
      $this->fail("Expected exception not thrown");
    } catch(Exception $ex) {
      $this->assertEquals("Some error", $ex->getMessage());
    }
  }
}
```

Certainly you have encoutnered or written a unit tests that is at least similar to this structure. PhockitoUnit simplifies this structure by eliminating some common boilerplate, here it is:

```
class SomeTest extends \PhockitoUnit\PhockitoUnitTestCase
{
  /** @var SomeDependency **/
  protected $mockDependency;

  function testSomeMethod(){
    Phockito::when($this->mockDependency->dependentMethod(anything()))->return("value");

    $instance = new ThingThatNeedsDependency($mockDependency);

    $this->assertEquals("value", $instance->methodThatUsesDependency());
  }

  function testSomeMethodWhenSomeDependencyThrows(){
    Phockito::when($this->mockDependency->dependentMethod(anything()))->throw(new Exception("Some error"));

    $instance = new ThingThatNeedsDependency($mockDependency);
    try{
      $instance->methodThatUsesDependency());
      $this->fail("Expected exception not thrown");
    } catch(Exception $ex) {
      $this->assertEquals("Some error", $ex->getMessage());
    }
  }
}
```

It's not a monsterous change, but it helps quite a bit, eliminating the chance of class name typos, class rename refactorings, etc. And in more advanced scenarios where you are mocking an domain object graph it can make it easier to write more tests. More tests means more coverage of intent. Here's an example that sets up a graph and uses a spy:

```
class FamilyTest extends \PhockitoUnit\PhockitoUnitTestCase
{
  /** @var Child **/
  protected $mockChild1;

  /** @var Child **/
  protected $spyChild2;

  /** @var Parent **/
  protected $mockParent;

  public function setUp(){
    parent::setUp();

    Phockito::when($this->mockParent->getEledestChild())->return($this->mockChild1);
    Phockito::when($this->mockParent->getYoungestChild())->return($this->spyChild1);
  }

  function testGetEldestChildNickName(){
    Phockito::when($this->mockChild1->getNickName())->return("Oldie");

    $family = new Family(array($this->mockParent));

    $this->assertEquals("Oldie", $family->getElestChildNickName());
  }

  function testGetYoungestchildFullName(){
    Phockito::when($this->spyChild2->getFirstName())->return("Youngy");
    Phockito::when($this->spyChild2->getLastName())->return("McYoung");

    $family = new Family(array($this->mockParent));

    $this->assertEquals("Youngy McYoung", $family->testGetYoungestchildFullName());
  }
}
```

Do you use a DI Framework?
==========================

[](#do-you-use-a-di-framework)

If you use Phockito to mock things, then you likely use a DI framework. If you do we suggest building a package on top of this to register your mocks in your DI container automatically. We use [PHP-DI](https://github.com/mnapoli/PHP-DI) and so we have built [PhockitoUnit-PHP-DI](https://github.com/balihoo/phockito-unit-php-di) to make it that much easier to test application code.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

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

4590d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4ffdbd430f6f574c049ec2a8c4f0522af7d7926c5886d5f174b4002235f104ad?d=identicon)[balint777](/maintainers/balint777)

---

Top Contributors

[![drdamour](https://avatars.githubusercontent.com/u/1514496?v=4)](https://github.com/drdamour "drdamour (17 commits)")[![balint777](https://avatars.githubusercontent.com/u/4312347?v=4)](https://github.com/balint777 "balint777 (2 commits)")[![balihoo-cdamour](https://avatars.githubusercontent.com/u/5853225?v=4)](https://github.com/balihoo-cdamour "balihoo-cdamour (1 commits)")[![rowanhill](https://avatars.githubusercontent.com/u/2607287?v=4)](https://github.com/rowanhill "rowanhill (1 commits)")

---

Tags

unittestmockPHP Unitphockito

### Embed Badge

![Health badge](/badges/balint777-phockito-unit/health.svg)

```
[![Health](https://phpackages.com/badges/balint777-phockito-unit/health.svg)](https://phpackages.com/packages/balint777-phockito-unit)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[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)[colinodell/psr-testlogger

PSR-3 compliant test logger based on psr/log v1's, but compatible with v2 and v3 too!

1712.1M47](/packages/colinodell-psr-testlogger)[polishsymfonycommunity/symfony-mocker-container

Provides base Symfony dependency injection container enabling service mocking.

1468.0M237](/packages/polishsymfonycommunity-symfony-mocker-container)

PHPackages © 2026

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