vendredi 13 juillet 2018

Injecting Doctrine into Tests - Symfony 4.1

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 :)

Aucun commentaire:

Enregistrer un commentaire