diff options
Diffstat (limited to 'tests/PluginManagerTest.php')
-rw-r--r-- | tests/PluginManagerTest.php | 76 |
1 files changed, 54 insertions, 22 deletions
diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php index 195d959c..efef5e87 100644 --- a/tests/PluginManagerTest.php +++ b/tests/PluginManagerTest.php | |||
@@ -1,4 +1,5 @@ | |||
1 | <?php | 1 | <?php |
2 | |||
2 | namespace Shaarli\Plugin; | 3 | namespace Shaarli\Plugin; |
3 | 4 | ||
4 | use Shaarli\Config\ConfigManager; | 5 | use Shaarli\Config\ConfigManager; |
@@ -6,7 +7,7 @@ use Shaarli\Config\ConfigManager; | |||
6 | /** | 7 | /** |
7 | * Unit tests for Plugins | 8 | * Unit tests for Plugins |
8 | */ | 9 | */ |
9 | class PluginManagerTest extends \PHPUnit\Framework\TestCase | 10 | class PluginManagerTest extends \Shaarli\TestCase |
10 | { | 11 | { |
11 | /** | 12 | /** |
12 | * Path to tests plugin. | 13 | * Path to tests plugin. |
@@ -25,7 +26,7 @@ class PluginManagerTest extends \PHPUnit\Framework\TestCase | |||
25 | */ | 26 | */ |
26 | protected $pluginManager; | 27 | protected $pluginManager; |
27 | 28 | ||
28 | public function setUp() | 29 | public function setUp(): void |
29 | { | 30 | { |
30 | $conf = new ConfigManager(''); | 31 | $conf = new ConfigManager(''); |
31 | $this->pluginManager = new PluginManager($conf); | 32 | $this->pluginManager = new PluginManager($conf); |
@@ -33,57 +34,88 @@ class PluginManagerTest extends \PHPUnit\Framework\TestCase | |||
33 | 34 | ||
34 | /** | 35 | /** |
35 | * Test plugin loading and hook execution. | 36 | * Test plugin loading and hook execution. |
36 | * | ||
37 | * @return void | ||
38 | */ | 37 | */ |
39 | public function testPlugin() | 38 | public function testPlugin(): void |
40 | { | 39 | { |
41 | PluginManager::$PLUGINS_PATH = self::$pluginPath; | 40 | PluginManager::$PLUGINS_PATH = self::$pluginPath; |
42 | $this->pluginManager->load(array(self::$pluginName)); | 41 | $this->pluginManager->load(array(self::$pluginName)); |
43 | 42 | ||
44 | $this->assertTrue(function_exists('hook_test_random')); | 43 | $this->assertTrue(function_exists('hook_test_random')); |
45 | 44 | ||
46 | $data = array(0 => 'woot'); | 45 | $data = [0 => 'woot']; |
47 | $this->pluginManager->executeHooks('random', $data); | 46 | $this->pluginManager->executeHooks('random', $data); |
48 | $this->assertEquals('woot', $data[1]); | ||
49 | 47 | ||
50 | $data = array(0 => 'woot'); | 48 | static::assertCount(2, $data); |
49 | static::assertSame('woot', $data[1]); | ||
50 | |||
51 | $data = [0 => 'woot']; | ||
51 | $this->pluginManager->executeHooks('random', $data, array('target' => 'test')); | 52 | $this->pluginManager->executeHooks('random', $data, array('target' => 'test')); |
52 | $this->assertEquals('page test', $data[1]); | ||
53 | 53 | ||
54 | $data = array(0 => 'woot'); | 54 | static::assertCount(2, $data); |
55 | static::assertSame('page test', $data[1]); | ||
56 | |||
57 | $data = [0 => 'woot']; | ||
55 | $this->pluginManager->executeHooks('random', $data, array('loggedin' => true)); | 58 | $this->pluginManager->executeHooks('random', $data, array('loggedin' => true)); |
56 | $this->assertEquals('loggedin', $data[1]); | 59 | |
60 | static::assertCount(2, $data); | ||
61 | static::assertEquals('loggedin', $data[1]); | ||
62 | |||
63 | $data = [0 => 'woot']; | ||
64 | $this->pluginManager->executeHooks('random', $data, array('loggedin' => null)); | ||
65 | |||
66 | static::assertCount(3, $data); | ||
67 | static::assertEquals('loggedin', $data[1]); | ||
68 | static::assertArrayHasKey(2, $data); | ||
69 | static::assertNull($data[2]); | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * Test plugin loading and hook execution with an error: raise an incompatibility error. | ||
74 | */ | ||
75 | public function testPluginWithPhpError(): void | ||
76 | { | ||
77 | PluginManager::$PLUGINS_PATH = self::$pluginPath; | ||
78 | $this->pluginManager->load(array(self::$pluginName)); | ||
79 | |||
80 | $this->assertTrue(function_exists('hook_test_error')); | ||
81 | |||
82 | $data = []; | ||
83 | $this->pluginManager->executeHooks('error', $data); | ||
84 | |||
85 | $this->assertRegExp( | ||
86 | '/test \[plugin incompatibility\]: Class [\'"]Unknown[\'"] not found/', | ||
87 | $this->pluginManager->getErrors()[0] | ||
88 | ); | ||
57 | } | 89 | } |
58 | 90 | ||
59 | /** | 91 | /** |
60 | * Test missing plugin loading. | 92 | * Test missing plugin loading. |
61 | */ | 93 | */ |
62 | public function testPluginNotFound() | 94 | public function testPluginNotFound(): void |
63 | { | 95 | { |
64 | $this->pluginManager->load(array()); | 96 | $this->pluginManager->load([]); |
65 | $this->pluginManager->load(array('nope', 'renope')); | 97 | $this->pluginManager->load(['nope', 'renope']); |
66 | $this->addToAssertionCount(1); | 98 | $this->addToAssertionCount(1); |
67 | } | 99 | } |
68 | 100 | ||
69 | /** | 101 | /** |
70 | * Test plugin metadata loading. | 102 | * Test plugin metadata loading. |
71 | */ | 103 | */ |
72 | public function testGetPluginsMeta() | 104 | public function testGetPluginsMeta(): void |
73 | { | 105 | { |
74 | PluginManager::$PLUGINS_PATH = self::$pluginPath; | 106 | PluginManager::$PLUGINS_PATH = self::$pluginPath; |
75 | $this->pluginManager->load(array(self::$pluginName)); | 107 | $this->pluginManager->load([self::$pluginName]); |
76 | 108 | ||
77 | $expectedParameters = array( | 109 | $expectedParameters = [ |
78 | 'pop' => array( | 110 | 'pop' => [ |
79 | 'value' => '', | 111 | 'value' => '', |
80 | 'desc' => 'pop description', | 112 | 'desc' => 'pop description', |
81 | ), | 113 | ], |
82 | 'hip' => array( | 114 | 'hip' => [ |
83 | 'value' => '', | 115 | 'value' => '', |
84 | 'desc' => '', | 116 | 'desc' => '', |
85 | ), | 117 | ], |
86 | ); | 118 | ]; |
87 | $meta = $this->pluginManager->getPluginsMeta(); | 119 | $meta = $this->pluginManager->getPluginsMeta(); |
88 | $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); | 120 | $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); |
89 | $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); | 121 | $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); |