]> git.mxchange.org Git - friendica.git/commitdiff
Adhere feedback
authorPhilipp <admin@philipp.info>
Fri, 21 Jul 2023 20:41:36 +0000 (22:41 +0200)
committerPhilipp <admin@philipp.info>
Fri, 21 Jul 2023 20:42:24 +0000 (22:42 +0200)
- rename hooks.config.php to strategies.config.php
- change all corresponding classes and tests

16 files changed:
doc/StrategyHooks.md
src/Core/Hooks/Capabilities/ICanCreateInstances.php
src/Core/Hooks/Capabilities/ICanRegisterInstances.php [deleted file]
src/Core/Hooks/Capabilities/ICanRegisterStrategies.php [new file with mode: 0644]
src/Core/Hooks/Model/DiceInstanceManager.php
src/Core/Hooks/Util/HookFileManager.php [deleted file]
src/Core/Hooks/Util/StrategiesFileManager.php [new file with mode: 0644]
src/Core/Logger/Factory/Logger.php
src/DI.php
src/Module/Admin/Summary.php
static/dependencies.config.php
static/hooks.config.php [deleted file]
static/strategies.config.php [new file with mode: 0644]
tests/src/Core/Hooks/Model/InstanceManagerTest.php
tests/src/Core/Hooks/Util/HookFileManagerTest.php [deleted file]
tests/src/Core/Hooks/Util/StrategiesFileManagerTest.php [new file with mode: 0644]

index cb355f93f00128f0a93594238f2fbc002658787d..39fc1bd8df4cea30a7d6dc0ca520c169d87d7b86 100644 (file)
@@ -43,7 +43,7 @@ public class ConcreteClassB implements ExampleInterface
        }
 }
 
-/** @var \Friendica\Core\Hooks\Capabilities\ICanRegisterInstances $instanceRegister */
+/** @var \Friendica\Core\Hooks\Capabilities\ICanRegisterStrategies $instanceRegister */
 $instanceRegister->registerStrategy(ExampleInterface::class, ConcreteClassA::class, 'A');
 $instanceRegister->registerStrategy(ExampleInterface::class, ConcreteClassB::class, 'B');
 
index f2e4b8b0a1761e12ed789cb5443e4ef242ea6e00..77d4c4b3664e6072d5f8b0df0fb8872759d53f09 100644 (file)
@@ -32,10 +32,10 @@ interface ICanCreateInstances
         * The instance will be build based on the registered strategy and the (unique) name
         *
         * @param string $class     The fully-qualified name of the given class or interface which will get returned
-        * @param string $name      An arbitrary identifier to find a concrete instance strategy.
+        * @param string $strategy  An arbitrary identifier to find a concrete instance strategy.
         * @param array  $arguments Additional arguments, which can be passed to the constructor of "$class" at runtime
         *
         * @return object The concrete instance of the type "$class"
         */
-       public function create(string $class, string $name, array $arguments = []): object;
+       public function create(string $class, string $strategy, array $arguments = []): object;
 }
