Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Tomas Vortuba:
How to Test Private Services in Symfony
May 18, 2018 @ 16:21:39

Tomas Vortuba has a tutorial posted to his site showing you how to test private services in Symfony in unit tests for pre-4.1 Symfony installations (it has been resolved via simpler testing methods in Symfony 4.1 with the FrameworkBundle).

2 versions of Symfony are affected by this dissonance between services and tests. Do you use Symfony 3.4 or 4.0? Do you want to test your services, but struggle to get them in a clean way?

Today we look at possible solutions.

He starts with an example of the error you'd face if you tried to pull a service directly from the container that was marked as private. While you can specifically make it public in the yaml configuration, this potentially means doing that for all of the services you need to test. While this might work for smaller projects, it's unmaintainable for larger ones. He then shares some other options that could help resolve the issue including the one he ended up on: a compiler pass. He gets into a bit of detail on the changes this would require and where the "magic" is that lets it work.

tagged: test unittest private service symfony tutorial compiler pass

Link: https://www.tomasvotruba.cz/blog/2018/05/17/how-to-test-private-services-in-symfony/


Trending Topics: