Skip to content

Commit

Permalink
test: Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YunusEmreNalbant committed Jan 22, 2024
1 parent aa807d4 commit 3be7da4
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
6 changes: 3 additions & 3 deletions tests/ModelAttributesToMachinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
38 changes: 24 additions & 14 deletions tests/PersistingStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;

Expand Down Expand Up @@ -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')
Expand All @@ -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);
}
});
18 changes: 18 additions & 0 deletions tests/Stubs/Machines/Yns/Actions/YAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Yns\Actions;

use Tarfinlabs\EventMachine\Behavior\ActionBehavior;
use Tarfinlabs\EventMachine\Tests\Stubs\Models\ModelA;

class YAction extends ActionBehavior
{
public function __invoke(): void
{
ModelA::create([
'value' => 'lorem ipsum dolor',
]);
}
}
15 changes: 15 additions & 0 deletions tests/Stubs/Machines/Yns/Events/SEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Yns\Events;

use Tarfinlabs\EventMachine\Behavior\EventBehavior;

class SEvent extends EventBehavior
{
public static function getType(): string
{
return 'S_EVENT';
}
}
32 changes: 32 additions & 0 deletions tests/Stubs/Machines/Yns/YnsMachine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Yns;

use Tarfinlabs\EventMachine\Actor\Machine;
use Tarfinlabs\EventMachine\Definition\MachineDefinition;
use Tarfinlabs\EventMachine\Tests\Stubs\Machines\Yns\Events\SEvent;
use Tarfinlabs\EventMachine\Tests\Stubs\Machines\Yns\Actions\YAction;

class YnsMachine extends Machine
{
public static function definition(): MachineDefinition
{
return MachineDefinition::define(config: [
'should_persist' => false,
'initial' => 'stateY',
'states' => [
'stateY' => [
'on' => [
SEvent::class => [
'target' => 'stateS',
'actions' => [YAction::class],
],
],
],
'stateS' => [],
],
]);
}
}

0 comments on commit 3be7da4

Please sign in to comment.