]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/PluginManagerTest.php
Run Unit Tests against PHP 7.4 (#1353)
[github/shaarli/Shaarli.git] / tests / PluginManagerTest.php
1 <?php
2 namespace Shaarli\Plugin;
3
4 use Shaarli\Config\ConfigManager;
5
6 /**
7 * Unit tests for Plugins
8 */
9 class PluginManagerTest extends \PHPUnit\Framework\TestCase
10 {
11 /**
12 * Path to tests plugin.
13 * @var string $pluginPath
14 */
15 private static $pluginPath = 'tests/plugins';
16
17 /**
18 * Test plugin.
19 * @var string $pluginName
20 */
21 private static $pluginName = 'test';
22
23 /**
24 * @var PluginManager $pluginManager Plugin Mananger instance.
25 */
26 protected $pluginManager;
27
28 public function setUp()
29 {
30 $conf = new ConfigManager('');
31 $this->pluginManager = new PluginManager($conf);
32 }
33
34 /**
35 * Test plugin loading and hook execution.
36 *
37 * @return void
38 */
39 public function testPlugin()
40 {
41 PluginManager::$PLUGINS_PATH = self::$pluginPath;
42 $this->pluginManager->load(array(self::$pluginName));
43
44 $this->assertTrue(function_exists('hook_test_random'));
45
46 $data = array(0 => 'woot');
47 $this->pluginManager->executeHooks('random', $data);
48 $this->assertEquals('woot', $data[1]);
49
50 $data = array(0 => 'woot');
51 $this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
52 $this->assertEquals('page test', $data[1]);
53
54 $data = array(0 => 'woot');
55 $this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
56 $this->assertEquals('loggedin', $data[1]);
57 }
58
59 /**
60 * Test missing plugin loading.
61 */
62 public function testPluginNotFound()
63 {
64 $this->pluginManager->load(array());
65 $this->pluginManager->load(array('nope', 'renope'));
66 $this->addToAssertionCount(1);
67 }
68
69 /**
70 * Test plugin metadata loading.
71 */
72 public function testGetPluginsMeta()
73 {
74 PluginManager::$PLUGINS_PATH = self::$pluginPath;
75 $this->pluginManager->load(array(self::$pluginName));
76
77 $expectedParameters = array(
78 'pop' => array(
79 'value' => '',
80 'desc' => 'pop description',
81 ),
82 'hip' => array(
83 'value' => '',
84 'desc' => '',
85 ),
86 );
87 $meta = $this->pluginManager->getPluginsMeta();
88 $this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
89 $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
90 }
91 }