LiipTestFixturesBundle icon indicating copy to clipboard operation
LiipTestFixturesBundle copied to clipboard

Specifying an entity manager when using the loadFixtures method

Open leksinteractive opened this issue 6 years ago • 7 comments

Is your feature request related to a problem? Please describe. Hi team, I have two different bundles in my app and each one has their own entity manager. I am writing an integration test for a service in one of the bundles, but I need to load a fixture class from the second bundle. Is this currently possible?

Describe alternatives you've considered I tried passing in the name of the entity manager for the fixture I'd like to load, in the $omName parameter of the loadFixtures method unsuccessfully. This was the main exception I kept getting:

InvalidArgumentException: Doctrine ORM Manager named "shared" does not exist.

I also tried different variations of the entity manager name but same outcome, eg: "shared", "doctrine.orm.shared_entity_manager" and "shared_entity_manager":

Additional context Here is part of my doctrine orm config with the two entity managers for more context:

orm:
    default_entity_manager: default
    auto_generate_proxy_classes: '%kernel.debug%'
    entity_managers:
        default:
            connection: client_database
            auto_mapping: true
        shared:
            connection: shared_database
            mappings:
                AdminBundle:
                    type: annotation
                    dir: '%kernel.project_dir%/src/AdminBundle/Entity'
                    prefix: AdminBundle\Entity

Thanks, -Aleks

leksinteractive avatar Jan 16 '19 11:01 leksinteractive

You can use 3rd and 4th parameters of loadFixtures for this: https://github.com/liip/LiipFunctionalTestBundle/blob/57f2b4054e86aace108cff2d9e9b7c417884ccf8/src/Test/WebTestCase.php#L250

I thought that it was documented but it looks like it's not.

alexislefebvre avatar Jan 16 '19 14:01 alexislefebvre

Thanks for the quick feedback.

This is the way I'm currently trying with those parameters, and with the orm config above:

$this->loadFixtures([SomeEntityFixtures::class], false, 'shared');

The message I get is: InvalidArgumentException: Doctrine ORM Connection named "shared" does not exist.

Am I doing something obviously wrong here?

leksinteractive avatar Jan 17 '19 00:01 leksinteractive

Can you please try with shared_database instead of shared?

alexislefebvre avatar Jan 17 '19 09:01 alexislefebvre

So if I do that I get this, slightly different message:

InvalidArgumentException: Doctrine ORM Manager named "shared_database" does not exist

Thanks.

leksinteractive avatar Jan 18 '19 04:01 leksinteractive

Did you change the order of the parameters? You have InvalidArgumentException: Doctrine ORM **Connection** named instead of InvalidArgumentException: Doctrine ORM **Manager** named. This is strange.

alexislefebvre avatar Jan 18 '19 10:01 alexislefebvre

No, I definitely didn't switch parameters - just changed the third parameter from 'shared' to 'shared_database'. I am not passing the 4th param as it defaults to 'doctrine' anyways. I agree the messages look strange, which is what threw me off initially (and prompted me to seek help here)

leksinteractive avatar Jan 18 '19 11:01 leksinteractive

Your code looks valid. We need a test with several entity_managers, it would also fix issue liip/LiipFunctionalTestBundle#470 that use several entity managers too.

alexislefebvre avatar Jan 22 '19 23:01 alexislefebvre