8

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.

1 Answer 1

13

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:

  • Create an instance of ContainerBuilder
  • Register some stub services
  • Create the compiler pass
  • Call $pass->process($container);
  • Assert that the pass did its thing correctly

For an example of this, take a look at the RemoveUnusedDefinitionsPassTest.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thanks. I think testing it in true isolation would be ridiculously overkill, so integration testing is perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.