How to get the EntityManager inside of webtestcases in Symfony2

I was wondering how I could retrieve the entitymanager inside of a
WebTestCase instance.

e.g. to look whether a entity was really created in DB during the testrun.

Any ideas on that ?

Answer

You can retrieve the DIC (Dependency Injection Container) through the Kernel, which is a protected member of the WebTestCase.

You could do this from within your WebTestCase:

$em = $this->kernel->getContainer()->get('doctrine.orm.entity_manager');

Update

From your own comment, there is even a shortcut for this (since you will have a client anyway):

$client = $this->getClient();
$container = $client->getContainer();

As is mentioned in the docs.

Attribution
Source : Link , Question Author : stoefln , Answer Author : igorw

Leave a Comment