Create Addon class, that calls Addon Bootstrap

This commit is contained in:
Art4 2025-01-02 08:43:45 +00:00
parent a6a0af0670
commit b3bc5981ea
4 changed files with 74 additions and 3 deletions

32
src/Addon/Addon.php Normal file
View file

@ -0,0 +1,32 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Addon;
use Friendica\Addon\Event\AddonStartEvent;
/**
* Proxy object for an addon.
*/
final class Addon
{
private AddonBootstrap $bootstrap;
public function __construct(AddonBootstrap $bootstrap)
{
$this->bootstrap = $bootstrap;
}
public function initAddon(): void
{
$event = new AddonStartEvent();
$this->bootstrap->initAddon($event);
}
}

View file

@ -25,5 +25,5 @@ interface AddonBootstrap extends StaticEventSubscriber
/** /**
* Init of the addon. * Init of the addon.
*/ */
public static function initAddon(AddonStartEvent $event): void; public function initAddon(AddonStartEvent $event): void;
} }

View file

@ -0,0 +1,38 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Addon;
use Friendica\Addon\Addon;
use Friendica\Addon\AddonBootstrap;
use Friendica\Addon\Event\AddonStartEvent;
use PHPUnit\Framework\TestCase;
class AddonTest extends TestCase
{
public function testCreateWithAddonBootstrap(): void
{
$bootstrap = $this->createMock(AddonBootstrap::class);
$addon = new Addon($bootstrap);
$this->assertInstanceOf(Addon::class, $addon);
}
public function testInitAddonCallsBootstrap(): void
{
$bootstrap = $this->createMock(AddonBootstrap::class);
$bootstrap->expects($this->once())->method('initAddon')->willReturnCallback(function($event) {
$this->assertInstanceOf(AddonStartEvent::class, $event);
});
$addon = new Addon($bootstrap);
$addon->initAddon();
}
}

View file

@ -15,6 +15,7 @@ declare(strict_types=1);
namespace FriendicaAddons\HelloAddon; namespace FriendicaAddons\HelloAddon;
use Friendica\Addon\AddonBootstrap;
use Friendica\Addon\Event\AddonInstallEvent; use Friendica\Addon\Event\AddonInstallEvent;
use Friendica\Addon\Event\AddonStartEvent; use Friendica\Addon\Event\AddonStartEvent;
use Friendica\Addon\Event\AddonUninstallEvent; use Friendica\Addon\Event\AddonUninstallEvent;
@ -23,7 +24,7 @@ use Friendica\Event\ProvideLoggerEvent;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
class HelloAddon implements \Friendica\Addon\AddonBootstrap class HelloAddon implements AddonBootstrap
{ {
/** @var LoggerInterface */ /** @var LoggerInterface */
private static $logger; private static $logger;
@ -50,7 +51,7 @@ class HelloAddon implements \Friendica\Addon\AddonBootstrap
]; ];
} }
public static function initAddon(AddonStartEvent $event): void public function initAddon(AddonStartEvent $event): void
{ {
// $dependencies containts an array of services defined in getRequiredDependencies(). // $dependencies containts an array of services defined in getRequiredDependencies().
// The keys are the FQCN of the services. // The keys are the FQCN of the services.