Does anyone have any advice for how to unit test a Symfony2 CompilerPassInterface::process()
instance?
In particular, the ones I want to test generally deal with tagged items, adding method calls to a "manager" service.
Compiler passes give you an opportunity to manipulate other service definitions that have been registered with the service container. You can read about how to create them in the components section "Compiling the Container". Compiler passes are registered in the build() method of the application kernel: Copy.
It's a best practice to create a separate project for your tests, so they are kept separate from your application code.
If you want to test it in isolation, you'll have to mock the ContainerBuilder
, and mock any service definition it returns.
That's usually quite annoying though. So I would tend to write an integration test instead. And in fact, that's what most of the compiler pass tests of symfony core do as well.
You would:
ContainerBuilder
$pass->process($container);
For an example of this, take a look at the RemoveUnusedDefinitionsPassTest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With