Skip to content

Commit

Permalink
- multiple guard fix
Browse files Browse the repository at this point in the history
- new tests added
  • Loading branch information
emreakay committed Nov 4, 2023
1 parent 6c3af80 commit e106b0f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
6 changes: 4 additions & 2 deletions README-todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ https://github.com/sebdesign/laravel-state-machine
- history testleri
- kanban boards
- test coverage %90


- geçişlerde veya fail’lerde event’lerin dispatch edilmesi
- eğer hiç guard yoksa geçemiyor. geçebilmeli
- drop column testleri
- metadata ların kayıt edilip edilememesi test yazılması

## in progress
- arflow facade'ı içine
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ docker-compose up -d
```shell
composer format
composer analyse
composer test
composer test-coverage
composer analyse
```


Expand Down
31 changes: 19 additions & 12 deletions src/Collections/TransitionGuardResultCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@ class TransitionGuardResultCollection extends Collection

public function allowed(): bool
{
$allowed = false;
foreach ($this as $collection) {

/**
* @var Collection<int, TransitionGuardResultDTO> $collection
*/
$allowed = true;
foreach ($collection as $transitionGuardResultDTO) {

/**
* @var TransitionGuardResultDTO $transitionGuardResultDTO
*/
if ($transitionGuardResultDTO->status == TransitionGuardResultDTO::DISALLOWED) {
$allowed = false;
}
}

$this->each(
function (Collection $collection) use (&$allowed) {
$collection->each(
function (TransitionGuardResultDTO $transitionGuardResultDTO) use (&$allowed) {
if ($transitionGuardResultDTO->status == TransitionGuardResultDTO::ALLOWED) {
$allowed = true;
}
}
);
if ($allowed) {
return true;
}
);
}

return $allowed;
return false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Stateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class Stateable extends Model implements StateableModelContract

public static function supportedWorkflows(): array
{
return ['workflow1', 'workflow3'];
return ['workflow1', 'workflow3', 'workflow4'];
}
}
49 changes: 48 additions & 1 deletion tests/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@
'states' => ['todo', 'in_progress', 'done', 'cancelled'],
'initial_state' => 'todo',
],
'workflow4' => [
'states' => ['todo', 'in_progress', 'done'],
'initial_state' => 'todo',
'transitions' => [
'transtion1' => [
'from' => ['todo'],
'to' => 'in_progress',
'guards' => [],
'actions' => [],
'success_metadata' => ['asd' => 'asd'],
'success_jobs' => [TestTransitionSuccessJob::class],
],
'transtion2' => [
'from' => ['todo'],
'to' => ['done'],
'guards' => [
[TestDisallowedTransitionGuard::class, ['permission' => 'represtative_approval']],
],
'actions' => [
[TestSuccessTransitionAction::class, ['a' => 'b']],
],
'success_metadata' => ['asd' => 'asd'],
'success_jobs' => [],
],
],
],
],
],
]
Expand Down Expand Up @@ -206,7 +232,7 @@
$modelInstance->applyWorkflow($workflow);
$resultCollection = $modelInstance->transitionGuardResults($toState);
$this->assertEquals($resultCollection->count(), 2);
$this->assertEquals($resultCollection->allowed(), TransitionGuardResultDTO::ALLOWED);
$this->assertEquals($resultCollection->allowed(), true);

$resultCollection->get('transtion1')
->each(fn (TransitionGuardResultDTO $item) => expect($item->allowed())->toBeTrue());
Expand Down Expand Up @@ -373,3 +399,24 @@
->toContain('todo', 'in_progress', 'done', 'cancelled', 'in_review', 'on_going')
->toHaveCount(6);
});

it('can transition to an non-guarded and non actioned state', function () {
Queue::fake();

$name = 'name100';
$workflow = 'workflow4';
$toState = 'in_progress';

/**
* @var StateableModelContract & Model $modelInstance
*/
$modelInstance = Stateable::create(
['name' => $name]
);

$modelInstance->applyWorkflow($workflow);
$modelInstance->transitionTo($toState);

expect($modelInstance->currentState())->toEqual($toState);
Queue::assertPushed(TestTransitionSuccessJob::class);
});

0 comments on commit e106b0f

Please sign in to comment.