Skip to content

Commit

Permalink
test: Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
aydinfatih committed Dec 22, 2023
1 parent f97c153 commit 04ed0e0
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/EventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Tarfinlabs\EventMachine\Actor\Machine;
use Tarfinlabs\EventMachine\ContextManager;
use Tarfinlabs\EventMachine\Definition\EventDefinition;
use Tarfinlabs\EventMachine\Definition\MachineDefinition;
Expand Down Expand Up @@ -159,3 +160,66 @@
'in.transition.active.MUT.fail',
]);
});

it('stores incremental context', function (): void {
$machine = Machine::create([
'config' => [
'id' => 'traffic_light',
'initial' => 'green',
'context' => [
'count' => 1,
'value' => 'test',
],
'states' => [
'green' => [
'on' => [
'GREEN_TIMER' => [
'target' => 'yellow',
'actions' => 'changeCount',
],
],
],
'yellow' => [
'on' => [
'RED_TIMER' => [
'target' => 'red',
'actions' => 'changeValue',
],
],
],
'red' => [],
],
],
'behavior' => [
'actions' => [
'changeCount' => function (ContextManager $context): void {
$context->set('count', $context->get('count') + 1);
},
'changeValue' => function (ContextManager $context): void {
$context->set('value', 'retry');
},
],
],
]);

$machine->send(event: [
'type' => 'GREEN_TIMER',
]);

$newState = $machine->send(event: [
'type' => 'RED_TIMER',
]);

expect($newState->history)
->whereNotIn('type', [
'traffic_light.start',
'traffic_light.action.changeCount.finish',
'traffic_light.action.changeValue.finish',
'traffic_light.state.red.entry.finish',
])->each(fn ($event) => $event->context->toEqual([]))
->first()->context->toEqual(['data' => ['count' => 1, 'value' => 'test']])
->where('type', 'traffic_light.action.changeCount.finish')->first()->context->toEqual(['data' => ['count' => 2]])
->where('type', 'traffic_light.action.changeValue.finish')->first()->context->toEqual(['data' => ['value' => 'retry']])
->last()->context->toEqual(['data' => ['count' => 2, 'value' => 'retry']]);

});

0 comments on commit 04ed0e0

Please sign in to comment.