3 namespace Shaarli\Plugin
;
5 use Shaarli\Config\ConfigManager
;
8 * Unit tests for Plugins
10 class PluginManagerTest
extends \Shaarli\TestCase
13 * Path to tests plugin.
14 * @var string $pluginPath
16 private static $pluginPath = 'tests/plugins';
20 * @var string $pluginName
22 private static $pluginName = 'test';
25 * @var PluginManager $pluginManager Plugin Mananger instance.
27 protected $pluginManager;
29 public function setUp(): void
31 $conf = new ConfigManager('');
32 $this->pluginManager
= new PluginManager($conf);
36 * Test plugin loading and hook execution.
38 public function testPlugin(): void
40 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
41 $this->pluginManager
->load(array(self
::$pluginName));
43 $this->assertTrue(function_exists('hook_test_random'));
45 $data = [0 => 'woot'];
46 $this->pluginManager
->executeHooks('random', $data);
48 static::assertCount(2, $data);
49 static::assertSame('woot', $data[1]);
51 $data = [0 => 'woot'];
52 $this->pluginManager
->executeHooks('random', $data, array('target' => 'test'));
54 static::assertCount(2, $data);
55 static::assertSame('page test', $data[1]);
57 $data = [0 => 'woot'];
58 $this->pluginManager
->executeHooks('random', $data, array('loggedin' => true));
60 static::assertCount(2, $data);
61 static::assertEquals('loggedin', $data[1]);
63 $data = [0 => 'woot'];
64 $this->pluginManager
->executeHooks('random', $data, array('loggedin' => null));
66 static::assertCount(3, $data);
67 static::assertEquals('loggedin', $data[1]);
68 static::assertArrayHasKey(2, $data);
69 static::assertNull($data[2]);
73 * Test plugin loading and hook execution with an error: raise an incompatibility error.
75 public function testPluginWithPhpError(): void
77 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
78 $this->pluginManager
->load(array(self
::$pluginName));
80 $this->assertTrue(function_exists('hook_test_error'));
83 $this->pluginManager
->executeHooks('error', $data);
86 '/test \[plugin incompatibility\]: Class [\'"]Unknown[\'"] not found/',
87 $this->pluginManager
->getErrors()[0]
92 * Test missing plugin loading.
94 public function testPluginNotFound(): void
96 $this->pluginManager
->load([]);
97 $this->pluginManager
->load(['nope', 'renope']);
98 $this->addToAssertionCount(1);
102 * Test plugin metadata loading.
104 public function testGetPluginsMeta(): void
106 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
107 $this->pluginManager
->load([self
::$pluginName]);
109 $expectedParameters = [
112 'desc' => 'pop description',
119 $meta = $this->pluginManager
->getPluginsMeta();
120 $this->assertEquals('test plugin', $meta[self
::$pluginName]['description']);
121 $this->assertEquals($expectedParameters, $meta[self
::$pluginName]['parameters']);