PHPackages                             webignition/symfony-test-service-injector-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. webignition/symfony-test-service-injector-trait

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

webignition/symfony-test-service-injector-trait
===============================================

Inject Symfony services into test class properties

0.2(5y ago)04.8k1MITPHPPHP &gt;=7.4.0|^8

Since Nov 13Pushed 5y ago1 watchersCompare

[ Source](https://github.com/webignition/symfony-test-service-injector-trait)[ Packagist](https://packagist.org/packages/webignition/symfony-test-service-injector-trait)[ Docs](https://github.com/webignition/symfony-test-service-injector-trait)[ RSS](/packages/webignition-symfony-test-service-injector-trait/feed)WikiDiscussions main Synced yesterday

READMEChangelog (2)Dependencies (3)Versions (3)Used By (1)

Inject Symfony Services Into Test Class Properties
==================================================

[](#inject-symfony-services-into-test-class-properties)

A trait to use in your Symfony tests to populate test class properties with their services from the container.

How to Use It
-------------

[](#how-to-use-it)

- `use TestClassServicePropertyInjectorTrait` in any test that extends `Symfony\Bundle\FrameworkBundle\Test\KernelTestCase`
- call `$this->injectContainerServicesIntoClassProperties()` in your `setUp()` method

What It Does
------------

[](#what-it-does)

Sets all private and protected properties to their respective services. For a property to be set it must:

- be `private` or `protected`
- be type-hinted to a non-scalar type
- have a type hint that matches a service in the container

Usage Example
-------------

[](#usage-example)

```
use App\Services\FooService;
use App\Services\BarService;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use webignition\SymfonyTestServiceInjectorTrait\TestClassServicePropertyInjectorTrait;

class AcmeControllerTest extends WebTestCase
{
    use TestClassServicePropertyInjectorTrait;

    private FooService $fooService;
    private BarService $barService;

    protected function setUp(): void
    {
        parent::setUp();
        $this->injectContainerServicesIntoClassProperties();
    }

    public function testUsingFooService(): void
    {
        $foo = $this->fooService->createFoo();

        // ... carry out tests that depend on FooService creating $foo
    }

    public function testUsingBarService(): void
    {
        $bar = $this->barService->createBar();

        // ... carry out tests that depend on BarService creating $bar
    }
}
```

Before-and-after Comparison
---------------------------

[](#before-and-after-comparison)

### Before

[](#before)

```
use App\Services\ServiceA;
use App\Services\ServiceB;
use App\Services\ServiceC;
use App\Services\ServiceD;
use App\Services\ServiceE;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class AcmeControllerTest extends WebTestCase
{
    private ServiceA $serviceA;
    private ServiceB $serviceB;
    private ServiceC $serviceC;
    private ServiceD $serviceD;
    private ServiceE $serviceE;

    protected function setUp(): void
    {
        parent::setUp();

        $serviceA = self::$container->get(ServiceA::class);
        if ($serviceA instanceof ServiceA) {
            $this->serviceA = $serviceA;
        }

        $serviceB = self::$container->get(ServiceB::class);
        if ($serviceB instanceof ServiceB) {
            $this->$serviceB = $serviceB;
        }

        $serviceC = self::$container->get(ServiceC::class);
        if ($serviceC instanceof ServiceC) {
            $this->$serviceC = $serviceC;
        }

        $serviceD = self::$container->get(ServiceD::class);
        if ($serviceD instanceof ServiceD) {
            $this->$serviceD = $serviceD;
        }

        $serviceE = self::$container->get(ServiceE::class);
        if ($serviceE instanceof ServiceE) {
            $this->$serviceE = $serviceE;
        }
    }
}
```

### After

[](#after)

```
use App\Services\ServiceA;
use App\Services\ServiceB;
use App\Services\ServiceC;
use App\Services\ServiceD;
use App\Services\ServiceE;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use webignition\SymfonyTestServiceInjectorTrait\TestClassServicePropertyInjectorTrait;

class AcmeControllerTest extends WebTestCase
{
    use TestClassServicePropertyInjectorTrait;

    private ServiceA $serviceA;
    private ServiceB $serviceB;
    private ServiceC $serviceC;
    private ServiceD $serviceD;
    private ServiceE $serviceE;

    protected function setUp(): void
    {
        parent::setUp();
        $this->injectContainerServicesIntoClassProperties();
    }
}
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity45

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

Total

2

Last Release

1958d ago

PHP version history (2 changes)0.1PHP &gt;=7.4.0

0.2PHP &gt;=7.4.0|^8

### Community

Maintainers

![](https://www.gravatar.com/avatar/278be37d1b614ef0c40b22b777663e545a1f6d69dd4f908888cbb557ad7e608f?d=identicon)[webignition](/maintainers/webignition)

---

Top Contributors

[![webignition](https://avatars.githubusercontent.com/u/76546?v=4)](https://github.com/webignition "webignition (5 commits)")

---

Tags

testingsymfonytestinjectionpropertyinject

###  Code Quality

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/webignition-symfony-test-service-injector-trait/health.svg)

```
[![Health](https://phpackages.com/badges/webignition-symfony-test-service-injector-trait/health.svg)](https://phpackages.com/packages/webignition-symfony-test-service-injector-trait)
```

###  Alternatives

[zenstruck/mailer-test

Alternative, opinionated helpers for testing emails sent with symfony/mailer.

45747.8k7](/packages/zenstruck-mailer-test)[happyr/service-mocking

Make it easy to mock services in a built container

47269.0k2](/packages/happyr-service-mocking)[quizlet/hammock

Hammock is a stand-alone mocking library for Hacklang.

27445.5k](/packages/quizlet-hammock)[albertcht/lumen-testing

Testing Suite For Lumen like Laravel does.

4335.9k1](/packages/albertcht-lumen-testing)[lastzero/test-tools

Increases testing productivity by adding a service container and self-initializing fakes to PHPUnit

2244.5k13](/packages/lastzero-test-tools)[sofa/eloquent-testsuite

Helpers for fast and reliable UNIT tests for your Eloquent Models with PHPUnit

10107.6k](/packages/sofa-eloquent-testsuite)

PHPackages © 2026

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