dmaicher / doctrine-test-bundle

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

Support for PHP sub-processes #233

Closed akserikawa closed 1 year ago

akserikawa commented 1 year ago

Is there a way to share DB state between PHP main and sub-processes?

Currently, the class I want to test does something like this:

  1. Create entity in main process
  2. Edit that entity from a PHP sub-process via Symfony/Process component.

I would like to test that the result in DB matches what I expect once both steps finish.

Problem is the sub-process cannot retrieve the entity created in the main one.

ps: Note the sub-processes run asynchronously.

class A
{
    public function methodToTest()
    {
        // ...create entity in Database via it's entity repository

        $process = new Process(['bin/console', 'app:edit-entity', '--entity-id', $entity->getId()]);

        $process->start();
    }
}

class EditEntityCommand extends Command
{
    // ...
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
           $entityId = intval($input->getOption('entity-id'));

            $entityRepository = $this->entityManager->getRepository(Entity::class);
            $entity = $entityRepository->find($entityId);

            // -> ERROR: $entity is always null
    }
}

Any ideas on how to solve this? Is there a way to retrieve the changes in the entity?

dmaicher commented 1 year ago

Also see https://github.com/dmaicher/doctrine-test-bundle/issues/66

Currently this bundle requires everything to be run in the same php process so that the same PDO instance is used and the transaction logic works.

Wondering why you need to edit the entity in a different php process?

akserikawa commented 1 year ago

Thanks for the fast answer! Actually my use case is a bit more complicated than the example I provided. I'm creating/editing a long list of entities at once and I need to run parallel processes due to performance issues. Because the editing part involves updating the actual entity + sending messages to different queues via AMQP.

From #66 I tried setting StaticDriver::setKeepStaticConnections(true); in my test case without success.

There is no way to solve this ATM then?

dmaicher commented 1 year ago

There is no way to solve this ATM then?

Yeah as of now this is impossible.

I would maybe instead look into solving those performance issues? Do you run your tests in "no-debug" mode for the Symfony kernel?

Doing this in sub-processes seems very exotic :blush:

akserikawa commented 1 year ago

Sadly I currently don't have an option other than running parallel processes. Updating thousands of records at once every minute and having to serialize each record and publish it to different queues via 3rd party API (each message takes ~140ms) would take more time than the mandatory minute timeframe.

Doing this in sub-processes seems very exotic blush

I agree, exotic solution for a bit exotic use case. And PHP does not help much here either :')

I'm now considering not using this bundle for this test case in particular and let the test case persist the data to the DB. I'll have to clean up all records manually after that.

Thanks for your time though. Have a good day!

dmaicher commented 1 year ago

I'm now considering not using this bundle for this test case in particular and let the test case persist the data to the DB. I'll have to clean up all records manually after that.

Yeah then this is the only way currently indeed

dmaicher commented 1 year ago

Closing as its the same issue as https://github.com/dmaicher/doctrine-test-bundle/issues/66