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.0.3(2y ago)01.2k—0%6MITPHPPHP &gt;=8.2

Since Nov 27Pushed 2y 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 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (6)

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

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

A helper trait for PHPUnit 10+ 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 10 (11?)

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`.

### Tests? More examples?

[](#tests-more-examples)

I'll add them shortly, for now this code is being used thoroughly in a few of the projects I work at and I grew tired of updating it across multiple repositories. It's also very simple, so I doubt anyone is going to complain.

### Feature requests?

[](#feature-requests)

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

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity58

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

Total

4

Last Release

753d ago

### 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 (5 commits)")

### 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

[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[timacdonald/log-fake

A drop in fake logger for testing with the Laravel framework.

4235.9M56](/packages/timacdonald-log-fake)[jasonmccreary/laravel-test-assertions

A set of helpful assertions when testing Laravel applications.

3513.9M32](/packages/jasonmccreary-laravel-test-assertions)[ergebnis/phpunit-slow-test-detector

Provides facilities for detecting slow tests in phpunit/phpunit.

1468.1M72](/packages/ergebnis-phpunit-slow-test-detector)[typo3/testing-framework

The TYPO3 testing framework provides base classes for unit, functional and acceptance testing.

675.0M775](/packages/typo3-testing-framework)[robiningelbrecht/phpunit-pretty-print

Prettify PHPUnit output

76460.0k15](/packages/robiningelbrecht-phpunit-pretty-print)

PHPackages © 2026

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