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

View file

@ -17,10 +17,10 @@ interface DependencyProvider
/**
* Returns an array of Dice rules.
*/
public static function provideDependencyRules(): array;
public function provideDependencyRules(): array;
/**
* 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 {
$this->bootstrapAddon($addonName);
} catch (\Throwable $th) {
// @TODO Here we can check if we have a Legacy addon and try to load it
// throw $th;
}
}
@ -46,7 +47,7 @@ final class AddonManager
$dependencies = [];
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());
}

View file

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

View file

@ -10,12 +10,18 @@ declare(strict_types=1);
namespace Friendica\Test\Unit\Addon;
use Friendica\Addon\AddonBootstrap;
use Friendica\Addon\DependencyProvider;
use Friendica\Addon\Event\AddonStartEvent;
use Friendica\Service\Addon\Addon;
use Friendica\Service\Addon\AddonProxy;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
/**
* Helper interface to combine AddonBootstrap and DependencyProvider.
*/
interface CombinedAddonBootstrapDependencyProvider extends AddonBootstrap, DependencyProvider {}
class AddonProxyTest extends TestCase
{
public function testCreateWithAddonBootstrap(): void
@ -27,6 +33,26 @@ class AddonProxyTest extends TestCase
$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
{
$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().
*/
public static function getRequiredDependencies(): array
public function getRequiredDependencies(): array
{
return [
LoggerInterface::class,
@ -56,7 +56,7 @@ class HelloAddon implements AddonBootstrap, DependencyProvider
*
* @return array<string, string>
*/
public static function getSubscribedEvents(): array
public function getSubscribedEvents(): array
{
return [
HtmlFilterEvent::PAGE_END => 'onPageEnd',
@ -66,7 +66,7 @@ class HelloAddon implements AddonBootstrap, DependencyProvider
/**
* Returns an array of Dice rules.
*/
public static function provideDependencyRules(): array
public function provideDependencyRules(): array
{
// or return require($path_to_dependencies_file);
return [
@ -80,7 +80,7 @@ class HelloAddon implements AddonBootstrap, DependencyProvider
/**
* Returns an array of strategy rules.
*/
public static function provideStrategyRules(): array
public function provideStrategyRules(): array
{
// or return require($path_to_strategies_file);
return [];