php - PHPUnit test not sharing the entity manager with Symfony

I'm running Symfony 5.4 and PHPUnit 9.5. I have a test which extends Symfony\Bundle\FrameworkBundle\Test\WebTestCase. I create an entity in my test, then execute the code under test. However, in my app's code, the entity is not to be found. I've tried finding the entity directly in the called app code and using dd() to dump it out (ending the test early), but I always get null. Somehow my test is using a different entity manager from the app code. This is how I'm fetching the entity manager:

<?php
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyTest extends WebTestCase {
  protected EntityManager $entity_manager;

  protected function setUp(): void {
    $this->entity_manager = static::getContainer()->get('doctrine')->getManager();
  }

  public function testShouldCreateAnEntityThatIsVisibleInTheAppCode() {
    $user = new User();
    $user->setFirstName('Joe');
    $user->setLastName('Bloggs');
    $user->setEmail('joe.bloggs@example.com');
    $this->entity_manager->persist($user);
    $this->entity_manager->flush();

    $crawler = static::$client->request('GET', 'https://localhost/admin/show-users');
    $this->assertStringContainsString('joe.bloggs@example.com', $crawler->html());
  }
}

How do I get my test to use the same entity manager as the code under test?

Answer

Solution:

It turned out all I needed to do was add

static::$client->disableReboot();

into setUp() like so:

  protected function setUp(): void {
    static::$client->disableReboot();
    $this->entity_manager = static::getContainer()->get('doctrine')->getManager();
  }

Source