2

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('[email protected]');
    $this->entity_manager->persist($user);
    $this->entity_manager->flush();

    $crawler = static::$client->request('GET', 'https://localhost/admin/show-users');
    $this->assertStringContainsString('[email protected]', $crawler->html());
  }
}

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

1
  • since you found the solution, why don't you write a short answer ;o) Commented Nov 17, 2022 at 3:00

1 Answer 1

2

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();
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.