定制 tweakers/symfony-service-mock 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

tweakers/symfony-service-mock

Composer 安装命令:

composer require tweakers/symfony-service-mock

包简介

Proxy layer to allow services to have their internals replaced with a test double, without affecting a service-container's (i.e. Symfony) bindings.

README 文档

README

Introduction

This library is designed to be used with Symfony's service-container and leverages Ocramius' Proxy Manager to allow you to configure an 'original' implementation of a service that can be changed to an 'alternative' implementation on-the-fly.

As of Symfony 4.0 you're not allowed to change or remove a service once it has been initialized. As such, when you need a temporary test double in a unit or functional test, you cannot simply replace the service.

This library allows you to add an alternative configuration to Symfony, which replaces the service with a special proxy that will allow you to gain complete control over the 'internals', even after Symfony has initialized the service and started using it as dependency in related services.

Installation

Just include this as a dev dependency:

composer require --dev tweakers/symfony-service-mock

How to use

Any service that is configured in Symfony can be re-configured in the test-environment's specific services.yaml. For this library to work optimally, you should reconfigure those services as a decorator.

If you have a service 'App\TestService' in your regular configuration, you can configure it like this to allow mocking its internal behavior:

# In config\packages\test\services.yaml:
services:
  _defaults:
    public: true
    autowire: true

  Tweakers\Test\MockableService\MockableServiceProxyFactory: ~

  App\TestService_mocked:
    decorates: App\TestService
    decoration_inner_name: 'App\TestService.inner'
    factory: ['@Tweakers\Test\MockableService\MockableServiceProxyFactory', 'createServiceProxy']
    arguments: ['@App\TestService.inner']

With just this test-configuration, there is normally no difference in behavior. Although there may be minor changes due to the use of a proxy (see Ocramius' manual) or the fact that it was made public.

But all services that depend on the App\TestService will now use the newly configured decorating proxy. Which by default falls back to the original service for any actual work.

Therefor this configuration allows you to adjust the behavior of the TestService without having to know which service is using it or whether Symfony already initiated it. That can be done in a unit test like so:

<?php

namespace App;

class TestService
{
    private $stuff;

    public function getStuff()
    {
        return $this->stuff;
    }

    public function setStuff($stuff)
    {
        $this->stuff = $stuff;
    }
}
<?php

namespace App\Tests;

use App\TestService;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class TestServiceTest extends KernelTestCase
{
    protected function setUp()
    {
    	parent::setUp();

    	self::bootKernel();
    }

    protected function tearDown()
    {
        parent::tearDown();
        
        // Make sure the original version is restored inside the proxy
        self::$container->get(TestService::class)->restoreOriginalService();
    }

    public function testServiceBehaviorCanBeChanged(): void
    {
        // Set the value of "stuff" to 39 in the original TestService
        $service = self::$container->get(TestService::class);
        $service->setStuff(39);

        $this->assertSame(39, $service->getStuff());

        // Set our mock as alternative for this test
        $mock = $this->createMock(TestService::class);
        $mock->method('getStuff')->willReturn(42);
        
        $service->setAlternativeService($mock);

        $this->assertSame(42, $service->getStuff());
        
        // Revert to original service
        $service->restoreOriginalService();
        $this->assertSame(39, $service->getStuff());
    }
}

Compatibility

This code has only been tested with Symfony 3.4, 4.1 and 4.2, although it should work with 4.0 and older versions. In Symfony 4.1 a TestContainer was introduced that gives access to private services. So it may not be necessary to define a service as public with Symfony 4.1 and higher. Symfony however does 'inline' or completely remove unused private services (even in 4.1), so tests may fail due to missing services if you define everything private. To prevent inlining, the example above creates the proxies with public=true, but it may also be necessary to redefine specific services as well.

tweakers/symfony-service-mock 适用场景与选型建议

tweakers/symfony-service-mock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69.65k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 tweakers/symfony-service-mock 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 6
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-16