1
votes

Using Symfony\Bundle\FrameworkBundle\Test\WebTestCase, we get easy access to container, entity manager etc. We can use it for functional testing by automatic manual HTTP requests.

Can we use it to test Symfony2 console commands as well so that we can have easy access to container and all services?

I want to test my costum Symfony2 console commands which uses many services which in turn uses doctrine entity manager to access data.

PHPunit documentation suggest to extend the test class with PHPUnit_Extensions_Database_TestCase,

Can we extend WebTestCase instead of test instead to test console commands ?

I have already refereed

1

1 Answers

4
votes

WebTestCase is meant for functional testing your web applications. Nothing will stop you from using it to test commands, but it doesn't feel right (hence your question).

Testing commands

Remember, that command tests (as well as controller tests) shouldn't be complex, just like the code you're putting in there shouldn't be complex either.

Treat your commands as controllers, make them slim and put your business logic where it belongs - to the model.

Accessing the container

Having said that, you can implement your own KernelAwareTestCase (or ContainerAwareTestCase) yourself. Here's a base class I'm using occasionally: jakzal / KernelAwareTest.php Gist

Also, note that next to Symfony\Component\Console\Application there's a Symfony\Bundle\FrameworkBundle\Console\Application which can actually work with the Symfony kernel.

Final note

Remember, that the most extensive testing should be done on a unit level.