-
Notifications
You must be signed in to change notification settings - Fork 38
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
021838e
commit 9525725
Showing
5 changed files
with
61 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,24 @@ | ||
<?php | ||
|
||
namespace Thunk\Verbs\Examples\Counter\Events; | ||
|
||
use Thunk\Verbs\Event; | ||
use Thunk\Verbs\Examples\Counter\Events\ResetCount; | ||
use Thunk\Verbs\Examples\Counter\States\CountState; | ||
use Thunk\Verbs\Attributes\Autodiscovery\AppliesToSingletonState; | ||
|
||
#[AppliesToSingletonState(CountState::class)] | ||
class DecrementCount extends Event | ||
{ | ||
public function apply(CountState $state) | ||
{ | ||
$state->count--; | ||
} | ||
|
||
public function handle() | ||
{ | ||
if ($this->state(CountState::class)->count < 0) { | ||
ResetCount::fire(); | ||
} | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Thunk\Verbs\Examples\Counter\Events; | ||
|
||
use Thunk\Verbs\Event; | ||
use Thunk\Verbs\Examples\Counter\States\CountState; | ||
use Thunk\Verbs\Attributes\Autodiscovery\AppliesToSingletonState; | ||
|
||
#[AppliesToSingletonState(CountState::class)] | ||
class ResetCount extends Event | ||
{ | ||
public function apply(CountState $state) | ||
{ | ||
$state->count = 0; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
use Thunk\Verbs\Facades\Verbs; | ||
use Thunk\Verbs\Examples\Counter\Events\DecrementCount; | ||
|
||
it('does not fire nested events while replaying', function () { | ||
$state = DecrementCount::fire()->state(); | ||
|
||
Verbs::commit(); | ||
|
||
// The DecrementCount handle() fires | ||
// ResetCount since count < 0 | ||
expect($state->count)->toBe(0); | ||
|
||
Verbs::replay(); | ||
|
||
// This time only DecrementCount fires | ||
expect($state->fresh()->count)->toBe(-1); | ||
}); |
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