From 527622df4ace40cb52fb48f03fe16881f9e9962e Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 16 Jul 2023 22:41:56 +0200 Subject: [PATCH] Add tests for HookFileManager --- src/Core/Hooks/Util/HookFileManager.php | 4 +- .../Core/Hooks/Util/HookFileManagerTest.php | 243 ++++++++++++++++++ 2 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 tests/src/Core/Hooks/Util/HookFileManagerTest.php diff --git a/src/Core/Hooks/Util/HookFileManager.php b/src/Core/Hooks/Util/HookFileManager.php index eda7e93884..e25641e367 100644 --- a/src/Core/Hooks/Util/HookFileManager.php +++ b/src/Core/Hooks/Util/HookFileManager.php @@ -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 index 0000000000..cbd9a63b1a --- /dev/null +++ b/tests/src/Core/Hooks/Util/HookFileManagerTest.php @@ -0,0 +1,243 @@ +setUpVfsDir(); + } + + public function dataHooks(): array + { + return [ + 'normal' => [ + 'content' => << [ + \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' => << [ + \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' => << [ + \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' => << [ + \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' => << [ + \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' => << [ + \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("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); + } +} -- 2.39.5