From 3be7da4e19bfc13b8c9983def1ee0c3850d3fed8 Mon Sep 17 00:00:00 2001 From: Yunus Emre Nalbant Date: Mon, 22 Jan 2024 12:31:26 +0300 Subject: [PATCH] test: Update tests --- tests/ModelAttributesToMachinesTest.php | 6 ++-- tests/PersistingStateTest.php | 38 ++++++++++++-------- tests/Stubs/Machines/Yns/Actions/YAction.php | 18 ++++++++++ tests/Stubs/Machines/Yns/Events/SEvent.php | 15 ++++++++ tests/Stubs/Machines/Yns/YnsMachine.php | 32 +++++++++++++++++ 5 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 tests/Stubs/Machines/Yns/Actions/YAction.php create mode 100644 tests/Stubs/Machines/Yns/Events/SEvent.php create mode 100644 tests/Stubs/Machines/Yns/YnsMachine.php diff --git a/tests/ModelAttributesToMachinesTest.php b/tests/ModelAttributesToMachinesTest.php index 71910f9..888752f 100644 --- a/tests/ModelAttributesToMachinesTest.php +++ b/tests/ModelAttributesToMachinesTest.php @@ -11,9 +11,9 @@ 'value' => 'some value', ]); - $modelA->traffic_mre->send(['type' => 'INC'], shouldPersist: false); - $modelA->traffic_mre->send(['type' => 'INC'], shouldPersist: false); - $modelA->traffic_mre->send(['type' => 'INC'], shouldPersist: false); + $modelA->traffic_mre->send(['type' => 'INC']); + $modelA->traffic_mre->send(['type' => 'INC']); + $modelA->traffic_mre->send(['type' => 'INC']); $modelA->traffic_mre->persist(); diff --git a/tests/PersistingStateTest.php b/tests/PersistingStateTest.php index 6c7a499..668074b 100644 --- a/tests/PersistingStateTest.php +++ b/tests/PersistingStateTest.php @@ -2,29 +2,26 @@ declare(strict_types=1); -use Tarfinlabs\EventMachine\Actor\State; use Illuminate\Database\Eloquent\Collection; +use Tarfinlabs\EventMachine\Actor\State; use Tarfinlabs\EventMachine\Models\MachineEvent; use Tarfinlabs\EventMachine\Definition\EventDefinition; use Tarfinlabs\EventMachine\Definition\StateDefinition; +use Tarfinlabs\EventMachine\Tests\Stubs\Machines\Yns\YnsMachine; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsMachine; it('can persist the machine state', function (): void { $machine = TrafficLightsMachine::create(); - $machine->send(['type' => 'INC'], shouldPersist: false); - $machine->send(['type' => 'INC'], shouldPersist: false); - $machine->send(['type' => 'INC'], shouldPersist: false); - - $state = $machine->persist(); + $machine->send(['type' => 'INC']); + $machine->send(['type' => 'INC']); + $machine->send(['type' => 'INC']); $eventIds = $machine->state->history ->pluck('id') ->map(fn ($key) => ['id' => $key]) ->toArray(); - expect($state)->toBeInstanceOf(State::class); - foreach ($eventIds as $eventId) { $this->assertDatabaseHas(MachineEvent::class, $eventId); } @@ -33,11 +30,9 @@ it('can restore the persisted state', function (): void { $machine = TrafficLightsMachine::create(); - $machine->send(['type' => 'INC'], shouldPersist: false); - $machine->send(['type' => 'INC'], shouldPersist: false); - $machine->send(['type' => 'INC'], shouldPersist: false); - - $machine->persist(); + $machine->send(['type' => 'INC']); + $machine->send(['type' => 'INC']); + $machine->send(['type' => 'INC']); $state = $machine->state; @@ -68,7 +63,7 @@ it('can auto persist after an event', function (): void { $machine = TrafficLightsMachine::create(); - $machine->send(['type' => 'INC'], shouldPersist: true); + $machine->send(['type' => 'INC']); $eventIds = $machine->state->history ->pluck('id') @@ -79,3 +74,18 @@ $this->assertDatabaseHas(MachineEvent::class, $eventId); } }); + +it('should not persist the machine state', function (): void { + $machine = YnsMachine::create(); + + $machine->send(['type' => 'S_EVENT']); + + $eventIds = $machine->state->history + ->pluck('id') + ->map(fn ($key) => ['id' => $key]) + ->toArray(); + + foreach ($eventIds as $eventId) { + $this->assertDatabaseMissing(MachineEvent::class, $eventId); + } +}); diff --git a/tests/Stubs/Machines/Yns/Actions/YAction.php b/tests/Stubs/Machines/Yns/Actions/YAction.php new file mode 100644 index 0000000..f0472ad --- /dev/null +++ b/tests/Stubs/Machines/Yns/Actions/YAction.php @@ -0,0 +1,18 @@ + 'lorem ipsum dolor', + ]); + } +} diff --git a/tests/Stubs/Machines/Yns/Events/SEvent.php b/tests/Stubs/Machines/Yns/Events/SEvent.php new file mode 100644 index 0000000..f2cd9ee --- /dev/null +++ b/tests/Stubs/Machines/Yns/Events/SEvent.php @@ -0,0 +1,15 @@ + false, + 'initial' => 'stateY', + 'states' => [ + 'stateY' => [ + 'on' => [ + SEvent::class => [ + 'target' => 'stateS', + 'actions' => [YAction::class], + ], + ], + ], + 'stateS' => [], + ], + ]); + } +}