dmaicher / doctrine-test-bundle

Symfony bundle to isolate your app's doctrine database tests and improve the test performance
MIT License
1.08k stars 61 forks source link

How to handle multiple databases? #303

Closed MaikL closed 3 months ago

MaikL commented 3 months ago

Hello David,

thanks for your awesome work. It is useful to not reset the database on every testrun but also have the opportunity to test INSERT, UPDATE and DELETE actions. Yet, I cannot use it (yet?) for multiple databases. In the Symfony documentation it is well described and I use this technique since years now. https://symfony.com/doc/current/doctrine/multiple_entity_managers.html To easy the database handling, I delegated e.g. the "save" action to the Repository.

public function save(Product $entity, bool $flush = false): void
    {
        $this->getEntityManager()->persist($entity);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

It seems, that this is not rolled back properly, I expect because it is outsourced to the Repository?! I tried to handle this in the Controller, but I can´t wrap my head around it.

$customers = $doctrine->getRepository(Customer::class, 'customer')

If I would use this, I still can´t change data, because I would need:

EntityManagerInterface $entityManager,
OR
ManagerRegistry $doctrine

for this to work. And when I try to use this:

$entityManager->persist($product);
$entityManager->flush();

I always end up with the following:

The class 'App\Entity\Customer\Product' was not found in the chain configured namespaces App\Entity\Main

Which is logical, as I have no possibility to change the connection, when using the entityManager Object. Would be good, if someone could point me in the right direction.

dmaicher commented 3 months ago

Sorry but I don't understand your issue and I'm not sure how its related to this Bundle. I'm using this bundle on a project with multiple connections and it works just fine.

I would kindly ask you to try to get support via StackOverflow or Symfony Slack from the community.

In case you can show how its related to this bundle and provide a way of reproducing the issue then I can happily take another look and re-open.