-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c33c49
commit 476fe93
Showing
6 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Thunk\Verbs\Facades\Verbs; | ||
use Thunk\Verbs\Examples\Counter\States\CountState; | ||
use Thunk\Verbs\Examples\Counter\Events\IncrementCount; | ||
use Thunk\Verbs\Events\VerbsStateInitialized; | ||
use Thunk\Verbs\Models\VerbEvent; | ||
|
||
it('State factory initializes a state', function () { | ||
$count_state = CountState::factory([ | ||
'count' => 1337 | ||
]); | ||
|
||
expect($events = VerbEvent::all()) | ||
->toHaveCount(1); | ||
|
||
expect($events->first()) | ||
->type->toBe(VerbsStateInitialized::class); | ||
|
||
expect($count_state) | ||
->toBeInstanceOf(CountState::class) | ||
->count | ||
->toBe(1337); | ||
|
||
IncrementCount::fire(); | ||
Verbs::commit(); | ||
|
||
expect($count_state) | ||
->toBeInstanceOf(CountState::class) | ||
->count | ||
->toBe(1338); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Thunk\Verbs\Events; | ||
|
||
use Thunk\Verbs\Event; | ||
|
||
class VerbsStateInitialized extends Event | ||
{ | ||
protected $verbs_should_commit_immediately = true; | ||
|
||
public function __construct( | ||
public int|string|null $state_id, | ||
public string $state_class, | ||
public array $state_data, | ||
) {} | ||
|
||
public function registerStates(): array | ||
{ | ||
return [ | ||
$this->state_id | ||
? $this->state_class::load($this->state_id) | ||
: $this->state_class::singleton(), | ||
]; | ||
} | ||
|
||
public function validate() | ||
{ | ||
$this->assert( | ||
! $this->state()->__verbs_initialized, | ||
'State has already been initialized', | ||
); | ||
} | ||
|
||
public function apply() | ||
{ | ||
foreach ($this->state_data as $key => $value) { | ||
$this->state()->$key = $value; | ||
} | ||
|
||
$this->state()->__verbs_initialized = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters