-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3351 from MTES-MCT/feature/3346-disable-entity-li…
…stener [TECH] Option pour désactiver EntityHistoryListener
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/EventListener/Behaviour/DoctrineListenerRemoverTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\EventListener\Behaviour; | ||
|
||
use Doctrine\Common\EventManager; | ||
|
||
trait DoctrineListenerRemoverTrait | ||
{ | ||
public function removeListener(EventManager $eventManager, string $listenerClass, string $event): void | ||
{ | ||
foreach ($eventManager->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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
tests/Unit/EventListener/Behaviour/DoctrineListenerRemoverTraitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace App\Tests\Unit\EventListener\Behaviour; | ||
|
||
use App\EventListener\Behaviour\DoctrineListenerRemoverTrait; | ||
use Doctrine\Common\EventManager; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DoctrineListenerRemoverTraitTest extends TestCase | ||
{ | ||
private MockObject|EventManager $eventManagerMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->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 | ||
{ | ||
} |