title: Symfony 4 unit test Detached entity cannot be removed
body: <p>I've some trouble with new version of symfony, symfony 4.</p>
  
  <p>I'm trying to test a rest api but I have this error.</p>
  
  <blockquote>
    <p>Doctrine\ORM\ORMInvalidArgumentException: Detached entity App\Entity\User@000000005b034b8800000000320e4469 cannot be removed</p>
  </blockquote>
  
  <p>In my unit test, I create a user, then in the tear down I want to clean the database.</p>
  
  <p>Unit Tests:
      
  
  <pre><code>namespace App\Controller;
  
  use App\Entity\User;
  use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  
  class UserControllerTest extends WebTestCase
  {
      /**
       * @var \Doctrine\ORM\EntityManager
       */
      private $em;
  
      /**
       * {@inheritDoc}
       */
      protected function setUp()
      {
          $kernel = self::bootKernel();
  
          $this-&gt;em = $kernel-&gt;getContainer()
              -&gt;get('doctrine')
              -&gt;getManager();
      }
  
      protected function tearDown()
      {
          parent::tearDown();
          $users = $this-&gt;em-&gt;getRepository(User::class)-&gt;findAll();
          foreach ($users as $user) {
              $this-&gt;em-&gt;remove($user);
              $this-&gt;em-&gt;flush($users);
          }
          $this-&gt;em-&gt;close();
          $this-&gt;em = null; // avoid memory leaks
      }
  
      public function testGetAllEmpty()
      {
          $client = static::createClient();
          $client-&gt;request('GET', '/users');
          $this-&gt;assertEquals(200, $client-&gt;getResponse()-&gt;getStatusCode());
          $this-&gt;assertEquals([], json_decode($client-&gt;getResponse()-&gt;getContent()));
      }
  
      public function testGetAllNotEmpty()
      {
          $this-&gt;createUser();
          $client = static::createClient();
          $client-&gt;request('GET', '/users');
          $this-&gt;assertEquals(200, $client-&gt;getResponse()-&gt;getStatusCode());
          $content = json_decode($client-&gt;getResponse()-&gt;getContent(), true);
  //        $this-&gt;assertCount(1, $content);
  //        var_dump($content);
  //        $this-&gt;assertCount(1, $content["pseudo"]);
      }
  
      public function createUser()
      {
          $user = new User();
          $user-&gt;setPseudo('mitsi');
          $user-&gt;setPassword('dev');
          $this-&gt;em-&gt;persist($user);
          $this-&gt;em-&gt;flush();
          return $user;
      }
  }
  </code></pre>
  
  <p>The Controller</p>
  
  <pre><code>&lt;?php
  
  namespace App\Controller;
  
  
  use App\Entity\User; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request;
  
  class UserController extends Controller {
      public function getAll()
      {
          return new JsonResponse($this-&gt;getDoctrine()-&gt;getRepository(User::class)-&gt;findAll());
      }
  
      public function get($id)
      {
  
      }
  
      public function add(Request $request)
      {
  
      }
  
      public function edit($id)
      {
  
      }
  
      public function delete($id)
      {
  
      } 
  }
  </code></pre>
  
  <p>The User</p>
  
  <pre><code>&lt;?php
  
  namespace App\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
   */
  class User
  {
      /**
       * @ORM\Id
       * @ORM\GeneratedValue
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      private $pseudo;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      private $password;
  
      /**
       * @return mixed
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * @return mixed
       */
      public function getPseudo()
      {
          return $this-&gt;pseudo;
      }
  
      /**
       * @param mixed $pseudo
       */
      public function setPseudo($pseudo): void
      {
          $this-&gt;pseudo = $pseudo;
      }
  
      /**
       * @return mixed
       */
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      /**
       * @param mixed $password
       */
      public function setPassword($password): void
      {
          $this-&gt;password = $password;
      }
  }
  </code></pre>
  
  <p>What is the problem ?</p>
  
  <p>Thank you.</p>