diff --git a/src/Definition/MachineDefinition.php b/src/Definition/MachineDefinition.php index dc6eebd..fd8b8b9 100644 --- a/src/Definition/MachineDefinition.php +++ b/src/Definition/MachineDefinition.php @@ -8,6 +8,7 @@ use Illuminate\Support\Collection; use Tarfinlabs\EventMachine\Actor\State; use Tarfinlabs\EventMachine\ContextManager; +use Tarfinlabs\EventMachine\EventCollection; use Tarfinlabs\EventMachine\Enums\BehaviorType; use Tarfinlabs\EventMachine\Enums\InternalEvent; use Tarfinlabs\EventMachine\Behavior\EventBehavior; @@ -689,6 +690,8 @@ public function transition( * * @param string $actionDefinition The action definition, either a class * @param EventBehavior|null $eventBehavior The event (optional). + * + * @throws \ReflectionException */ public function runAction( string $actionDefinition, @@ -726,13 +729,13 @@ public function runAction( $numberOfEventsInQueue = $this->eventQueue->count(); $actionBehaviorParameters = []; - /** @var \ReflectionParameter $parameter */ foreach ((new ReflectionFunction($actionBehavior))->getParameters() as $parameter) { $value = match ($parameter->getType()->getName()) { - ContextManager::class => $state->context, - EventBehavior::class => $eventBehavior, - State::class => $state, - default => null, + ContextManager::class => $state->context, + EventBehavior::class => $eventBehavior, + State::class => $state, + EventCollection::class => $state->history, + default => null, }; $actionBehaviorParameters[$parameter->getName()] = $value; } diff --git a/tests/BehaviorDependencyInjectionTest.php b/tests/BehaviorDependencyInjectionTest.php index 78f58c9..6874af9 100644 --- a/tests/BehaviorDependencyInjectionTest.php +++ b/tests/BehaviorDependencyInjectionTest.php @@ -5,6 +5,7 @@ use Tarfinlabs\EventMachine\Actor\State; use Tarfinlabs\EventMachine\Actor\Machine; use Tarfinlabs\EventMachine\ContextManager; +use Tarfinlabs\EventMachine\EventCollection; use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Definition\MachineDefinition; @@ -29,6 +30,7 @@ ContextManager $c, EventBehavior $e, State $s, + EventCollection $ec ): void { expect($c)->toBeInstanceOf(ContextManager::class); expect($c->value)->toBe(1); @@ -38,6 +40,9 @@ expect($s)->toBeInstanceOf(State::class); expect($s->value)->toBe(['machine.ready']); + + expect($ec)->toBeInstanceOf(EventCollection::class); + expect($ec->count())->toBe(7); }, ], ],