title: Symfony Doctrine relationship empty in PhpUnit test
body: <p>I have a Symfony app with a <code>User</code> entity with a many-to-many relation to a <code>Cat</code> entity. I also have a PhpUnit test which checks that deleting a cat (that belongs to 2 users) from 1 user doesn't actually delete the cat:</p>
  <pre class="lang-php prettyprint-override"><code>    public function testDeletingACatBelongingToTwoUsersOnlyDeletesTheAssociationNotTheCat()
      {
          $cat = $this-&gt;createCat();
          // Associate with user 1
          $user1 = new User();
          $user1-&gt;setEmail('test@example.com');
          $user1-&gt;setPassword('pwdpwd');
          $user1-&gt;addCat($cat);
          $this-&gt;em-&gt;persist($user1);
          // Associate with user 2
          $user2 = new User();
          $user2-&gt;setEmail('another@example.com');
          $user2-&gt;setPassword('pwdpwd');
          $user2-&gt;addCat($cat);
          $this-&gt;em-&gt;persist($user2);
          $this-&gt;em-&gt;flush();
          // Sanity check:
          $this-&gt;assertCount(1, $user1-&gt;getCats()); // PASS
          $this-&gt;assertCount(1, $user2-&gt;getCats()); // PASS
          $this-&gt;assertCount(2, $cat-&gt;getUsers()); // FAIL (0)
          // ... perform the test (not shown here)
      }
  
      private function createCat(): Cat
      {
          $cat = new Cat();
          $cat-&gt;setName($this-&gt;name);
          $this-&gt;em-&gt;persist($cat);
          $this-&gt;em-&gt;flush();
  
          return $cat;
      }
  </code></pre>
  <p>My question is, why does <code>$cat-&gt;getUsers()</code> return <code>0</code> in my test? At runtime it doesn't, it returns the correct value. It's only in the test that it returns <code>0</code>.</p>
  <p>Here are the relevant excerpts from my entities, auto-generated by Symfony:</p>
  <pre class="lang-php prettyprint-override"><code>/**
   * @ORM\Entity(repositoryClass=UserRepository::class)
   */
  class User implements UserInterface
  {
      /**
       * @ORM\ManyToMany(targetEntity=Cat::class, inversedBy=&quot;users&quot;)
       */
      private $cats;
  
      /**
       * @return Collection|Cat[]
       */
      public function getCats(): Collection
      {
          return $this-&gt;cats;
      }
  
      public function addCat(Cat $cat): self
      {
          if (!$this-&gt;cats-&gt;contains($cat)) {
              $this-&gt;cats[] = $cat;
          }
  
          return $this;
      }
  
      public function removeCat(Cat $cat): self
      {
          $this-&gt;cats-&gt;removeElement($cat);
  
          return $this;
      }
  }
  
  /**
   * @ORM\Entity(repositoryClass=CatRepository::class)
   */
  class Cat
  {
      /**
       * @ORM\ManyToMany(targetEntity=User::class, mappedBy=&quot;cats&quot;)
       */
      private $users;
  
      /**
       * @return Collection|User[]
       */
      public function getUsers(): Collection
      {
          return $this-&gt;users;
      }
  }
  </code></pre>