Skip to content

Commit

Permalink
split behavior in GlobalDispatcher and add some integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
overclokk committed Sep 27, 2023
1 parent 51fb48a commit 234a737
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/GlobalState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/LegacyEventDispatcherMethodsDeprecatedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand All @@ -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)
{

Expand Down
4 changes: 2 additions & 2 deletions src/NullState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/StateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions tests/integration/GlobalStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions tests/src/GlobalStateTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -95,7 +95,7 @@ public function testGlobalStateWithCallingForEventAndProgressbeforeAfter()
'Should be dispatching event'
);

$sut->progress(StateInterface::AFTER);
$sut->progress(StateInterface::AFTER, $this->makeDispatcher());

Assert::assertSame(
1,
Expand Down

0 comments on commit 234a737

Please sign in to comment.