Addon::initAddon() should call bootstrap only once

This commit is contained in:
Art4 2025-01-03 13:18:34 +00:00
parent b47fc318a5
commit a85343dbed
2 changed files with 21 additions and 0 deletions

View file

@ -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);

View file

@ -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);