From 3307cd965ceccd1ef607b77432d9fb62099dd3e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Emre=20Delig=C3=B6z?= Date: Mon, 4 Dec 2023 22:45:23 +0300 Subject: [PATCH] WEB-4344: feat: Inject `EventCollection` into `runAction` in `MachineDefinition.php` This commit modifies the `runAction` method in `src/Definition/MachineDefinition.php` to include `EventCollection` as a parameter. It also updates the test `it can inject requested parameters` in `tests/BehaviorDependencyInjectionTest.php` to pass `EventCollection` and adds assertions to check the instance and count of `EventCollection`. --- src/Definition/MachineDefinition.php | 13 ++++++++----- tests/BehaviorDependencyInjectionTest.php | 5 +++++ 2 files changed, 13 insertions(+), 5 deletions(-) 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); }, ], ],