Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Unit Testing Compiler Passes

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.

like image 521
samanime Avatar asked Feb 25 '13 07:02

samanime


People also ask

What is Symfony compiler pass?

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.

Should unit tests be in a separate project?

It's a best practice to create a separate project for your tests, so they are kept separate from your application code.


1 Answers

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.

like image 92
igorw Avatar answered Oct 18 '22 01:10

igorw