2 namespace Shaarli\Plugin
;
4 use Shaarli\Config\ConfigManager
;
7 * Unit tests for Plugins
9 class PluginManagerTest
extends \PHPUnit\Framework\TestCase
12 * Path to tests plugin.
13 * @var string $pluginPath
15 private static $pluginPath = 'tests/plugins';
19 * @var string $pluginName
21 private static $pluginName = 'test';
24 * @var PluginManager $pluginManager Plugin Mananger instance.
26 protected $pluginManager;
28 public function setUp(): void
30 $conf = new ConfigManager('');
31 $this->pluginManager
= new PluginManager($conf);
35 * Test plugin loading and hook execution.
37 public function testPlugin(): void
39 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
40 $this->pluginManager
->load(array(self
::$pluginName));
42 $this->assertTrue(function_exists('hook_test_random'));
44 $data = array(0 => 'woot');
45 $this->pluginManager
->executeHooks('random', $data);
46 $this->assertEquals('woot', $data[1]);
48 $data = array(0 => 'woot');
49 $this->pluginManager
->executeHooks('random', $data, array('target' => 'test'));
50 $this->assertEquals('page test', $data[1]);
52 $data = array(0 => 'woot');
53 $this->pluginManager
->executeHooks('random', $data, array('loggedin' => true));
54 $this->assertEquals('loggedin', $data[1]);
58 * Test plugin loading and hook execution with an error: raise an incompatibility error.
60 public function testPluginWithPhpError(): void
62 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
63 $this->pluginManager
->load(array(self
::$pluginName));
65 $this->assertTrue(function_exists('hook_test_error'));
68 $this->pluginManager
->executeHooks('error', $data);
71 'test [plugin incompatibility]: Class \'Unknown\' not found',
72 $this->pluginManager
->getErrors()[0]
77 * Test missing plugin loading.
79 public function testPluginNotFound(): void
81 $this->pluginManager
->load(array());
82 $this->pluginManager
->load(array('nope', 'renope'));
83 $this->addToAssertionCount(1);
87 * Test plugin metadata loading.
89 public function testGetPluginsMeta(): void
91 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
92 $this->pluginManager
->load(array(self
::$pluginName));
94 $expectedParameters = array(
97 'desc' => 'pop description',
104 $meta = $this->pluginManager
->getPluginsMeta();
105 $this->assertEquals('test plugin', $meta[self
::$pluginName]['description']);
106 $this->assertEquals($expectedParameters, $meta[self
::$pluginName]['parameters']);