PHPackages                             e3n/testing-unit-creator - 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. e3n/testing-unit-creator

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

e3n/testing-unit-creator
========================

Creates the unit under test while mocking dependencies.

1.1.0(1y ago)10MITPHPPHP &gt;=8.1

Since Oct 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/e3n-team/testing-unit-creator)[ Packagist](https://packagist.org/packages/e3n/testing-unit-creator)[ Docs](https://e3n.de)[ RSS](/packages/e3n-testing-unit-creator/feed)WikiDiscussions main Synced 1mo ago

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

e3n Testing Unit Creator
========================

[](#e3n-testing-unit-creator)

The testing-unit-creator by e3n is a user-friendly tool designed to simplify the creation of units under test in your PHPUnit test cases. Whether you're working with regular or abstract classes, this tool provides the flexibility to generate testable units efficiently.

Features
--------

[](#features)

- creates the unit under test
- mocks the dependencies of your unit
- provides access to the mocks
- supports abstract unit under test

Installation
------------

[](#installation)

```
composer require e3n/testing-unit-creator --dev
```

Getting started
---------------

[](#getting-started)

### Use the `UnitCreatorTrait`

[](#use-the-unitcreatortrait)

The features are provided by the `UnitCreatorTrait`. There are two different ways for using this trait.

#### 1) Implement your own abstract test case class (recommended)

[](#1-implement-your-own-abstract-test-case-class-recommended)

Implement your own abstract TestCase class. Your custom test case should use the `UnitCreatorTrait` and extend from PHPUnit's `TestCase`.

```
use e3n\Test\UnitCreatorTrait;
use PHPUnit\Framework\TestCase;

/**
 * @template UNIT of object
 */
abstract class MyAbstractTestCase extends TestCase
{

    /** @use UnitCreatorTrait */
    use UnitCreatorTrait;

    protected function tearDown(): void
    {
        $this->clearUnit();
    }

    ...
}
```

Your test cases can now easily extend from your abstract test case. Provide the `@extends` annotation for code completion.

```
/**
 * @extends MyAbstractTestCase
 */
class MyClassTest extends MyAbstractTestCase
{
}
```

#### 2) Use the UnitCreatorTrait in each test case

[](#2-use-the-unitcreatortrait-in-each-test-case)

Instead of implementing an abstract test case for your project your test cases can use the `UnitCreatorTrait` directly. We do not recommend this way due to bugs in the phpstorm's code completion.

```
use e3n\Test\UnitCreatorTrait;
use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase
{

    /** @use UnitCreatorTrait */
    use UnitCreatorTrait;

    protected function tearDown(): void
    {
        $this->clearUnit();
    }

    ...
}
```

### Provide the class of your unit under test

[](#provide-the-class-of-your-unit-under-test)

There are tree ways to provide the class of your unit under test to the `UnitCreatorTrait`.

#### 1) `@covers` annotation

[](#1-covers-annotation)

The `UnitCreatorTrait` uses the first `@covers` annotation of your test case for determining your unit under test.

```
/**
 * @covers \Fully\Qualified\Class\Name\Of\MyClass
 * @extends MyAbstractTestCase
 */
class MyClassTest extends MyAbstractTestCase
{
}
```

#### 2) `CoversClass` attribute

[](#2-coversclass-attribute)

The `UnitCreatorTrait` uses the first `CoversClass` attribute of your test case for determining your unit under test.

```
/**
 * @extends MyAbstractTestCase
 */
#[CoversClass(MyClass::class)]
class MyClassTest extends MyAbstractTestCase
{
}
```

#### 3) `getUnitClass` method

[](#3-getunitclass-method)

You can implement the method `getUnitClass` to tell the `UnitCreatorTrait` the class of your unit under test.

```
/**
 * @extends MyAbstractTestCase
 */
class MyClassTest extends MyAbstractTestCase
{
    /** @return class-string */
    protected function getUnitClass(): string
    {
        return MyClass::class;
    }
}
```

### Access your unit under test

[](#access-your-unit-under-test)

#### 1) regular class

[](#1-regular-class)

```
class MyClassTest extends MyAbstractTestCase
{
    public function testMyMethod(): void
    {
        $unit   = $this->getUnit();
        $actual = $unit->myMethod();

        self::assertEquals('expectedValue', $actual);
    }
}
```

#### 2) abstract class

[](#2-abstract-class)

When testing an abstract class the `UnitCreatorTrait` provides a partial mock of your unit under test. So you have the possibility to mock the behaviour of abstract methods.

```
class MyClassTest extends MyAbstractTestCase
{
    public function testMyMethod(): void
    {
        $unit   = $this->getAbstractUnit();

        $unit->expects(self::once())
         ->method('abstractMethod')
         ->with('parameter')
         ->willReturn('expectedValue');

        $actual = $unit->myMethod();

        self::assertEquals('expectedValue', $actual);
    }
}
```

### Access mocked dependencies

[](#access-mocked-dependencies)

When creating the unit under test the `UnitCreatorTrait` mocks the dependencies. You can access those mocks by calling `$this->mock()`.

```
class MyDependency
{
    public function methodA(): string
    {
        return 'lol';
    }
}

class MyClass
{
    public function __construct(private MyDependency $dependency)
    {
    }

    public function methodB(): string
    {
        return $this->dependency->methodA();
    }
}

class MyClassTest extends MyAbstractTestCase
{
    public function testMethodB(): void
    {
        $unit   = $this->getUnit();

        $this->mock(MyDependency::class)
            ->method('methodA')
            ->willReturn('rofl');

        $actual = $unit->methodB();

        self::assertEquals('rofl', $actual);
    }
}
```

### Provide builtin constructor parameters

[](#provide-builtin-constructor-parameters)

When your unit under test requires some builtin parameters in the constructor you have to provide them by implementing the method `getUnitConstructorParameters`.

```
class MyClass
{
    public function __construct(private string $a, private int $b)
    {
    }
}

class MyClassTest extends MyAbstractTestCase
{
    protected function getUnitConstructorParameters(): array
    {
        return [
            'a' => 'rofl mao',
            'b' => 1337,
        ];
    }
}
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance44

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 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 ~26 days

Total

2

Last Release

559d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4192ecc96493ba56154d4b86d1ed9f9ea6559870221b6124591a39a93f03a151?d=identicon)[t.niebergall](/maintainers/t.niebergall)

---

Top Contributors

[![BenediktFabian](https://avatars.githubusercontent.com/u/48583034?v=4)](https://github.com/BenediktFabian "BenediktFabian (7 commits)")

---

Tags

testingphpunitdevmocke3n

### Embed Badge

![Health badge](/badges/e3n-testing-unit-creator/health.svg)

```
[![Health](https://phpackages.com/badges/e3n-testing-unit-creator/health.svg)](https://phpackages.com/packages/e3n-testing-unit-creator)
```

###  Alternatives

[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[donatj/mock-webserver

Simple mock web server for unit testing

1382.5M80](/packages/donatj-mock-webserver)[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)[ta-tikoma/phpunit-architecture-test

Methods for testing application architecture

10745.9M13](/packages/ta-tikoma-phpunit-architecture-test)[blastcloud/guzzler

Supercharge your app or SDK with a testing library specifically for Guzzle.

272419.3k35](/packages/blastcloud-guzzler)

PHPackages © 2026

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