承接 shopsys/phpunit-injector 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shopsys/phpunit-injector

Composer 安装命令:

composer require shopsys/phpunit-injector

包简介

Injects services from a PSR-11 dependency injection container to PHPUnit test cases

README 文档

README

Build

Provides a PHPUnit listener to inject services from a PSR-11 dependency injection container to PHPUnit test cases.

Services are injected to test cases that implement Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase to any property tagged with @inject.

Symfony DependencyInjection component integration is also provided.

Installation

Composer

composer require --dev zalas/phpunit-injector

Phar

The extension is also distributed as a PHAR, which can be downloaded from the most recent Github Release.

Put the extension in your PHPUnit extensions directory. Remember to instruct PHPUnit to load extensions in your phpunit.xml:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.0/phpunit.xsd"
         extensionsDirectory="tools/phpunit.d"
>
</phpunit>

Configuration

Enable the service injector listener in the PHPUnit configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.0/phpunit.xsd">

    <!-- ... -->

    <listeners>
        <listener class="Zalas\Injector\PHPUnit\TestListener\ServiceInjectorListener" />
    </listeners>
</phpunit>

Usage

To inject services using any PSR-11 service container, implement the Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase and tag selected properties with @inject:

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase;

class ServiceInjectorTest extends TestCase implements ServiceContainerTestCase
{
    /**
     * @inject
     */
    private SerializerInterface $serializer;

    /**
     * @inject logger
     */
    private LoggerInterface $logger;

    public function testThatServicesAreInjected()
    {
        $this->assertInstanceOf(SerializerInterface::class, $this->serializer, 'The service is injectd by its type');
        $this->assertInstanceOf(LoggerInterface::class, $this->logger, 'The service is injected by its id');
    }

    public function createServiceContainer(): ContainerInterface
    {
        // create a service container here
    }
}

The service is found by its type, or an id if it's given in the @inject tag.

The createServiceContainer method would be usually provided by a base test case or a trait. In case of Symfony, such a trait is provided by this package (see the next section).

Symfony Test Container (Symfony >= 4.1)

The Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer trait provides access to the test container (introduced in Symfony 4.1). Including the trait in a test case implementing the ServiceContainerTestCase will make that services are injected into annotated properties:

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer;
use Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase;

class ServiceInjectorTest extends TestCase implements ServiceContainerTestCase
{
    use SymfonyTestContainer;

    /**
     * @inject
     */
    private SerializerInterface $serializer;

    /**
     * @inject logger
     */
    private LoggerInterface $logger;

    public function testThatServicesAreInjected()
    {
        $this->assertInstanceOf(SerializerInterface::class, $this->serializer, 'The service is injectd by its type');
        $this->assertInstanceOf(LoggerInterface::class, $this->logger, 'The service is injected by its id');
    }
}

Note that test needs to be set to true in your test environment configuration for the framework bundle:

framework:
    test: true

Even though services are automatically made private by Symfony, the test container makes them available in your tests. Note that this only happens for private services that are actually used in your app (so are injected into a public service, i.e. a controller). If a service is not injected anywhere, it's removed by the container compiler.

The kernel used to bootstrap the container is created in a similar way to the KernelTestCase known from the FrameworkBundle. Similar environment variables are supported:

  • KERNEL_CLASS required - kernel class to instantiate to create the service container
  • APP_ENV default: test - kernel environment
  • APP_DEBUG default: false - kernel debug flag

These could for example be configured in phpunit.xml, or via global variables.

Symfony Container (Symfony 3.4 & 4.0)

The Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyContainer trait gives access to the full Symfony Container and can be used with any Symfony version. Opposed to the Test Container approach for Symfony 4.1, this version provides access to each service even if it's not used by your application anywhere and would normally be removed by the compiler. This should be treated as a limitation rather than a feature.

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyContainer;
use Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase;

class ServiceInjectorTest extends TestCase implements ServiceContainerTestCase
{
    use SymfonyContainer;

    /**
     * @inject
     */
    private SerializerInterface $serializer;

    /**
     * @inject logger
     */
    private LoggerInterface $logger;

    public function testThatServicesAreInjected()
    {
        $this->assertInstanceOf(SerializerInterface::class, $this->serializer, 'The service is injectd by its type');
        $this->assertInstanceOf(LoggerInterface::class, $this->logger, 'The service is injected by its id');
    }
}

Since the test container is not available until Symfony 4.1, you'll also have to register the Zalas\Injector\PHPUnit\Symfony\Compiler\ExposeServicesForTestsPass compiler pass:

use Zalas\Injector\PHPUnit\Symfony\Compiler\ExposeServicesForTestsPass;

class Kernel extends BaseKernel
{
    // ...

    protected function build(ContainerBuilder $container)
    {
        if ('test' === $this->getEnvironment()) {
            $container->addCompilerPass(new ExposeServicesForTestsPass());
        }
    }
}

The compiler pass makes sure that even private services are available to be used in tests.

Contributing

Please read the Contributing guide to learn about contributing to this project. Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

shopsys/phpunit-injector 适用场景与选型建议

shopsys/phpunit-injector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67.15k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 shopsys/phpunit-injector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 shopsys/phpunit-injector 我们能提供哪些服务?
定制开发 / 二次开发

基于 shopsys/phpunit-injector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 67.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 28
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-05