From a85343dbed7e97c6e0c302ea4af8cca4eb972425 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 3 Jan 2025 13:18:34 +0000 Subject: [PATCH] Addon::initAddon() should call bootstrap only once --- src/Service/Addon/AddonProxy.php | 8 ++++++++ tests/Unit/Service/Addon/AddonProxyTest.php | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Service/Addon/AddonProxy.php b/src/Service/Addon/AddonProxy.php index 47643ef60c..20704521e6 100644 --- a/src/Service/Addon/AddonProxy.php +++ b/src/Service/Addon/AddonProxy.php @@ -20,6 +20,8 @@ final class AddonProxy implements Addon { private AddonBootstrap $bootstrap; + private bool $isInit = false; + public function __construct(AddonBootstrap $bootstrap) { $this->bootstrap = $bootstrap; @@ -41,6 +43,12 @@ final class AddonProxy implements Addon public function initAddon(array $dependencies): void { + if ($this->isInit) { + return; + } + + $this->isInit = true; + $event = new AddonStartEvent($dependencies); $this->bootstrap->initAddon($event); diff --git a/tests/Unit/Service/Addon/AddonProxyTest.php b/tests/Unit/Service/Addon/AddonProxyTest.php index 8564df7ad9..ac86aff936 100644 --- a/tests/Unit/Service/Addon/AddonProxyTest.php +++ b/tests/Unit/Service/Addon/AddonProxyTest.php @@ -67,6 +67,19 @@ class AddonProxyTest extends TestCase $addon->initAddon([]); } + public function testInitAddonMultipleTimesWillCallBootstrapOnce(): void + { + $bootstrap = $this->createMock(AddonBootstrap::class); + $bootstrap->expects($this->once())->method('initAddon')->willReturnCallback(function ($event) { + $this->assertInstanceOf(AddonStartEvent::class, $event); + }); + + $addon = new AddonProxy($bootstrap); + + $addon->initAddon([]); + $addon->initAddon([]); + } + public function testInitAddonCallsBootstrapWithDependencies(): void { $bootstrap = $this->createMock(AddonBootstrap::class);