PHPackages                             symfonyboot/inject-mocks - 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. symfonyboot/inject-mocks

Abandoned → [https://github.com/silasyudi/inject-mocks](/?search=https%3A%2F%2Fgithub.com%2Fsilasyudi%2Finject-mocks)Library[Testing &amp; Quality](/categories/testing)

symfonyboot/inject-mocks
========================

Automatic injection of mocks into test subjects via #InjectMocks and #Mock annotations, to speed up unit testing with PHPUnit.

v3.0.0(1y ago)19MITPHPPHP &gt;=8.1

Since Apr 5Pushed 1y ago1 watchersCompare

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

READMEChangelog (5)Dependencies (2)Versions (7)Used By (0)

Inject-Mocks
============

[](#inject-mocks)

[![Tests](https://github.com/silasyudi/inject-mocks/actions/workflows/tests.yml/badge.svg)](https://github.com/silasyudi/inject-mocks/actions/workflows/tests.yml)[![Maintainability](https://camo.githubusercontent.com/f6ac68b64fc5490bbe8544d32455ce5c3e90f89ff6c8e4222fc16f9b401dc906/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f62383962633630363333346337656465633932652f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/silasyudi/inject-mocks/maintainability)[![Test Coverage](https://camo.githubusercontent.com/0a9169bcef0d1eac472760b9f44aefff7a172b98532816643069d6b0c4b8d177/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f62383962633630363333346337656465633932652f746573745f636f766572616765)](https://codeclimate.com/github/silasyudi/inject-mocks/test_coverage)

Automatic injection of mocks into test subjects via #InjectMocks and #Mock annotations, to speed up unit testing with PHPUnit.

Summary
-------

[](#summary)

- [Language / Idioma](#language--idioma)
- [Instalation](#instalation)
- [Requirements](#requirements)
- [Features](#features)
- [Usage](#usage)

Language / Idioma
-----------------

[](#language--idioma)

Leia a versão em português 🇧🇷 [aqui](README_PT_BR.md).

Instalation
-----------

[](#instalation)

To install in the development environment:

```
composer require --dev silasyudi/inject-mocks
```

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

[](#requirements)

- PHP 8.3+
- Composer 2

Features
--------

[](#features)

Using #InjectMocks and #Mock annotations in test classes you can automatically inject mocks into test subjects.

In a typical scenario, we would do it like this:

### Example without #InjectMocks/#Mock:

[](#example-without-injectmocksmock)

```
class SomeTest extends \PHPUnit\Framework\TestCase
{
    public void testSomething()
    {
        $someDependency = $this->createMock(Dependency::class);
        $anotherDependency = $this->createMock(AnotherDependency::class);
        ...
        $subject = new Service($someDependency, $anotherDependency, ...);
        ...
    }

    ...
```

This approach brings the difficulty of maintenance, because if the test subject is changed, either by adding, decreasing or replacing the dependencies, you will have to change it in each test.

With the #InjectMocks/#Mock annotations, we abstract these test subject changes. Example:

### Example with #InjectMocks/#Mock:

[](#example-with-injectmocksmock)

```
use SilasYudi\InjectMocks\InjectMocks;
use SilasYudi\InjectMocks\Mock;
use SilasYudi\InjectMocks\MockInjector;

class SomeTest extends \PHPUnit\Framework\TestCase
{
    #[Mock]
    private Dependency $someDependency;
    #[Mock]
    private AnotherDependency $anotherDependency;

    ...

    #[InjectMocks]

    public function setUp() : void
    {
        MockInjector::inject($this);
    }

    public void testSomething()
    {
        // $this->subject e as dependências já estão instanciadas.
    }

    ...
```

Usage
-----

[](#usage)

As in the example in the previous topic, the #InjectMocks attribute must be placed on the property of the test subject that you want to test, and the #Mock attribute must be placed on the properties corresponding to the dependencies that you want to mock or inject.

After that, run the injector service with the sentence `MockInjector::inject($this)`. This execution can be declared in each test or in `setUp`.

After executing the injector, the `service` annotated with #InjectMocks will be a real instance available in the scope of the test class, and each dependency annotated with #Mock will be an instance of MockObject, injected into the test subject via the constructor, and will also be available in the scope of the test class.

### Details

[](#details)

#### 1. Scope of Attributes

[](#1-scope-of-attributes)

- Both #InjectMocks and #Mock MUST be placed over a TYPED property in a TestCase class;
- Properties that receive #InjectMocks and #Mock attributes MUST be an object;
- You MUST use only one #InjectMocks attribute per TestCase. When using more than one on the same scope, this library will use only the first one, and ignore the others;
- You MUST use a #Mock attribute for each test subject dependency you want to mock;
- Using attributes on untyped properties or on primitive types will cause a `MockInjectException` exception.
- When using #Mock on more than one object of the same type in the same test class, this library will match each one via property names, which MUST be identical to the test subject class.

#### 2. Behaviors

[](#2-behaviors)

\#InjectMocks and #Mock work independently and alone, or together. Details about each one:

##### 2.1. #InjectMocks

[](#21-injectmocks)

It will create a real instance through the constructor, and if there are parameters in the constructor, the following value will be used in each parameter, in this order:

- A `mock` created from the #\[Mock\] attribute, if one exists;
- The `default` value if it is an optional parameter;
- `null` if it is typed as `null`;
- Will create a `mock` if it is not a primitive type. In this case, this `mock` will not be injected in the TestCase scope;
- Finally, if the previous options are not satisfied, it will throw `MockInjectException` exception.

Obs.: You can use #Mock attribute on all, some or none of the test subject's dependencies.

##### 2.2. #Mock

[](#22-mock)

Will create a `mock` injected into the TestCase scope, without using the constructor. This creation behavior is identical to `TestCase::createMock()`.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 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

Every ~201 days

Total

5

Last Release

690d ago

Major Versions

v1.0.0 → v2.0.02022-05-14

v2.2.0 → v3.0.02024-06-20

PHP version history (2 changes)v1.0.0PHP &gt;=7.4

v3.0.0PHP &gt;=8.1

### Community

Maintainers

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

---

Top Contributors

[![silasyudi](https://avatars.githubusercontent.com/u/4693297?v=4)](https://github.com/silasyudi "silasyudi (10 commits)")

---

Tags

annotationsinject-mocksmocksphpphpunittesting-toolstestsphpunitannotationstestsmockstesting-toolsinject-mocks

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/symfonyboot-inject-mocks/health.svg)

```
[![Health](https://phpackages.com/badges/symfonyboot-inject-mocks/health.svg)](https://phpackages.com/packages/symfonyboot-inject-mocks)
```

###  Alternatives

[whatthejeff/nyancat-phpunit-resultprinter

Nyan Cat result printer for PHPUnit

283689.8k24](/packages/whatthejeff-nyancat-phpunit-resultprinter)[aik099/phpunit-mink

Library for using Mink in PHPUnit tests. Supports session sharing between tests in a test case.

72136.2k1](/packages/aik099-phpunit-mink)[phpunit/phpunit-dom-assertions

DOM assertions for PHPUnit

29343.5k11](/packages/phpunit-phpunit-dom-assertions)[janmarek/mockista

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

29221.0k28](/packages/janmarek-mockista)[rybakit/phpunit-extras

Custom annotations and expectations for PHPUnit.

4778.2k1](/packages/rybakit-phpunit-extras)[hot/phpunit-runner

The lib allows to watch phpunit tests

3066.9k4](/packages/hot-phpunit-runner)

PHPackages © 2026

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