Skip to content

Commit 974e882

Browse files
committed
removed EventDispatcher dependency in favour of Piwik::postEvent
1 parent 18a9c55 commit 974e882

2 files changed

Lines changed: 29 additions & 32 deletions

File tree

Generation/PluginListProvider.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Piwik\Plugins\OpenApiDocs\Generation;
1313

14-
use Piwik\EventDispatcher;
14+
use Piwik\Piwik;
1515
use Piwik\Plugin\Manager;
1616
use Piwik\Plugins\OpenApiDocs\OpenApiDocs;
1717

@@ -22,15 +22,9 @@ class PluginListProvider
2222
*/
2323
private $pluginManager;
2424

25-
/**
26-
* @var EventDispatcher
27-
*/
28-
private $eventDispatcher;
29-
30-
public function __construct(?Manager $pluginManager = null, ?EventDispatcher $eventDispatcher = null)
25+
public function __construct(?Manager $pluginManager = null)
3126
{
3227
$this->pluginManager = $pluginManager ?? Manager::getInstance();
33-
$this->eventDispatcher = $eventDispatcher ?? EventDispatcher::getInstance();
3428
}
3529

3630
/**
@@ -96,6 +90,14 @@ protected function pluginHasApiFile(string $pluginName): bool
9690
*/
9791
private function dispatchUpdatePluginListEvent(array &$pluginNames): void
9892
{
99-
$this->eventDispatcher->postEvent('OpenApiDocs.updatePluginList', [&$pluginNames]);
93+
$this->postEvent('OpenApiDocs.updatePluginList', [&$pluginNames]);
94+
}
95+
96+
/**
97+
* @param array<int, mixed> $params
98+
*/
99+
protected function postEvent(string $eventName, array $params): void
100+
{
101+
Piwik::postEvent($eventName, $params);
100102
}
101103
}

tests/Unit/Generation/PluginListProviderTest.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
require_once PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/vendor/autoload.php';
1515

1616
use PHPUnit\Framework\TestCase;
17-
use Piwik\EventDispatcher;
1817
use Piwik\Plugin\Manager;
1918
use Piwik\Plugins\OpenApiDocs\Generation\PluginListProvider;
2019

@@ -92,10 +91,9 @@ public function testGetPluginsForSpecGenerationAppliesEventUpdates(): void
9291
['HasApi' => true, 'Login' => true],
9392
['HasApi' => true, 'Login' => true],
9493
['HasApi' => true, 'Login' => true],
95-
static function (EventDispatcher $eventDispatcher): void {
96-
$eventDispatcher->addObserver('OpenApiDocs.updatePluginList', function (&$pluginNames): void {
97-
$pluginNames[] = 'Login';
98-
});
94+
static function (string $eventName, array $params): void {
95+
$pluginNames = &$params[0];
96+
$pluginNames[] = 'Login';
9997
}
10098
);
10199

@@ -134,14 +132,13 @@ public function testGetPluginsForSpecGenerationDropsInvalidEventUpdatesButKeepsU
134132
'NoApi' => false,
135133
'ConnectAccounts' => true,
136134
],
137-
static function (EventDispatcher $eventDispatcher): void {
138-
$eventDispatcher->addObserver('OpenApiDocs.updatePluginList', function (&$pluginNames): void {
139-
$pluginNames[] = 'Login';
140-
$pluginNames[] = 'InactivePlugin';
141-
$pluginNames[] = 'NoApi';
142-
$pluginNames[] = 'ConnectAccounts';
143-
$pluginNames[] = 123;
144-
});
135+
static function (string $eventName, array $params): void {
136+
$pluginNames = &$params[0];
137+
$pluginNames[] = 'Login';
138+
$pluginNames[] = 'InactivePlugin';
139+
$pluginNames[] = 'NoApi';
140+
$pluginNames[] = 'ConnectAccounts';
141+
$pluginNames[] = 123;
145142
}
146143
);
147144

@@ -159,7 +156,7 @@ private function makeProvider(
159156
array $activatedByPlugin,
160157
array $inFilesystemByPlugin,
161158
$hasApiFile,
162-
?callable $configureEventDispatcher = null
159+
?callable $postEventCallback = null
163160
): PluginListProvider {
164161
$pluginManager = $this->createConfiguredMock(Manager::class, [
165162
'getInstalledPluginsName' => $installedPlugins,
@@ -176,16 +173,9 @@ private function makeProvider(
176173
return $inFilesystemByPlugin[$pluginName] ?? false;
177174
});
178175

179-
if ($configureEventDispatcher) {
180-
$eventDispatcher = new EventDispatcher($pluginManager, []);
181-
$configureEventDispatcher($eventDispatcher);
182-
} else {
183-
$eventDispatcher = $this->createMock(EventDispatcher::class);
184-
}
185-
186176
$provider = $this->getMockBuilder(PluginListProvider::class)
187-
->setConstructorArgs([$pluginManager, $eventDispatcher])
188-
->onlyMethods(['pluginHasApiFile'])
177+
->setConstructorArgs([$pluginManager])
178+
->onlyMethods(['pluginHasApiFile', 'postEvent'])
189179
->getMock();
190180

191181
$provider->method('pluginHasApiFile')
@@ -197,6 +187,11 @@ private function makeProvider(
197187
return $hasApiFile;
198188
});
199189

190+
if ($postEventCallback) {
191+
$provider->method('postEvent')
192+
->willReturnCallback($postEventCallback);
193+
}
194+
200195
return $provider;
201196
}
202197
}

0 commit comments

Comments
 (0)