PHPackages                             tweakers/symfony-service-mock - 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. tweakers/symfony-service-mock

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

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.

5.0.0(2y ago)367.3k↓40.4%1MITPHPPHP ^8.0

Since Oct 16Pushed 2y ago5 watchersCompare

[ Source](https://github.com/tweakers/symfony-service-mock)[ Packagist](https://packagist.org/packages/tweakers/symfony-service-mock)[ RSS](/packages/tweakers-symfony-service-mock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (8)Used By (0)

Mockable Service proxy generator
================================

[](#mockable-service-proxy-generator)

Introduction
------------

[](#introduction)

This library is designed to be used with Symfony's service-container and leverages [Ocramius' Proxy Manager](https://packagist.org/packages/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
------------

[](#installation)

Just include this as a dev dependency:

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

```

How to use
----------

[](#how-to-use)

Any service that is configured in Symfony can be re-configured in the [test-environment's](https://symfony.com/doc/current/testing.html) specific services.yaml. For this library to work optimally, you should reconfigure those services as a [decorator](https://symfony.com/doc/current/service_container/service_decoration.html).

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](https://ocramius.github.io/ProxyManager/docs/lazy-loading-value-holder.html)) 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:

```