diff --git a/src/Core/Hooks/Capabilities/ICanRegisterInstances.php b/src/Core/Hooks/Capabilities/ICanRegisterInstances.php
deleted file mode 100644 (file)
index f7689bb..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2023, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Core\Hooks\Capabilities;
-
-use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
-
-/**
- * Register strategies for given classes
- */
-interface ICanRegisterInstances
-{
-       /**
-        * Register a class(strategy) for a given interface with a unique name.
-        *
-        * @see https://refactoring.guru/design-patterns/strategy
-        *
-        * @param string  $interface The interface, which the given class implements
-        * @param string  $class     The fully-qualified given class name
-        *                           A placeholder for dependencies is possible as well
-        * @param ?string $name      An arbitrary identifier for the given class, which will be used for factories, dependency injections etc.
-        *
-        * @return $this This interface for chain-calls
-        *
-        * @throws HookRegisterArgumentException in case the given class for the interface isn't valid or already set
-        */
-       public function registerStrategy(string $interface, string $class, ?string $name = null): self;
-}
diff --git a/src/Core/Hooks/Capabilities/ICanRegisterStrategies.php b/src/Core/Hooks/Capabilities/ICanRegisterStrategies.php
new file mode 100644 (file)
index 0000000..911eb34
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Core\Hooks\Capabilities;
+
+use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
+
+/**
+ * Register strategies for given classes
+ */
+interface ICanRegisterStrategies
+{
+       /**
+        * Register a class(strategy) for a given interface with a unique name.
+        *
+        * @see https://refactoring.guru/design-patterns/strategy
+        *
+        * @param string  $interface The interface, which the given class implements
+        * @param string  $class     The fully-qualified given class name
+        *                           A placeholder for dependencies is possible as well
+        * @param ?string $name      An arbitrary identifier for the given strategy, which will be used for factories, dependency injections etc.
+        *
+        * @return $this This interface for chain-calls
+        *
+        * @throws HookRegisterArgumentException in case the given class for the interface isn't valid or already set
+        */
+       public function registerStrategy(string $interface, string $class, ?string $name = null): self;
+}
index 77b9c4d495f6d8e8b5d21ada70b9f8aa5e6e08b2..a8b0b540cef2e1a47347e0160490a4e3d641ed15 100644 (file)
@@ -23,31 +23,31 @@ namespace Friendica\Core\Hooks\Model;
 
 use Dice\Dice;
 use Friendica\Core\Hooks\Capabilities\ICanCreateInstances;
-use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
+use Friendica\Core\Hooks\Capabilities\ICanRegisterStrategies;
 use Friendica\Core\Hooks\Exceptions\HookInstanceException;
 use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
-use Friendica\Core\Hooks\Util\HookFileManager;
+use Friendica\Core\Hooks\Util\StrategiesFileManager;
 
 /**
  * This class represents an instance register, which uses Dice for creation
  *
  * @see Dice
  */
