PHPackages                             balihoo/phockito-unit-php-di - 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. balihoo/phockito-unit-php-di

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

balihoo/phockito-unit-php-di
============================

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

1.0.0(12y ago)09[4 issues](https://github.com/balihoo/phockito-unit-php-di/issues)MITPHPPHP &gt;=5.3.0

Since Oct 17Pushed 12y agoCompare

[ Source](https://github.com/balihoo/phockito-unit-php-di)[ Packagist](https://packagist.org/packages/balihoo/phockito-unit-php-di)[ RSS](/packages/balihoo-phockito-unit-php-di/feed)WikiDiscussions master Synced today

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

PhockitoUnit PHP-DI
===================

[](#phockitounit-php-di)

[![Build Status](https://camo.githubusercontent.com/2b5f17cfd31df2b8ff03ece92351ac25975acd77ea3f7d8c745a8c191c8af685/68747470733a2f2f7472617669732d63692e6f72672f62616c69686f6f2f70686f636b69746f2d756e69742d7068702d64692e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/balihoo/phockito-unit-php-di)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/db317f737d89f5cabf5601a2345d23fedb97cc02ab118ec1f7c4f910307e4ca4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62616c69686f6f2f70686f636b69746f2d756e69742d7068702d64692f6261646765732f7175616c6974792d73636f72652e706e673f733d35316165633639356236616365376434353534343734336437613766633536343032306330346363)](https://scrutinizer-ci.com/g/balihoo/phockito-unit-php-di/)[![Code Coverage](https://camo.githubusercontent.com/556e93d19eef5e37450d08ea0836262456644871dc193d74b3957126e6a71d46/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62616c69686f6f2f70686f636b69746f2d756e69742d7068702d64692f6261646765732f636f7665726167652e706e673f733d37663963306539363031343932633132383736626538643062323336656436383233626335323336)](https://scrutinizer-ci.com/g/balihoo/phockito-unit-php-di/)[![Latest Stable Version](https://camo.githubusercontent.com/f436b12f31ff148555a60d21d664a05673661172a24c6a983831d2296c5b8b77/68747470733a2f2f706f7365722e707567782e6f72672f62616c69686f6f2f70686f636b69746f2d756e69742d7068702d64692f762f737461626c652e706e67)](https://packagist.org/packages/balihoo/phockito-unit)[![Total Downloads](https://camo.githubusercontent.com/c88d1fd2f8457cc3454ca170e6ca1d785b1dc29d0bcac2143c8e5db8144d978a/68747470733a2f2f706f7365722e707567782e6f72672f62616c69686f6f2f70686f636b69746f2d756e69742d7068702d64692f646f776e6c6f6164732e706e67)](https://packagist.org/packages/balihoo/phockito-unit)[![Latest Unstable Version](https://camo.githubusercontent.com/abec853d80275a5e43142856db3ce81970266d5fc03eb5018007fdc3dfdc7e7a/68747470733a2f2f706f7365722e707567782e6f72672f62616c69686f6f2f70686f636b69746f2d756e69742d7068702d64692f762f756e737461626c652e706e67)](https://packagist.org/packages/balihoo/phockito-unit)

PhockitoUnit PHP-DI exists to marry [PHP Unit](https://github.com/sebastianbergmann/phpunit/) with the [Phockito](https://github.com/hafriedlander/phockito) mocking framework and the [PHP-DI](https://github.com/mnapoli/PHP-DI) dependency injection framework in an everlasting love praised by PHP developers everywhere. It is a PHP-DI specific enhancement to the [PhockitoUnit](https://github.com/balihoo/phockito-unit) libary. 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
- Automatically register your mocks in the DI Container

That's it!

PhockitoUnit PHP-DI in Action
=============================

[](#phockitounit-php-di-in-action)

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

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

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

  public 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;

  public function testSomeMethod(){

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

    $instance = new ThingThatNeedsDependency($mockDependency);

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

  public 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);

  }

  public function testGetEldestChildNickName(){

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

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

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

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

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

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

4586d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1385a528babaa37e00f37ab295611e32ac6e809721b9e7d1421e5b9df365ba8d?d=identicon)[drdamour](/maintainers/drdamour)

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

---

Top Contributors

[![drdamour](https://avatars.githubusercontent.com/u/1514496?v=4)](https://github.com/drdamour "drdamour (15 commits)")[![balihoo-cdamour](https://avatars.githubusercontent.com/u/5853225?v=4)](https://github.com/balihoo-cdamour "balihoo-cdamour (2 commits)")[![mnapoli](https://avatars.githubusercontent.com/u/720328?v=4)](https://github.com/mnapoli "mnapoli (2 commits)")

---

Tags

unittestmockdiPHP Unitphockito

### Embed Badge

![Health badge](/badges/balihoo-phockito-unit-php-di/health.svg)

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

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.5k](/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.5M346](/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.2M397](/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)
