Skip to content

Commit

Permalink
Add TransitionListener and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaweipan committed Aug 6, 2023
1 parent 04ccb6f commit 35b1178
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Dispatcher/AbstractSubject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DDDPHP\StateMachine\Dispatcher;

use DDDPHP\StateMachine\Event\EventInterface;
use DDDPHP\StateMachine\StateMachine\StateMachineException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;

Expand All @@ -20,7 +21,11 @@ public function __construct()
}
public function addListener(EventInterface $event, callable $listener): void
{
$this->listeners[$event::class][] = $listener;
$classImplements = array_values(class_implements($event));
if (empty($classImplements[0])) {
throw new StateMachineException(" the event must implements an interface");
}
$this->listeners[$classImplements[0]][] = $listener;
}

public function fireEvent(EventInterface $event): void
Expand Down
4 changes: 1 addition & 3 deletions src/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
final class Dispatcher implements EventDispatcherInterface
{
private ListenerProviderInterface $listenerProvider;
/**
* @param ListenerProviderInterface $listenerProvider
*/

public function __construct(ListenerProviderInterface $listenerProvider)
{
$this->listenerProvider = $listenerProvider;
Expand Down
16 changes: 16 additions & 0 deletions src/StateMachine/StateMachineImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,20 @@ public function setReady(bool $ready): void
{
$this->ready = $ready;
}

public function addTransitionPreListener(callable $listener): void
{
$this->addListener(
new class () implements PreTransitionEventInterface {},
$listener
);
}

public function addTransitionPostListener(callable $listener): void
{
$this->addListener(
new class () implements PostTransitionEventInterface {},
$listener
);
}
}
4 changes: 4 additions & 0 deletions src/StateMachine/StateMachineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public function fire(string $sourceStateId, string $event, $context): string;
public function getMachineId(): string;

public function showStateMachine(): void;

public function addTransitionPreListener(callable $listener): void;

public function addTransitionPostListener(callable $listener): void;
}
26 changes: 26 additions & 0 deletions tests/StateMachineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DDDPHP\StateMachine\Action\ActionInterface;
use DDDPHP\StateMachine\Builder\StateMachineBuilderFactory;
use DDDPHP\StateMachine\Condition\ConditionInterface;
use DDDPHP\StateMachine\Event\PreTransitionEventInterface;
use DDDPHP\StateMachine\StateMachine\StateMachineInterface;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -87,6 +88,31 @@ public function testInternalNormal(): void
$this->assertEquals(self::STATE1, $target);
}

public function testTransitionListener() :void
{
$preTransitionListenerCount = 0;
$postTransitionListenerCount = 0;
$builder = StateMachineBuilderFactory::create();
$builder->externalTransitions()
->fromAmong(self::STATE1, self::STATE2, self::STATE3)
->to(self::STATE4)
->on(self::EVENT1)
->when($this->checkCondition())
->perform($this->doAction());
$stateMachine = $builder->build(self::MACHINE_ID . "transition-listener");

$stateMachine->addTransitionPreListener(function () use (&$preTransitionListenerCount) {
$preTransitionListenerCount += 1;
});
$stateMachine->addTransitionPostListener(function () use (&$postTransitionListenerCount) {
$postTransitionListenerCount += 2;
});

$stateMachine->fire(self::STATE2, self::EVENT1, $this->context);

$this->assertEquals(1, $preTransitionListenerCount);
$this->assertEquals(2, $postTransitionListenerCount);
}
public function testExternalInternalNormal(): void
{
$stateMachine = $this->buildStateMachine();
Expand Down

0 comments on commit 35b1178

Please sign in to comment.