From 234a7376a7fd7fcb2561658c1953f22a5a25c6b4 Mon Sep 17 00:00:00 2001 From: Enea Date: Wed, 27 Sep 2023 13:01:17 +0200 Subject: [PATCH] split behavior in GlobalDispatcher and add some integration tests --- src/Dispatcher.php | 6 +++--- src/GlobalState.php | 4 ++-- ...cyEventDispatcherMethodsDeprecatedTrait.php | 10 ++++++++++ src/NullState.php | 4 ++-- src/StateInterface.php | 4 ++-- tests/integration/GlobalStateTest.php | 6 ++++++ tests/src/GlobalStateTestTrait.php | 18 +++++++++--------- 7 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/Dispatcher.php b/src/Dispatcher.php index aab32b1..2cfd9d0 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -28,8 +28,8 @@ public function __construct( public function dispatch(object $event): object { - $this->state->forEvent($event); - $this->state->progress(StateInterface::BEFORE); + $this->state->forEvent($event, $this); + $this->state->progress(StateInterface::BEFORE, $this); /** @var callable $listener */ foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) { @@ -40,7 +40,7 @@ public function dispatch(object $event): object $listener($event); } - $this->state->progress(StateInterface::AFTER); + $this->state->progress(StateInterface::AFTER, $this); return $event; } diff --git a/src/GlobalState.php b/src/GlobalState.php index 5c03479..032e0d7 100644 --- a/src/GlobalState.php +++ b/src/GlobalState.php @@ -11,14 +11,14 @@ final class GlobalState implements StateInterface { private string $eventName = ''; - public function forEvent(object $event): void + public function forEvent(object $event, \Psr\EventDispatcher\EventDispatcherInterface $provider): void { $this->eventName = \get_class($event); global $wp_current_filter; $wp_current_filter[] = $this->eventName; } - public function progress(string $state): void + public function progress(string $state, \Psr\EventDispatcher\EventDispatcherInterface $provider): void { switch ($state) { case self::BEFORE: diff --git a/src/LegacyEventDispatcherMethodsDeprecatedTrait.php b/src/LegacyEventDispatcherMethodsDeprecatedTrait.php index 5d1b0f5..e241c5f 100644 --- a/src/LegacyEventDispatcherMethodsDeprecatedTrait.php +++ b/src/LegacyEventDispatcherMethodsDeprecatedTrait.php @@ -45,6 +45,11 @@ public function removeListener( return remove_filter($eventName, $listener, $priority); } + /** + * @param string $eventName + * @param int|false $priority + * @return bool + */ public function removeAllListener(string $eventName, $priority = false): bool { @@ -57,6 +62,11 @@ public function removeAllListener(string $eventName, $priority = false): bool return remove_all_filters($eventName, $priority); } + /** + * @param string $eventName + * @param array|callable|false|string $callback + * @return bool|int + */ public function hasListener(string $eventName, $callback = false) { diff --git a/src/NullState.php b/src/NullState.php index 01e5a4e..1644f1e 100644 --- a/src/NullState.php +++ b/src/NullState.php @@ -6,11 +6,11 @@ final class NullState implements StateInterface { - public function forEvent(object $event): void + public function forEvent(object $event, \Psr\EventDispatcher\EventDispatcherInterface $provider): void { } - public function progress(string $state): void + public function progress(string $state, \Psr\EventDispatcher\EventDispatcherInterface $provider): void { } diff --git a/src/StateInterface.php b/src/StateInterface.php index ef444cc..31a211c 100644 --- a/src/StateInterface.php +++ b/src/StateInterface.php @@ -12,9 +12,9 @@ interface StateInterface public const BEFORE = 'before'; public const AFTER = 'after'; - public function forEvent(object $event): void; + public function forEvent(object $event, \Psr\EventDispatcher\EventDispatcherInterface $provider): void; - public function progress(string $state): void; + public function progress(string $state, \Psr\EventDispatcher\EventDispatcherInterface $provider): void; public function currentEventName(): string; diff --git a/tests/integration/GlobalStateTest.php b/tests/integration/GlobalStateTest.php index fef7106..7348118 100644 --- a/tests/integration/GlobalStateTest.php +++ b/tests/integration/GlobalStateTest.php @@ -8,11 +8,17 @@ use ItalyStrap\Tests\GlobalStateTestTrait; use ItalyStrap\Tests\IntegrationTestCase; use PHPUnit\Framework\Assert; +use Psr\EventDispatcher\EventDispatcherInterface; class GlobalStateTest extends IntegrationTestCase { use GlobalStateTestTrait; + public function makeDispatcher(): EventDispatcherInterface + { + return $this->createMock(EventDispatcherInterface::class); + } + public function makeInstance(): GlobalState { return new GlobalState(); diff --git a/tests/src/GlobalStateTestTrait.php b/tests/src/GlobalStateTestTrait.php index f713398..f8de69e 100644 --- a/tests/src/GlobalStateTestTrait.php +++ b/tests/src/GlobalStateTestTrait.php @@ -33,8 +33,8 @@ public function testGlobalStateWithCallingForEvent() { $sut = $this->makeInstance(); - $sut->forEvent(new \stdClass()); - $sut->progress(StateInterface::BEFORE); + $sut->forEvent(new \stdClass(), $this->makeDispatcher()); + $sut->progress(StateInterface::BEFORE, $this->makeDispatcher()); Assert::assertSame( 1, @@ -58,11 +58,11 @@ public function testGlobalStateCount() { $sut = $this->makeInstance(); - $sut->forEvent(new \stdClass()); - $sut->progress(StateInterface::BEFORE); + $sut->forEvent(new \stdClass(), $this->makeDispatcher()); + $sut->progress(StateInterface::BEFORE, $this->makeDispatcher()); - $sut->forEvent(new \stdClass()); - $sut->progress(StateInterface::BEFORE); + $sut->forEvent(new \stdClass(), $this->makeDispatcher()); + $sut->progress(StateInterface::BEFORE, $this->makeDispatcher()); Assert::assertSame( 2, @@ -75,8 +75,8 @@ public function testGlobalStateWithCallingForEventAndProgressbeforeAfter() { $sut = $this->makeInstance(); - $sut->forEvent(new \stdClass()); - $sut->progress(StateInterface::BEFORE); + $sut->forEvent(new \stdClass(), $this->makeDispatcher()); + $sut->progress(StateInterface::BEFORE, $this->makeDispatcher()); Assert::assertSame( 1, @@ -95,7 +95,7 @@ public function testGlobalStateWithCallingForEventAndProgressbeforeAfter() 'Should be dispatching event' ); - $sut->progress(StateInterface::AFTER); + $sut->progress(StateInterface::AFTER, $this->makeDispatcher()); Assert::assertSame( 1,