From 69fc890417df3b72901354c5c12b64c8a71120df Mon Sep 17 00:00:00 2001 From: Saidi Date: Thu, 28 Nov 2024 14:32:59 +0100 Subject: [PATCH 1/2] trait to disable listener #3346 --- .../DoctrineListenerRemoverTrait.php | 24 ++++++ .../DoctrineListenerRemoverTraitTest.php | 77 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/EventListener/Behaviour/DoctrineListenerRemoverTrait.php create mode 100644 tests/Unit/EventListener/Behaviour/DoctrineListenerRemoverTraitTest.php diff --git a/src/EventListener/Behaviour/DoctrineListenerRemoverTrait.php b/src/EventListener/Behaviour/DoctrineListenerRemoverTrait.php new file mode 100644 index 000000000..2e8b01303 --- /dev/null +++ b/src/EventListener/Behaviour/DoctrineListenerRemoverTrait.php @@ -0,0 +1,24 @@ +getListeners($event) as $listener) { + if ($listener instanceof $listenerClass) { + $eventManager->removeEventListener([$event], $listener); + } + } + } + + public function removeListeners(EventManager $eventManager, string $listenerClass, array $events): void + { + foreach ($events as $event) { + $this->removeListener($eventManager, $listenerClass, $event); + } + } +} diff --git a/tests/Unit/EventListener/Behaviour/DoctrineListenerRemoverTraitTest.php b/tests/Unit/EventListener/Behaviour/DoctrineListenerRemoverTraitTest.php new file mode 100644 index 000000000..92e722682 --- /dev/null +++ b/tests/Unit/EventListener/Behaviour/DoctrineListenerRemoverTraitTest.php @@ -0,0 +1,77 @@ +eventManagerMock = $this->createMock(EventManager::class); + } + + public function testRemoveListenerWhenClassUsingTrait(): void + { + $listener = $this->createMock(ListenerToRemove::class); + + $this->eventManagerMock + ->expects($this->once()) + ->method('getListeners') + ->with('postPersist') + ->willReturn([$listener]); + + $this->eventManagerMock + ->expects($this->once()) + ->method('removeEventListener') + ->with(['postPersist'], $listener); + + $classUsingTrait = new class { + use DoctrineListenerRemoverTrait; + }; + + $classUsingTrait->removeListener( + $this->eventManagerMock, + ListenerToRemove::class, + 'postPersist' + ); + } + + public function testDoNotRemoveListenerWhenClassNotUsingTrait(): void + { + $listener = $this->createMock(Listener::class); + + $this->eventManagerMock + ->expects($this->once()) + ->method('getListeners') + ->with('postPersist') + ->willReturn([$listener]); + + $this->eventManagerMock + ->expects($this->never()) + ->method('removeEventListener'); + + $classUsingTrait = new class { + use DoctrineListenerRemoverTrait; + }; + + $classUsingTrait->removeListener( + $this->eventManagerMock, + ListenerToRemove::class, + 'postPersist' + ); + } +} + +class ListenerToRemove // As EntityHistoryListener +{ +} + +class Listener +{ +} From e6b3a7fc5bc05d09438a0cd19828c7f127ab5ebf Mon Sep 17 00:00:00 2001 From: Saidi Date: Thu, 28 Nov 2024 16:08:59 +0100 Subject: [PATCH 2/2] disable listener from manager #3346 --- src/Manager/HistoryEntryManager.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Manager/HistoryEntryManager.php b/src/Manager/HistoryEntryManager.php index b1bbbd161..494503385 100644 --- a/src/Manager/HistoryEntryManager.php +++ b/src/Manager/HistoryEntryManager.php @@ -11,18 +11,24 @@ use App\Entity\HistoryEntry; use App\Entity\Partner; use App\Entity\Signalement; +use App\EventListener\Behaviour\DoctrineListenerRemoverTrait; +use App\EventListener\EntityHistoryListener; use App\Factory\HistoryEntryFactory; use App\Repository\AffectationRepository; use App\Repository\HistoryEntryRepository; use App\Repository\PartnerRepository; use App\Service\TimezoneProvider; use Doctrine\Common\Collections\Collection; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Events; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Serializer\Exception\ExceptionInterface; class HistoryEntryManager extends AbstractManager { + use DoctrineListenerRemoverTrait; + public const FORMAT_DATE_TIME = 'Y-m-d H:i:s'; public function __construct( @@ -101,6 +107,18 @@ public function getAffectationHistory(int $signalementId): array return $formattedHistory; } + public function removeEntityListeners(): void + { + /** @var EntityManagerInterface $objectManager */ + $objectManager = $this->managerRegistry->getManager(); + $eventManager = $objectManager->getEventManager(); + $this->removeListeners( + $eventManager, + EntityHistoryListener::class, + [Events::postPersist, Events::postUpdate, Events::preRemove] + ); + } + private function getHistoryEntries(int $signalementId, string $entityClass, ?HistoryEntryEvent $event = null): array { $criteria = ['entityName' => str_replace($this->historyEntryFactory::ENTITY_PROXY_PREFIX, '', $entityClass)];