-class DiceInstanceManager implements ICanCreateInstances, ICanRegisterInstances
+class DiceInstanceManager implements ICanCreateInstances, ICanRegisterStrategies
 {
        protected $instance = [];
 
        /** @var Dice */
        protected $dice;
 
-       public function __construct(Dice $dice, HookFileManager $hookFileManager)
+       public function __construct(Dice $dice, StrategiesFileManager $strategiesFileManager)
        {
                $this->dice = $dice;
-               $hookFileManager->setupHooks($this);
+               $strategiesFileManager->setupStrategies($this);
        }
 
        /** {@inheritDoc} */
-       public function registerStrategy(string $interface, string $class, ?string $name = null): ICanRegisterInstances
+       public function registerStrategy(string $interface, string $class, ?string $name = null): ICanRegisterStrategies
        {
                if (!empty($this->instance[$interface][$name])) {
                        throw new HookRegisterArgumentException(sprintf('A class with the name %s is already set for the interface %s', $name, $interface));
@@ -59,12 +59,12 @@ class DiceInstanceManager implements ICanCreateInstances, ICanRegisterInstances
        }
 
        /** {@inheritDoc} */
-       public function create(string $class, string $name, array $arguments = []): object
+       public function create(string $class, string $strategy, array $arguments = []): object
        {
-               if (empty($this->instance[$class][$name])) {
-                       throw new HookInstanceException(sprintf('The class with the name %s isn\'t registered for the class or interface %s', $name, $class));
+               if (empty($this->instance[$class][$strategy])) {
+                       throw new HookInstanceException(sprintf('The class with the name %s isn\'t registered for the class or interface %s', $strategy, $class));
                }
 
-               return $this->dice->create($this->instance[$class][$name], $arguments);
+               return $this->dice->create($this->instance[$class][$strategy], $arguments);
        }
 }
diff --git a/src/Core/Hooks/Util/HookFileManager.php b/src/Core/Hooks/Util/HookFileManager.php
deleted file mode 100644 (file)
index b83f2b4..0000000
+++ /dev/null
@@ -1,107 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2023, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Core\Hooks\Util;
-
-use Friendica\Core\Addon\Capabilities\ICanLoadAddons;
-use Friendica\Core\Hooks\Capabilities\BehavioralHookType;
-use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
-use Friendica\Core\Hooks\Exceptions\HookConfigException;
-
-/**
- * Manage all hooks.config.php files
- */
-class HookFileManager
-{
-       const STATIC_DIR  = 'static';
-       const CONFIG_NAME = 'hooks';
-
-       /** @var ICanLoadAddons */
-       protected $addonLoader;
-       /** @var array */
-       protected $hookConfig = [];
-       /** @var string */
-       protected $basePath;
-
-       public function __construct(string $basePath, ICanLoadAddons $addonLoader)
-       {
-               $this->basePath    = $basePath;
-               $this->addonLoader = $addonLoader;
-       }
-
-       /**
-        * Loads all kinds of hooks and registers the corresponding instances
-        *
-        * @param ICanRegisterInstances $instanceRegister The instance register
-        *
-        * @return void
-        */
-       public function setupHooks(ICanRegisterInstances $instanceRegister)
-       {
-               // In case it wasn't used before, reload the whole hook config
-               if (empty($this->hookConfig)) {
-                       $this->reloadHookConfig();
-               }
-
-               foreach ($this->hookConfig as $hookType => $classList) {
-                       switch ($hookType) {
-                               case BehavioralHookType::STRATEGY:
-                                       foreach ($classList as $interface => $strategy) {
-                                               foreach ($strategy as $dependencyName => $names) {
-                                                       if (is_array($names)) {
-                                                               foreach ($names as $name) {
-                                                                       $instanceRegister->registerStrategy($interface, $dependencyName, $name);
-                                                               }
-                                                       } else {
-                                                               $instanceRegister->registerStrategy($interface, $dependencyName, $names);
-                                                       }
-                                               }
-                                       }
-                                       break;
-                       }
-               }
-       }
-
-       /**
-        * Reloads all hook config files into the config cache for later usage
-        *
-        * Merges all hook configs from every addon - if present - as well
-        *
-        * @return void
-        */
-       protected function reloadHookConfig()
-       {
-               // load core hook config
-               $configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
-
-               if (!file_exists($configFile)) {
-                       throw new HookConfigException(sprintf('config file %s does not exist.', $configFile));
-               }
-
-               $config = include $configFile;
-
-               if (!is_array($config)) {
-                       throw new HookConfigException(sprintf('Error loading config file %s.', $configFile));
-               }
-
-               $this->hookConfig = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));
-       }
-}
diff --git a/src/Core/Hooks/Util/StrategiesFileManager.php b/src/Core/Hooks/Util/StrategiesFileManager.php
new file mode 100644 (file)
index 0000000..700c401
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Core\Hooks\Util;
+
+use Friendica\Core\Addon\Capabilities\ICanLoadAddons;
+use Friendica\Core\Hooks\Capabilities\ICanRegisterStrategies;
+use Friendica\Core\Hooks\Exceptions\HookConfigException;
+
+/**
+ * Manage all strategies.config.php files
+ */
+class StrategiesFileManager
+{
+       const STATIC_DIR  = 'static';
+       const CONFIG_NAME = 'strategies';
+
+       /** @var ICanLoadAddons */
+       protected $addonLoader;
+       /** @var array */
+       protected $config = [];
+       /** @var string */
+       protected $basePath;
+
+       public function __construct(string $basePath, ICanLoadAddons $addonLoader)
+       {
+               $this->basePath    = $basePath;
+               $this->addonLoader = $addonLoader;
+       }
+
+       /**
+        * Loads all kinds of hooks and registers the corresponding instances
+        *
+        * @param ICanRegisterStrategies $instanceRegister The instance register
+        *
+        * @return void
+        */
+       public function setupStrategies(ICanRegisterStrategies $instanceRegister)
+       {
+               foreach ($this->config as $interface => $strategy) {
+                       foreach ($strategy as $dependencyName => $names) {
+                               if (is_array($names)) {
+                                       foreach ($names as $name) {
+                                               $instanceRegister->registerStrategy($interface, $dependencyName, $name);
+                                       }
+                               } else {
+                                       $instanceRegister->registerStrategy($interface, $dependencyName, $names);
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Reloads all hook config files into the config cache for later usage
+        *
+        * Merges all hook configs from every addon - if present - as well
+        *
+        * @return void
+        */
+       public function loadConfig()
+       {
+               // load core hook config
+               $configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
+
+               if (!file_exists($configFile)) {
+                       throw new HookConfigException(sprintf('config file %s does not exist.', $configFile));
+               }
+
+               $config = include $configFile;
+
+               if (!is_array($config)) {
+                       throw new HookConfigException(sprintf('Error loading config file %s.', $configFile));
+               }
+
+               $this->config = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));
+       }
+}
index 81f8887efcca04152ef8c9af943a534240df5f51..c370cc4dd7cc6caa7e4b05e07c9fb256149589a8 100644 (file)
@@ -43,7 +43,7 @@ class Logger
                $this->channel = $channel;
        }
 
-       public function create(ICanCreateInstances $createInstances, IManageConfigValues $config, Profiler $profiler): LoggerInterface
+       public function create(ICanCreateInstances $instanceCreator, IManageConfigValues $config, Profiler $profiler): LoggerInterface
        {
                if (empty($config->get('system', 'debugging') ?? false)) {
                        return new NullLogger();
@@ -53,7 +53,7 @@ class Logger
 
                try {
                        /** @var LoggerInterface $logger */
-                       $logger = $createInstances->create(LoggerInterface::class, $name, [$this->channel]);
+                       $logger = $instanceCreator->create(LoggerInterface::class, $name, [$this->channel]);
                        if ($config->get('system', 'profiling') ?? false) {
                                return new ProfilerLoggerClass($logger, $profiler);
                        } else {
index c62b5e8d793d814a9ed170a9651a9342860744a0..1917f710d3a77112e8780404e1b90c9384ba728b 100644 (file)
@@ -297,7 +297,7 @@ abstract class DI
                static::init($flushDice);
        }
 
-       public static function loggCheck(): ICheckLoggerSettings
+       public static function logCheck(): ICheckLoggerSettings
        {
                return self::$dice->create(LoggerSettingsCheck::class);
        }
index 80437305dccd2e1124193d09ee9ec65b75d55b47..99d0e3325bd467869d9dea79e32063395d976198 100644 (file)
@@ -126,10 +126,10 @@ class Summary extends BaseAdmin
                }
 
                // Check logfile permission
-               if (($return = DI::loggCheck()->checkLogfile()) !== null) {
+               if (($return = DI::logCheck()->checkLogfile()) !== null) {
                        $warningtext[] = $return;
                }
-               if (($return = DI::loggCheck()->checkDebugLogfile()) !== null) {
+               if (($return = DI::logCheck()->checkDebugLogfile()) !== null) {
                        $warningtext[] = $return;
                }
 
index ce8a5bd9df9356e5446b920664171ad62bce84e7..98a9f3077db211bec8140bdd2d155dbeb48138b0 100644 (file)
@@ -38,7 +38,7 @@ use Friendica\App;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\Hooks\Capabilities\ICanCreateInstances;
-use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
+use Friendica\Core\Hooks\Capabilities\ICanRegisterStrategies;
 use Friendica\Core\Hooks\Model\DiceInstanceManager;
 use Friendica\Core\PConfig;
 use Friendica\Core\L10n;
@@ -91,12 +91,15 @@ return [
                        [Dice::INSTANCE => Dice::SELF],
                ]
        ],
-       \Friendica\Core\Hooks\Util\HookFileManager::class => [
+       \Friendica\Core\Hooks\Util\StrategiesFileManager::class => [
                'constructParams' => [
                        [Dice::INSTANCE => '$basepath'],
                ],
+               'call' => [
+                       ['loadConfig'],
+               ],
        ],
-       ICanRegisterInstances::class => [
+       ICanRegisterStrategies::class => [
                'instanceOf' => DiceInstanceManager::class,
                'constructParams' => [
                        [Dice::INSTANCE => Dice::SELF],
diff --git a/static/hooks.config.php b/static/hooks.config.php
deleted file mode 100644 (file)
index d46762f..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2023, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-use Friendica\Core\Hooks\Capabilities\BehavioralHookType as H;
-use Friendica\Core\Logger\Type;
-use Psr\Log;
-
-return [
-       H::STRATEGY  => [
-               Log\LoggerInterface::class => [
-                       Log\NullLogger::class    => [''],
-                       Type\SyslogLogger::class => ['syslog'],
-                       Type\StreamLogger::class => ['stream'],
-               ],
-       ],
-];
diff --git a/static/strategies.config.php b/static/strategies.config.php
new file mode 100644 (file)
index 0000000..bb3598c
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+use Friendica\Core\Hooks\Capabilities\BehavioralHookType as H;
+use Friendica\Core\Logger\Type;
+use Psr\Log;
+
+return [
+       Log\LoggerInterface::class => [
+               Log\NullLogger::class    => [''],
+               Type\SyslogLogger::class => ['syslog'],
+               Type\StreamLogger::class => ['stream'],
+       ],
+];
index de2bdc6b834cc9c3c8302972586e32f6bca04ba7..44459eb2eeed8171ba45e21550f7e401eedf179c 100644 (file)
@@ -25,7 +25,7 @@ use Dice\Dice;
 use Friendica\Core\Hooks\Exceptions\HookInstanceException;
 use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
 use Friendica\Core\Hooks\Model\DiceInstanceManager;
-use Friendica\Core\Hooks\Util\HookFileManager;
+use Friendica\Core\Hooks\Util\StrategiesFileManager;
 use Friendica\Test\MockedTest;
 use Friendica\Test\Util\Hooks\InstanceMocks\FakeInstance;
 use Friendica\Test\Util\Hooks\InstanceMocks\FakeInstanceDecorator;
@@ -34,15 +34,15 @@ use Mockery\MockInterface;
 
 class InstanceManagerTest extends MockedTest
 {
-       /** @var HookFileManager|MockInterface */
+       /** @var StrategiesFileManager|MockInterface */
        protected $hookFileManager;
 
        protected function setUp(): void
        {
                parent::setUp();
 
-               $this->hookFileManager = \Mockery::mock(HookFileManager::class);
-               $this->hookFileManager->shouldReceive('setupHooks')->withAnyArgs();
+               $this->hookFileManager = \Mockery::mock(StrategiesFileManager::class);
+               $this->hookFileManager->shouldReceive('setupStrategies')->withAnyArgs();
        }
 
        protected function tearDown(): void
diff --git a/tests/src/Core/Hooks/Util/HookFileManagerTest.php b/tests/src/Core/Hooks/Util/HookFileManagerTest.php
deleted file mode 100644 (file)
index f718e8a..0000000
+++ /dev/null
@@ -1,240 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2023, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Test\src\Core\Hooks\Util;
-
-use Friendica\Core\Addon\Capabilities\ICanLoadAddons;
-use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
-use Friendica\Core\Hooks\Exceptions\HookConfigException;
-use Friendica\Core\Hooks\Util\HookFileManager;
-use Friendica\Test\MockedTest;
-use Friendica\Test\Util\VFSTrait;
-use org\bovigo\vfs\vfsStream;
-use Psr\Log\LoggerInterface;
-use Psr\Log\NullLogger;
-
-class HookFileManagerTest extends MockedTest
-{
-       use VFSTrait;
-
-       protected function setUp(): void
-       {
-               parent::setUp();
-
-               $this->setUpVfsDir();
-       }
-
-       public function dataHooks(): array
-       {
-               return [
-                       'normal' => [
-                               'content' => <<<EOF
-<?php
-
-return [
-       \Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
-               \Psr\Log\LoggerInterface::class => [
-                       \Psr\Log\NullLogger::class => [''],
-               ],
-       ],
-];
-EOF,
-                               'addonsArray'      => [],
-                               'assertStrategies' => [
-                                       [LoggerInterface::class, NullLogger::class, ''],
-                               ],
-                       ],
-                       'normalWithString' => [
-                               'content' => <<<EOF
-<?php
-
-return [
-       \Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
-               \Psr\Log\LoggerInterface::class => [
-                       \Psr\Log\NullLogger::class => '',
-               ],
-       ],
-];
-EOF,
-                               'addonsArray'      => [],
-                               'assertStrategies' => [
-                                       [LoggerInterface::class, NullLogger::class, ''],
-                               ],
-                       ],
-                       'withAddons' => [
-                               'content' => <<<EOF
-<?php
-
-return [
-       \Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
-               \Psr\Log\LoggerInterface::class => [
-                       \Psr\Log\NullLogger::class => [''],
-               ],
-       ],
-];
-EOF,
-                               'addonsArray' => [
-                                       \Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
-                                               \Psr\Log\LoggerInterface::class => [
-                                                       \Psr\Log\NullLogger::class => ['null'],
-                                               ],
-                                       ],
-                               ],
-                               'assertStrategies' => [
-                                       [LoggerInterface::class, NullLogger::class, ''],
-                                       [LoggerInterface::class, NullLogger::class, 'null'],
-                               ],
-                       ],
-                       'withAddonsWithString' => [
-                               'content' => <<<EOF
-<?php
-
-return [
-       \Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
-               \Psr\Log\LoggerInterface::class => [
-                       \Psr\Log\NullLogger::class => [''],
-               ],
-       ],
-];
-EOF,
-                               'addonsArray' => [
-                                       \Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
-                                               \Psr\Log\LoggerInterface::class => [
-                                                       \Psr\Log\NullLogger::class => 'null',
-                                               ],
-                                       ],
-                               ],
-                               'assertStrategies' => [
-                                       [LoggerInterface::class, NullLogger::class, ''],
-                                       [LoggerInterface::class, NullLogger::class, 'null'],
-                               ],
-                       ],
-                       // This should work because unique name convention is part of the instance manager logic, not of the file-infrastructure layer
-                       'withAddonsDoubleNamed' => [
-                               'content' => <<<EOF
-<?php
-
-return [
-       \Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
-               \Psr\Log\LoggerInterface::class => [
-                       \Psr\Log\NullLogger::class => [''],
-               ],
-       ],
-];
-EOF,
-                               'addonsArray' => [
-                                       \Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
-                                               \Psr\Log\LoggerInterface::class => [
-                                                       \Psr\Log\NullLogger::class => [''],
-                                               ],
-                                       ],
-                               ],
-                               'assertStrategies' => [
-                                       [LoggerInterface::class, NullLogger::class, ''],
-                                       [LoggerInterface::class, NullLogger::class, ''],
-                               ],
-                       ],
-                       'withWrongContentButAddons' => [
-                               'content' => <<<EOF
-<?php
-
-return [
-       'REALLY_WRONG' => [
-               \Psr\Log\LoggerInterface::class => [
-                       \Psr\Log\NullLogger::class => [''],
-               ],
-       ],
-];
-EOF,
-                               'addonsArray' => [
-                                       \Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
-                                               \Psr\Log\LoggerInterface::class => [
-                                                       \Psr\Log\NullLogger::class => [''],
-                                               ],
-                                       ],
-                               ],
-                               'assertStrategies' => [
-                                       [LoggerInterface::class, NullLogger::class, ''],
-                               ],
-                       ],
-               ];
-       }
-
-       /**
-        * @dataProvider dataHooks
-        */
-       public function testSetupHooks(string $content, array $addonsArray, array $assertStrategies)
-       {
-               vfsStream::newFile('static/hooks.config.php')
-                       ->withContent($content)
-                       ->at($this->root);
-
-               $addonLoader = \Mockery::mock(ICanLoadAddons::class);
-               $addonLoader->shouldReceive('getActiveAddonConfig')->andReturn($addonsArray)->once();
-
-               $hookFileManager = new HookFileManager($this->root->url(), $addonLoader);
-
-               $instanceManager = \Mockery::mock(ICanRegisterInstances::class);
-               foreach ($assertStrategies as $assertStrategy) {
-                       $instanceManager->shouldReceive('registerStrategy')->withArgs($assertStrategy)->once();
-               }
-
-               $hookFileManager->setupHooks($instanceManager);
-
-               self::expectNotToPerformAssertions();
-       }
-
-       /**
-        * Test the exception in case the hooks.config.php file is missing
-        */
-       public function testMissingHooksFile()
-       {
-               $addonLoader     = \Mockery::mock(ICanLoadAddons::class);
-               $instanceManager = \Mockery::mock(ICanRegisterInstances::class);
-               $hookFileManager = new HookFileManager($this->root->url(), $addonLoader);
-
-               self::expectException(HookConfigException::class);
-               self::expectExceptionMessage(sprintf('config file %s does not exist.',
-                               $this->root->url() . '/' . HookFileManager::STATIC_DIR . '/' . HookFileManager::CONFIG_NAME . '.config.php'));
-
-               $hookFileManager->setupHooks($instanceManager);
-       }
-
-       /**
-        * Test the exception in case the hooks.config.php file is wrong
-        */
-       public function testWrongHooksFile()
-       {
-               $addonLoader     = \Mockery::mock(ICanLoadAddons::class);
-               $instanceManager = \Mockery::mock(ICanRegisterInstances::class);
-               $hookFileManager = new HookFileManager($this->root->url(), $addonLoader);
-
-               vfsStream::newFile('static/hooks.config.php')
-                                ->withContent("<php return 'WRONG_CONTENT';")
-                                ->at($this->root);
-
-               self::expectException(HookConfigException::class);
-               self::expectExceptionMessage(sprintf('Error loading config file %s.',
-                       $this->root->url() . '/' . HookFileManager::STATIC_DIR . '/' . HookFileManager::CONFIG_NAME . '.config.php'));
-
-               $hookFileManager->setupHooks($instanceManager);
-       }
-}
diff --git a/tests/src/Core/Hooks/Util/StrategiesFileManagerTest.php b/tests/src/Core/Hooks/Util/StrategiesFileManagerTest.php
new file mode 100644 (file)
index 0000000..9e1855d
--- /dev/null
@@ -0,0 +1,202 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Test\src\Core\Hooks\Util;
+
+use Friendica\Core\Addon\Capabilities\ICanLoadAddons;
+use Friendica\Core\Hooks\Capabilities\ICanRegisterStrategies;
+use Friendica\Core\Hooks\Exceptions\HookConfigException;
+use Friendica\Core\Hooks\Util\StrategiesFileManager;
+use Friendica\Test\MockedTest;
+use Friendica\Test\Util\VFSTrait;
+use org\bovigo\vfs\vfsStream;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
+class StrategiesFileManagerTest extends MockedTest
+{
+       use VFSTrait;
+
+       protected function setUp(): void
+       {
+               parent::setUp();
+
+               $this->setUpVfsDir();
+       }
+
+       public function dataHooks(): array
+       {
+               return [
+                       'normal' => [
+                               'content' => <<<EOF
+<?php
+
+return [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => [''],
+               ],
+];
+EOF,
+                               'addonsArray'      => [],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                               ],
+                       ],
+                       'normalWithString' => [
+                               'content' => <<<EOF
+<?php
+
+return [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => '',
+               ],
+];
+EOF,
+                               'addonsArray'      => [],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                               ],
+                       ],
+                       'withAddons' => [
+                               'content' => <<<EOF
+<?php
+
+return [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => [''],
+               ],
+];
+EOF,
+                               'addonsArray' => [
+                                               \Psr\Log\LoggerInterface::class => [
+                                                       \Psr\Log\NullLogger::class => ['null'],
+                                               ],
+                               ],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                                       [LoggerInterface::class, NullLogger::class, 'null'],
+                               ],
+                       ],
+                       'withAddonsWithString' => [
+                               'content' => <<<EOF
+<?php
+
+return [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => [''],
+               ],
+];
+EOF,
+                               'addonsArray' => [
+                                               \Psr\Log\LoggerInterface::class => [
+                                                       \Psr\Log\NullLogger::class => 'null',
+                                               ],
+                               ],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                                       [LoggerInterface::class, NullLogger::class, 'null'],
+                               ],
+                       ],
+                       // This should work because unique name convention is part of the instance manager logic, not of the file-infrastructure layer
+                       'withAddonsDoubleNamed' => [
+                               'content' => <<<EOF
+<?php
+
+return [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => [''],
+               ],
+];
+EOF,
+                               'addonsArray' => [
+                                               \Psr\Log\LoggerInterface::class => [
+                                                       \Psr\Log\NullLogger::class => [''],
+                                               ],
+                               ],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                               ],
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider dataHooks
+        */
+       public function testSetupHooks(string $content, array $addonsArray, array $assertStrategies)
+       {
+               vfsStream::newFile(StrategiesFileManager::STATIC_DIR . '/' . StrategiesFileManager::CONFIG_NAME . '.config.php')
+                       ->withContent($content)
+                       ->at($this->root);
+
+               $addonLoader = \Mockery::mock(ICanLoadAddons::class);
+               $addonLoader->shouldReceive('getActiveAddonConfig')->andReturn($addonsArray)->once();
+
+               $hookFileManager = new StrategiesFileManager($this->root->url(), $addonLoader);
+
+               $instanceManager = \Mockery::mock(ICanRegisterStrategies::class);
+               foreach ($assertStrategies as $assertStrategy) {
+                       $instanceManager->shouldReceive('registerStrategy')->withArgs($assertStrategy)->once();
+               }
+
+               $hookFileManager->loadConfig();
+               $hookFileManager->setupStrategies($instanceManager);
+
+               self::expectNotToPerformAssertions();
+       }
+
+       /**
+        * Test the exception in case the strategies.config.php file is missing
+        */
+       public function testMissingStrategiesFile()
+       {
+               $addonLoader     = \Mockery::mock(ICanLoadAddons::class);
+               $instanceManager = \Mockery::mock(ICanRegisterStrategies::class);
+               $hookFileManager = new StrategiesFileManager($this->root->url(), $addonLoader);
+
+               self::expectException(HookConfigException::class);
+               self::expectExceptionMessage(sprintf('config file %s does not exist.',
+                               $this->root->url() . '/' . StrategiesFileManager::STATIC_DIR . '/' . StrategiesFileManager::CONFIG_NAME . '.config.php'));
+
+               $hookFileManager->loadConfig();
+       }
+
+       /**
+        * Test the exception in case the strategies.config.php file is wrong
+        */
+       public function testWrongStrategiesFile()
+       {
+               $addonLoader     = \Mockery::mock(ICanLoadAddons::class);
+               $instanceManager = \Mockery::mock(ICanRegisterStrategies::class);
+               $hookFileManager = new StrategiesFileManager($this->root->url(), $addonLoader);
+
+               vfsStream::newFile(StrategiesFileManager::STATIC_DIR . '/' . StrategiesFileManager::CONFIG_NAME . '.config.php')
+                                ->withContent("<php return 'WRONG_CONTENT';")
+                                ->at($this->root);
+
+               self::expectException(HookConfigException::class);
+               self::expectExceptionMessage(sprintf('Error loading config file %s.',
+                       $this->root->url() . '/' . StrategiesFileManager::STATIC_DIR . '/' . StrategiesFileManager::CONFIG_NAME . '.config.php'));
+
+               $hookFileManager->loadConfig();
+       }
+}