diff options
Diffstat (limited to 'tests/PluginManagerTest.php')
-rw-r--r-- | tests/PluginManagerTest.php | 79 |
1 files changed, 55 insertions, 24 deletions
diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php index 71761ac1..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,58 +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 | * | ||
62 | * @return void | ||
63 | */ | 93 | */ |
64 | public function testPluginNotFound() | 94 | public function testPluginNotFound(): void |
65 | { | 95 | { |
66 | $this->pluginManager->load(array()); | 96 | $this->pluginManager->load([]); |
67 | $this->pluginManager->load(array('nope', 'renope')); | 97 | $this->pluginManager->load(['nope', 'renope']); |
98 | $this->addToAssertionCount(1); | ||
68 | } | 99 | } |
69 | 100 | ||
70 | /** | 101 | /** |
71 | * Test plugin metadata loading. | 102 | * Test plugin metadata loading. |
72 | */ | 103 | */ |
73 | public function testGetPluginsMeta() | 104 | public function testGetPluginsMeta(): void |
74 | { | 105 | { |
75 | PluginManager::$PLUGINS_PATH = self::$pluginPath; | 106 | PluginManager::$PLUGINS_PATH = self::$pluginPath; |
76 | $this->pluginManager->load(array(self::$pluginName)); | 107 | $this->pluginManager->load([self::$pluginName]); |
77 | 108 | ||
78 | $expectedParameters = array( | 109 | $expectedParameters = [ |
79 | 'pop' => array( | 110 | 'pop' => [ |
80 | 'value' => '', | 111 | 'value' => '', |
81 | 'desc' => 'pop description', | 112 | 'desc' => 'pop description', |
82 | ), | 113 | ], |
83 | 'hip' => array( | 114 | 'hip' => [ |
84 | 'value' => '', | 115 | 'value' => '', |
85 | 'desc' => '', | 116 | 'desc' => '', |
86 | ), | 117 | ], |
87 | ); | 118 | ]; |
88 | $meta = $this->pluginManager->getPluginsMeta(); | 119 | $meta = $this->pluginManager->getPluginsMeta(); |
89 | $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); | 120 | $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); |
90 | $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); | 121 | $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); |