Skip to content

Commit

Permalink
WEB-4344: feat: Inject EventCollection into runAction in `Machine…
Browse files Browse the repository at this point in the history
…Definition.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`.
  • Loading branch information
deligoez committed Dec 4, 2023
1 parent c081b97 commit 3307cd9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Definition/MachineDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/BehaviorDependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +30,7 @@
ContextManager $c,
EventBehavior $e,
State $s,
EventCollection $ec
): void {
expect($c)->toBeInstanceOf(ContextManager::class);
expect($c->value)->toBe(1);
Expand All @@ -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);
},
],
],
Expand Down

0 comments on commit 3307cd9

Please sign in to comment.