]> git.mxchange.org Git - friendica.git/commitdiff
Add tests for HookFileManager
authorPhilipp <admin@philipp.info>
Sun, 16 Jul 2023 20:41:56 +0000 (22:41 +0200)
committerPhilipp <admin@philipp.info>
Sun, 16 Jul 2023 20:41:56 +0000 (22:41 +0200)
src/Core/Hooks/Util/HookFileManager.php
tests/src/Core/Hooks/Util/HookFileManagerTest.php [new file with mode: 0644]

index eda7e93884c52bb5c3adc4d5bff1d29e4f7a1b6b..e25641e367a33a5d9651461ba27f717150dcf845 100644 (file)
@@ -104,13 +104,13 @@ class HookFileManager
                $configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
 
                if (!file_exists($configFile)) {
-                       throw new HookConfigException(sprintf('config file %s does not exit.', $configFile));
+                       throw new HookConfigException(sprintf('config file %s does not exist.', $configFile));
                }
 
                $config = include $configFile;
 
                if (!is_array($config)) {
-                       throw new HookConfigException('Error loading config file ' . $configFile);
+                       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/tests/src/Core/Hooks/Util/HookFileManagerTest.php b/tests/src/Core/Hooks/Util/HookFileManagerTest.php
new file mode 100644 (file)
index 0000000..cbd9a63
--- /dev/null
@@ -0,0 +1,243 @@
+<?php
+
+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\HookType::STRATEGY => [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => [''],
+               ],
+       ],
+       \Friendica\Core\Hooks\Capabilities\HookType::DECORATOR => [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class,
+               ],
+       ],
+];
+EOF,
+                               'addonsArray' => [],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                               ],
+                               'assertDecorators' => [
+                                       [LoggerInterface::class, NullLogger::class],
+                               ],
+                       ],
+                       'normalWithString' => [
+                               'content' => <<<EOF
+<?php
+
+return [
+       \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => '',
+               ],
+       ],
+       \Friendica\Core\Hooks\Capabilities\HookType::DECORATOR => [
+               \Psr\Log\LoggerInterface::class => \Psr\Log\NullLogger::class,
+       ],
+];
+EOF,
+                               'addonsArray' => [],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                               ],
+                               'assertDecorators' => [
+                                       [LoggerInterface::class, NullLogger::class],
+                               ],
+                       ],
+                       'withAddons' => [
+                               'content' => <<<EOF
+<?php
+
+return [
+       \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => [''],
+               ],
+       ],
+];
+EOF,
+                               'addonsArray' => [
+                                       \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
+                                               \Psr\Log\LoggerInterface::class => [
+                                                       \Psr\Log\NullLogger::class => ['null'],
+                                               ],
+                                       ],
+                               ],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                                       [LoggerInterface::class, NullLogger::class, 'null'],
+                               ],
+                               'assertDecorators' => [],
+                       ],
+                       'withAddonsWithString' => [
+                               'content' => <<<EOF
+<?php
+
+return [
+       \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => [''],
+               ],
+       ],
+];
+EOF,
+                               'addonsArray' => [
+                                       \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
+                                               \Psr\Log\LoggerInterface::class => [
+                                                       \Psr\Log\NullLogger::class => 'null',
+                                               ],
+                                       ],
+                               ],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                                       [LoggerInterface::class, NullLogger::class, 'null'],
+                               ],
+                               'assertDecorators' => [],
+                       ],
+                       // 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\HookType::STRATEGY => [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => [''],
+               ],
+       ],
+];
+EOF,
+                               'addonsArray' => [
+                                       \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
+                                               \Psr\Log\LoggerInterface::class => [
+                                                       \Psr\Log\NullLogger::class => [''],
+                                               ],
+                                       ],
+                               ],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                               ],
+                               'assertDecorators' => [],
+                       ],
+                       'withWrongContentButAddons' => [
+                               'content' => <<<EOF
+<?php
+
+return [
+       'REALLY_WRONG' => [
+               \Psr\Log\LoggerInterface::class => [
+                       \Psr\Log\NullLogger::class => [''],
+               ],
+       ],
+];
+EOF,
+                               'addonsArray' => [
+                                       \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
+                                               \Psr\Log\LoggerInterface::class => [
+                                                       \Psr\Log\NullLogger::class => [''],
+                                               ],
+                                       ],
+                               ],
+                               'assertStrategies' => [
+                                       [LoggerInterface::class, NullLogger::class, ''],
+                               ],
+                               'assertDecorators' => [],
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider dataHooks
+        */
+       public function testSetupHooks(string $content, array $addonsArray, array $assertStrategies, array $assertDecorators)
+       {
+               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();
+               }
+
+               foreach ($assertDecorators as $assertDecorator) {
+                       $instanceManager->shouldReceive('registerDecorator')->withArgs($assertDecorator)->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);
+       }
+}