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 = [0 => 'woot'];
45 $this->pluginManager
->executeHooks('random', $data);
47 static::assertCount(2, $data);
48 static::assertSame('woot', $data[1]);
50 $data = [0 => 'woot'];
51 $this->pluginManager
->executeHooks('random', $data, array('target' => 'test'));
53 static::assertCount(2, $data);
54 static::assertSame('page test', $data[1]);
56 $data = [0 => 'woot'];
57 $this->pluginManager
->executeHooks('random', $data, array('loggedin' => true));
59 static::assertCount(2, $data);
60 static::assertEquals('loggedin', $data[1]);
62 $data = [0 => 'woot'];
63 $this->pluginManager
->executeHooks('random', $data, array('loggedin' => null));
65 static::assertCount(3, $data);
66 static::assertEquals('loggedin', $data[1]);
67 static::assertArrayHasKey(2, $data);
68 static::assertNull($data[2]);
72 * Test plugin loading and hook execution with an error: raise an incompatibility error.
74 public function testPluginWithPhpError(): void
76 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
77 $this->pluginManager
->load(array(self
::$pluginName));
79 $this->assertTrue(function_exists('hook_test_error'));
82 $this->pluginManager
->executeHooks('error', $data);
85 'test [plugin incompatibility]: Class \'Unknown\' not found',
86 $this->pluginManager
->getErrors()[0]
91 * Test missing plugin loading.
93 public function testPluginNotFound(): void
95 $this->pluginManager
->load([]);
96 $this->pluginManager
->load(['nope', 'renope']);
97 $this->addToAssertionCount(1);
101 * Test plugin metadata loading.
103 public function testGetPluginsMeta(): void
105 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
106 $this->pluginManager
->load([self
::$pluginName]);
108 $expectedParameters = [
111 'desc' => 'pop description',
118 $meta = $this->pluginManager
->getPluginsMeta();
119 $this->assertEquals('test plugin', $meta[self
::$pluginName]['description']);
120 $this->assertEquals($expectedParameters, $meta[self
::$pluginName]['parameters']);