Switch back to dynamic getter methods because static methods cannot been mocked in tests

This commit is contained in:
Art4 2025-01-03 10:12:09 +00:00
parent 73a55e4700
commit 82470738e4
6 changed files with 39 additions and 11 deletions

View file

@ -25,7 +25,7 @@ interface AddonBootstrap
* return [LoggerInterface::class]; * return [LoggerInterface::class];
* ``` * ```
*/ */
public static function getRequiredDependencies(): array; public function getRequiredDependencies(): array;
/** /**
* Return an array of events to subscribe to. * Return an array of events to subscribe to.
@ -41,7 +41,7 @@ interface AddonBootstrap
* *
* @return array<string, string> * @return array<string, string>
*/ */
public static function getSubscribedEvents(): array; public function getSubscribedEvents(): array;
/** /**
* Init the addon with the required dependencies. * Init the addon with the required dependencies.

View file

@ -17,10 +17,10 @@ interface DependencyProvider
/** /**
* Returns an array of Dice rules. * Returns an array of Dice rules.
*/ */
public static function provideDependencyRules(): array; public function provideDependencyRules(): array;
/** /**
* Returns an array of strategy rules. * Returns an array of strategy rules.
*/ */
public static function provideStrategyRules(): array; public function provideStrategyRules(): array;
} }

View file

@ -36,6 +36,7 @@ final class AddonManager
try { try {
$this->bootstrapAddon($addonName); $this->bootstrapAddon($addonName);
} catch (\Throwable $th) { } catch (\Throwable $th) {
// @TODO Here we can check if we have a Legacy addon and try to load it
// throw $th; // throw $th;
} }
} }
@ -46,7 +47,7 @@ final class AddonManager
$dependencies = []; $dependencies = [];
foreach ($this->addons as $addon) { foreach ($this->addons as $addon) {
// Here we can filter or deny dependencies from addons // @TODO Here we can filter or deny dependencies from addons
$dependencies = array_merge($dependencies, $addon->getRequiredDependencies()); $dependencies = array_merge($dependencies, $addon->getRequiredDependencies());
} }

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Friendica\Service\Addon; namespace Friendica\Service\Addon;
use Friendica\Addon\AddonBootstrap; use Friendica\Addon\AddonBootstrap;
use Friendica\Addon\DependencyProvider;
use Friendica\Addon\Event\AddonStartEvent; use Friendica\Addon\Event\AddonStartEvent;
/** /**
@ -26,13 +27,13 @@ final class AddonProxy implements Addon
public function getRequiredDependencies(): array public function getRequiredDependencies(): array
{ {
return $this->bootstrap::getRequiredDependencies(); return $this->bootstrap->getRequiredDependencies();
} }
public function getProvidedDependencyRules(): array public function getProvidedDependencyRules(): array
{ {
if ($this->bootstrap instanceof DependencyProvider) { if ($this->bootstrap instanceof DependencyProvider) {
return $this->bootstrap::provideDependencyRules(); return $this->bootstrap->provideDependencyRules();
} }
return []; return [];

View file

@ -10,12 +10,18 @@ declare(strict_types=1);
namespace Friendica\Test\Unit\Addon; namespace Friendica\Test\Unit\Addon;
use Friendica\Addon\AddonBootstrap; use Friendica\Addon\AddonBootstrap;
use Friendica\Addon\DependencyProvider;
use Friendica\Addon\Event\AddonStartEvent; use Friendica\Addon\Event\AddonStartEvent;
use Friendica\Service\Addon\Addon; use Friendica\Service\Addon\Addon;
use Friendica\Service\Addon\AddonProxy; use Friendica\Service\Addon\AddonProxy;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/**
* Helper interface to combine AddonBootstrap and DependencyProvider.
*/
interface CombinedAddonBootstrapDependencyProvider extends AddonBootstrap, DependencyProvider {}
class AddonProxyTest extends TestCase class AddonProxyTest extends TestCase
{ {
public function testCreateWithAddonBootstrap(): void public function testCreateWithAddonBootstrap(): void
@ -27,6 +33,26 @@ class AddonProxyTest extends TestCase
$this->assertInstanceOf(Addon::class, $addon); $this->assertInstanceOf(Addon::class, $addon);
} }
public function testGetRequiredDependenciesCallsBootstrap(): void
{
$bootstrap = $this->createMock(AddonBootstrap::class);
$bootstrap->expects($this->once())->method('getRequiredDependencies')->willReturn([]);
$addon = new AddonProxy($bootstrap);
$addon->getRequiredDependencies();
}
public function testGetProvidedDependencyRulesCallsBootstrap(): void
{
$bootstrap = $this->createMock(CombinedAddonBootstrapDependencyProvider::class);
$bootstrap->expects($this->once())->method('provideDependencyRules')->willReturn([]);
$addon = new AddonProxy($bootstrap);
$addon->getProvidedDependencyRules();
}
public function testInitAddonCallsBootstrap(): void public function testInitAddonCallsBootstrap(): void
{ {
$bootstrap = $this->createMock(AddonBootstrap::class); $bootstrap = $this->createMock(AddonBootstrap::class);

View file

@ -35,7 +35,7 @@ class HelloAddon implements AddonBootstrap, DependencyProvider
* *
* The dependencies will be passed to the initAddon() method via AddonStartEvent::getDependencies(). * The dependencies will be passed to the initAddon() method via AddonStartEvent::getDependencies().
*/ */
public static function getRequiredDependencies(): array public function getRequiredDependencies(): array
{ {
return [ return [
LoggerInterface::class, LoggerInterface::class,
@ -56,7 +56,7 @@ class HelloAddon implements AddonBootstrap, DependencyProvider
* *
* @return array<string, string> * @return array<string, string>
*/ */
public static function getSubscribedEvents(): array public function getSubscribedEvents(): array
{ {
return [ return [
HtmlFilterEvent::PAGE_END => 'onPageEnd', HtmlFilterEvent::PAGE_END => 'onPageEnd',
@ -66,7 +66,7 @@ class HelloAddon implements AddonBootstrap, DependencyProvider
/** /**
* Returns an array of Dice rules. * Returns an array of Dice rules.
*/ */
public static function provideDependencyRules(): array public function provideDependencyRules(): array
{ {
// or return require($path_to_dependencies_file); // or return require($path_to_dependencies_file);
return [ return [
@ -80,7 +80,7 @@ class HelloAddon implements AddonBootstrap, DependencyProvider
/** /**
* Returns an array of strategy rules. * Returns an array of strategy rules.
*/ */
public static function provideStrategyRules(): array public function provideStrategyRules(): array
{ {
// or return require($path_to_strategies_file); // or return require($path_to_strategies_file);
return []; return [];