Download the PHP package happyr/service-mocking without Composer
On this page you can find all versions of the php package happyr/service-mocking. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package service-mocking
Happyr Service Mocking
You want your tests to run as quick as possible, so you build your container once and let all your tests run on that built container. That is great!
However, when your service container is built, it is immutable. This causes problems when you want to mock a service during a functional test. There is no way for you to change the object in the service container.
Using this bundle, you can mark some services as "mockable", that will allow you to define a new custom behavior for a method in that service. If no custom behavior is defined, the service works as normal.
Install
Make sure to enable the bundle for your test environment only:
Configure services
You need to tell the bundle what services you want to mock. That could be done with
the "happyr_service_mock
" service tag or by defining a list of service ids:
PHP config (Symfony 5.3)
Yaml config
Usage
Internal
So how is this magic working?
When the container is built a new proxy class is generated from your service definition.
The proxy class acts and behaves just as the original. But on each method call it
checks the ProxyDefinition
if a custom behavior have been added.
With help from static properties, the ProxyDefinition
will be remembered even if
the Kernel is rebooted.
Limitations
This trick will not work if you have two different PHP processes, i.e. you are running your tests with Panther, Selenium etc.
We can also not create a proxy if your service is final.
We are only able to mock direct access to a service. Indirect method calls are not mocked. Example:
If we mock MyService::bar()
to return "mocked"
. You will still get "orignal"
when you call MyService::foo()
. The workaround is to mock MyService::foo()
too.
All versions of service-mocking with dependencies
friendsofphp/proxy-manager-lts Version ^1.0
symfony/config Version ^5.4 || ^6.0 || ^7.0
symfony/dependency-injection Version ^5.4 || ^6.0 || ^7.0
symfony/http-kernel Version ^5.4 || ^6.0 || ^7.0