Skip to content

Instantly share code, notes, and snippets.

@lstrojny
Created March 22, 2020 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lstrojny/a8675b47cc182101e088057ce8962122 to your computer and use it in GitHub Desktop.
Save lstrojny/a8675b47cc182101e088057ce8962122 to your computer and use it in GitHub Desktop.
SF functional test example
public function testSomething(): void
{
$client = static::createClient();
$entity = self::$container->get('em')->find(…, 1);
$entity->setState(1);
self::$container->get('em')->persist($entity);
self::$container->get('em')->flush();
$client->request(…, …);
$entity->setState(2);
self::$container->get('em')->persist($entity);
self::$container->get('em')->persist($entity);
// Boom
self::$container->get('em')->flush();
}
@tdomarkas
Copy link

  • Make sure that self::assertSame($client->getContainer()->get('em'), self::$container->get('em'));
  • Avoid $this->em->clear(); in a HTTP request.

@lstrojny
Copy link
Author

@tdomarkas
Copy link

tdomarkas commented Mar 23, 2020

Just to clarify: avoid $this->em->clear(); in a HTTP request IF you want to introspect and operate on the same entities after $client->request() is over. 🙂

EDIT: But if that can't be done or you don't want to care what happens inside a request, indeed - just refresh the entity.

@lstrojny
Copy link
Author

ServiceResetter during Kernel boot will clear the EM no matter what, so not too much one can do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment