-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from bjrnblm/master
Get transitionable states from state on Model
- Loading branch information
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
docs/working-with-transitions/04-retrieving-transitionable-states.md
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,52 @@ | ||
--- | ||
title: Retrieving transitionable states | ||
weight: 4 | ||
--- | ||
|
||
An array of transitionable states can use the retrieved with the `transitionableStates()` method on your model. | ||
|
||
```php | ||
class Payment extends Model | ||
{ | ||
// … | ||
|
||
protected function registerStates(): void | ||
{ | ||
$this->addState('state', PaymentState::class) | ||
->allowTransition(Pending::class, Paid::class) | ||
->allowTransition(Paid::class, Refunded::class) | ||
} | ||
} | ||
``` | ||
|
||
```php | ||
$transitionableStates = $payment->transitionableStates(Pending::class); | ||
``` | ||
|
||
This will return an array with all transitionable states for `Pending::class` | ||
|
||
```php | ||
[ | ||
0 => "paid" | ||
] | ||
``` | ||
|
||
## Transitionable states from state | ||
|
||
Its also possible to use `transitionableStates()` method directly on a state: | ||
|
||
```php | ||
$payment->state->transitionableStates(); | ||
``` | ||
|
||
## Multiple state fields on model | ||
|
||
If there are multiple fields, a `\Spatie\ModelStates\Exceptions\InvalidConfig` exception will be thrown. You can pass the state field name explicitly as a parameter: | ||
|
||
```php | ||
// From a model | ||
$payment->transitionableStates(Pending::class, 'fieldName') | ||
|
||
// From a state | ||
$payment->state->transitionableStates('fieldName'); | ||
``` |
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
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,59 @@ | ||
<?php | ||
|
||
namespace Spatie\ModelStates\Tests; | ||
|
||
use Spatie\ModelStates\Tests\Dummy\Payment; | ||
use Spatie\ModelStates\Exceptions\InvalidConfig; | ||
use Spatie\ModelStates\Tests\Dummy\States\Failed; | ||
use Spatie\ModelStates\Tests\Dummy\States\Created; | ||
use Spatie\ModelStates\Tests\Dummy\States\Pending; | ||
use Spatie\ModelStates\Tests\Dummy\ModelWithMultipleStates; | ||
|
||
class TransitionableStatesTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function transitionable_states_with_fieldname() | ||
{ | ||
$payment = new Payment(); | ||
|
||
$transitionableStates = $payment->transitionableStates(Created::class, 'state'); | ||
|
||
$this->assertEquals( | ||
$transitionableStates, | ||
[Pending::getMorphClass(), Failed::getMorphClass()] | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function transitionable_states_without_fieldname() | ||
{ | ||
$payment = new Payment(); | ||
|
||
$transitionableStates = $payment->transitionableStates(Created::class); | ||
|
||
$this->assertEquals( | ||
$transitionableStates, | ||
[Pending::getMorphClass(), Failed::getMorphClass()] | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function transitionable_states_with_invalid_fieldname_fails() | ||
{ | ||
$this->expectException(InvalidConfig::class); | ||
|
||
$payment = new Payment(); | ||
|
||
$transitionableStates = $payment->transitionableStates(Created::class, 'wrong'); | ||
} | ||
|
||
/** @test */ | ||
public function transitionable_states_with_multiple_states_without_fieldname_fails() | ||
{ | ||
$this->expectException(InvalidConfig::class); | ||
|
||
$payment = new ModelWithMultipleStates(); | ||
|
||
$transitionableStates = $payment->transitionableStates(DummyState::class); | ||
} | ||
} |