In Symfony 3.4 I was using this method to get doctrine entities in a test class:
Test class (snippet):
$kernel = self::bootKernel();
$em = $kernel->getContainer()
->get('doctrine')
->getManager();
This is deprecated.
I am attempting to inject the entity manager into my tests like this:
services.yml
Tests\AppBundle\NewUserTest:
public: true
autowire: true
calls:
- [ setEntityManager, ['@doctrine.orm.entity_manager']]
Test class (snippet, namespace = "Tests\AppBundle"):
/**
* @param EntityManager $em
*/
public function setEntityManager(EntityManager $em)
{
self::$em = $em;
}
I can't inject using constructor injection (as WebTestCases require a bunch of constructor params which I can't access)
Could someone help me out with this? I've been searching all over the place for this with no solution. There are some similar questions but not in a test environment.
Thanks :)
I think you can create client for it. Client has all services, you need.
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class WebTest extends WebTestCase
{
private $client;
public function setUp()
{
$this->client = static::createClient();
}
public function getLoggedInClient()
{
$session = $this->client->getContainer()->get('session');
}
}