title: Symfony/Doctrine event listener only triggered if first of any UniTest
body: <p>I'm encoutering a &quot;Im going to turn mad&quot; problem with doctrine listeners.</p>
  <p>Quickly summed up behavior:
  I need to refresh a summary of all aquired right when:</p>
  <ul>
  <li>1 A user is created</li>
  <li>2 A right is added to the user</li>
  <li>3 A riht is added to a profile linked to the user</li>
  <li>4 others on updates but we can trigger the bug with these use case only.</li>
  </ul>
  <p>I have an event listener on thoose entities to dispatch a message to re-compute the summary.
  Here's how it's setup.</p>
  <pre><code>services.yaml
      App\EventListener\DroitResume\ListenerOne:
          tags:
              - { name: 'doctrine.orm.entity_listener', event: 'postPersist', entity: 'App\Entity\Auth\UserAccount', method: 'postPersistUserAccount', lazy: true}
              - { name: 'doctrine.orm.entity_listener', event: 'postPersist', entity: 'App\Entity\Auth\UserRight', method: 'postChangeUserRight', lazy: true}
              - { name: 'doctrine.orm.entity_listener', event: 'postPersist', entity: 'App\Entity\Auth\UserProfileRight', method: 'postChangeUserProfileRight', lazy: true}
      App\EventListener\DroitResume\ListenerTwo:
          tags:
              - { name: 'doctrine.orm.entity_listener', event: 'postPersist', entity: 'App\Entity\Auth\UserAccount', method: 'postPersistUserAccount', lazy: true}
  </code></pre>
  <p>Second listener is here for demonstration/test purpose, usualcode only has one.</p>
  <pre><code>ListenerOne.php
  class ListenerOne
  {
      private MessageBusInterface $messageBus;
  
      public function __construct(MessageBusInterface $messageBus)
      {
          $this-&gt;messageBus = $messageBus;
      }
  
      public function postChangeUserRight(UserRight $userRight): void
      {
          print &quot;ListenerOne =&gt; dispatch postChangeUserRight\n&quot;;
          $this-&gt;messageBus-&gt;dispatch(new UserRefreshResumeRightsMessage($userRight-&gt;getUserAccount()-&gt;getId()));
      }
  
      public function postChangeUserProfileRight(UserProfileRight $userProfileRight): void
      {
          print &quot;ListenerOne =&gt; dispatch postChangeUserProfileRight\n&quot;;
          $this-&gt;messageBus-&gt;dispatch(new ProfileRefreshResumeRightsMessage($userProfileRight-&gt;getUserProfile()-&gt;getId()));
      }
  
      public function postPersistUserAccount(UserAccount $userAccount)
      {
          print &quot;ListenerOne =&gt; dispatch postPersistUserAccount\n&quot;;
          $this-&gt;messageBus-&gt;dispatch(new UserRefreshResumeRightsMessage($userAccount-&gt;getId()));
      }
  }
  
  
  ListenerTwo.php
  class ListenerTwo
  {
      private MessageBusInterface $messageBus;
  
      public function __construct(MessageBusInterface $messageBus)
      {
          $this-&gt;messageBus = $messageBus;
      }
  
      public function postPersistUserAccount(UserAccount $userAccount)
      {
          print &quot;ListenerTwo =&gt; dispatch postPersistUserAccount\n&quot;;
          $this-&gt;messageBus-&gt;dispatch(new UserRefreshResumeRightsMessage($userAccount-&gt;getId()));
      }
  }
  </code></pre>
  <p>Then thoose unit tests</p>
  <pre><code>RefreshRightTest.php
  class RefreshRightTest extends MyTestCase {  //MyTestCase extends KernelTestCase and add some usefull methods
  
      public function testPersistUserOne() {
          $em = $this-&gt;getEntityManager();
          
          $user = new UserAccount();
          $user-&gt;setName('blabla One');
          $user-&gt;setEmail('blablaone@test.com');
          print &quot;Persist User Blabla One\n&quot;;
          $em-&gt;persist($user);
          $em-&gt;flush();
  
          $user = new UserAccount();
          $user-&gt;setName('blabla Two');
          $user-&gt;setEmail('blablatwo@test.com');
          print &quot;Persist User Blabla Two\n&quot;;
          $em-&gt;persist($user);
          $em-&gt;flush();
      }
  
      public function testPersistUserRightOne() {
          $em = $this-&gt;getEntityManager();
  
          $user = $this-&gt;getExistingUserFromName('Existing User One');
          
          $userRight = new UserRight();
          $userRight-&gt;setUserAccount($user);
          $userRight-&gt;setRightKey('RIGHT_ONE');
          $userRight-&gt;setValue(1);
          print &quot;Persist UserRight One\n&quot;;
          $em-&gt;persist($userRight);
          $em-&gt;flush();
      }
  
      public function testPersistUserTwo() {
          $em = $this-&gt;getEntityManager();
          
          $user = new UserAccount();
          $user-&gt;setName('blabla Three');
          $user-&gt;setEmail('blablathree@test.com');
          print &quot;Persist User Blabla Three\n&quot;;
          $em-&gt;persist($user);
          $em-&gt;flush();
      }
  
  
      public function testPersistUserRightTwo() {
          $em = $this-&gt;getEntityManager();
  
          $user = $this-&gt;getExistingUserFromName('Existing User Two');
          
          $userRight = new UserRight();
          $userRight-&gt;setUserAccount($user);
          $userRight-&gt;setRightKey('RIGHT_ANY');
          $userRight-&gt;setValue(2);
          print &quot;Persist UserRight Two\n&quot;;
          $em-&gt;persist($userRight);
          $em-&gt;flush();
      }
  }
  </code></pre>
  <p>Here the console output when I run the tests:</p>
  <pre><code>Persist User Blabla One
  ListenerOne =&gt; dispatch postPersistUserAccount
  ListenerTwo =&gt; dispatch postPersistUserAccount
  Persist User Blabla Two
  ListenerOne =&gt; dispatch postPersistUserAccount
  ListenerTwo =&gt; dispatch postPersistUserAccount
  Persist UserRight One
  ListenerOne =&gt; dispatch postChangeUserRight
  Persist User Blabla Three
  Persist UserRight Two
  ListenerOne =&gt; dispatch postChangeUserRight
  </code></pre>
  <p>As you can see postPersistUserAccount listeners are only triggered if it's in the first test.
  As soon as any other listener is triggered it never triggers again.
  I can run any amount of tests on postChangeUserRight or postChangeUserProfileRight they will always trigger. But any listener on postPersistUserAccount won't trigger past first test is run.</p>
  <p>I don't have any other listner on any of thoose entities.
  I don't have any stop propagation anywhere in the code.
  The only difference between entities is that UserAccout implements UserInterface (as it's my users table)</p>
  <p>Has anyone encountered something similar before. I've tried everything I can to try and find what's happening, but nothing consistent as emerged yet.</p>
  <p>Some exemples of test order run:</p>
  <pre><code>T1: persist UserAccount
  T2: persist UserRight
  T3: persist UserAccount
  =&gt; postPersistUserAccount only triggers on T1
  
  
  T1: persist UserRight
  T2: persist UserAccount
  T3: persist UserAccount
  T4: persist UserRight
  =&gt; postPersistUserAccount never triggers, postPersistUserRight triggers ont Tests 1 and 4 as intended
  </code></pre>