PHPackages                             pkly/phpunit-service-create-trait - 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. pkly/phpunit-service-create-trait

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

pkly/phpunit-service-create-trait
=================================

A helper trait for PHPUnit 10+ for easier creation of services with dependencies in unit testing

1.1.1(1mo ago)01.5k↑70.6%8MITPHPPHP &gt;=8.4

Since Nov 27Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/pkly/phpunit-service-create-trait)[ Packagist](https://packagist.org/packages/pkly/phpunit-service-create-trait)[ RSS](/packages/pkly-phpunit-service-create-trait/feed)WikiDiscussions master Synced today

READMEChangelog (6)Dependencies (7)Versions (9)Used By (8)

PHPUnit Service Create Trait
============================

[](#phpunit-service-create-trait)

A helper trait for PHPUnit 12+ for easier creation of services with dependencies in unit testing

[![Packagist Downloads](https://camo.githubusercontent.com/284d8524351711e4169080d9ce59d42403bdd109ea90a8c3495d55856c572650/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f706b6c792f706870756e69742d736572766963652d6372656174652d7472616974)](https://packagist.org/packages/pkly/phpunit-service-create-trait)

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

[](#installation)

Simply run

```
composer require --dev pkly/phpunit-service-create-trait

```

Currently compatible only with PHPUnit 12+

Usage
-----

[](#usage)

In any of your PHPUnit test cases simply

```
class MyTestCase extends \PHPUnit\Framework\TestCase {
    use \Pkly\ServiceMockHelperTrait;

    private AnyClass $service;

    public function setUp(): void {
        $this->service = $this->createRealMockedServiceInstance(AnyClass::class);
    }

    public function testSomething(): void
    {
        $mock = $this->createMock(MyEntity::class);

        $this->getMockedService(EntityManagerInterface::class)
            ->expects($this->once())
            ->method('delete')
            ->with($mock);

        $this->service->deleteSomething($mock);
    }
}
```

Any dependencies in the constructor as well as methods marked with Symfony's `#[Required]` attribute will be automatically plugged in with mocks. This allows you to write complex tests without wasting time updating your construct calls each time you modify something.

### Okay, but what if I need to use something custom?

[](#okay-but-what-if-i-need-to-use-something-custom)

Simply assign the proper parameter name in either `$constructor` or `$required` in the appropriate methods. That will use your object instead of creating one for you, keep in mind you cannot retrieve it via `$this->getMockedService()`.

### Partial objects?

[](#partial-objects)

Sure, works the same, just use `createRealPartialMockedServiceInstance` instead of `createRealMockedServiceInstance`, in that case you must also specify the methods to override in your mock. Returned instance is `T&MockObject`.

### Feature requests?

[](#feature-requests)

Sure, hit me up with an issue if you wish.

###  Health Score

51

—

FairBetter than 95% of packages

Maintenance91

Actively maintained with recent releases

Popularity19

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity66

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

Recently: every ~226 days

Total

6

Last Release

43d ago

PHP version history (2 changes)1.0.0PHP &gt;=8.2

1.1.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/0642c17604cd51622ad504c999eb75ba78e5768bfa998eaa2922366fc90f0302?d=identicon)[pkly](/maintainers/pkly)

---

Top Contributors

[![pkly](https://avatars.githubusercontent.com/u/17160364?v=4)](https://github.com/pkly "pkly (21 commits)")

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pkly-phpunit-service-create-trait/health.svg)

```
[![Health](https://phpackages.com/badges/pkly-phpunit-service-create-trait/health.svg)](https://phpackages.com/packages/pkly-phpunit-service-create-trait)
```

###  Alternatives

[brianium/paratest

Parallel testing for PHP

2.5k136.1M986](/packages/brianium-paratest)[drupal/core-dev

require-dev dependencies from drupal/drupal; use in addition to drupal/core-recommended to run tests from drupal/core.

2022.6M343](/packages/drupal-core-dev)[webmozarts/strict-phpunit

Enables type-safe comparisons of objects in PHPUnit

30314.4k7](/packages/webmozarts-strict-phpunit)

PHPackages © 2026

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