forked from asantibanez/laravel-eloquent-state-machines
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Maksym_Odanets
authored and
Maksym_Odanets
committed
Oct 23, 2024
1 parent
44e3488
commit b77372e
Showing
8 changed files
with
202 additions
and
48 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
This file was deleted.
Oops, something went wrong.
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,97 @@ | ||
<?php | ||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\Feature; | ||
|
||
use Asantibanez\LaravelEloquentStateMachines\Models\PendingTransition; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestCase; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestEnums\StatusEnum; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestModels\SalesOrderWithEnumCasting; | ||
use Carbon\Carbon; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
|
||
class EnumCastingTransitionTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
use WithFaker; | ||
|
||
/** @test */ | ||
public function can_transition_to_any_state_using_enum_casting() | ||
{ | ||
//Arrange | ||
$salesOrder = SalesOrderWithEnumCasting::create(); | ||
|
||
$this->assertTrue($salesOrder->status()->is(StatusEnum::PENDING)); | ||
|
||
$this->assertEquals(StatusEnum::PENDING, $salesOrder->status); | ||
|
||
//Act | ||
$salesOrder->status()->transitionTo(StatusEnum::APPROVED); | ||
|
||
//Assert | ||
$salesOrder->refresh(); | ||
|
||
$this->assertTrue($salesOrder->status()->is(StatusEnum::APPROVED)); | ||
|
||
$this->assertEquals(StatusEnum::APPROVED, $salesOrder->status); | ||
} | ||
|
||
/** @test */ | ||
public function can_postpone_transition_to_any_state_using_enum_casting() | ||
{ | ||
//Arrange | ||
$salesOrder = SalesOrderWithEnumCasting::create(); | ||
|
||
$this->assertTrue($salesOrder->status()->is(StatusEnum::PENDING)); | ||
|
||
$this->assertEquals(StatusEnum::PENDING, $salesOrder->status); | ||
|
||
//Act | ||
$pendingTransition = $salesOrder->status()->postponeTransitionTo(StatusEnum::APPROVED, Carbon::tomorrow()->startOfDay()); | ||
|
||
//Assert | ||
$this->assertNotNull($pendingTransition); | ||
|
||
$salesOrder->refresh(); | ||
|
||
$this->assertTrue($salesOrder->status()->is('pending')); | ||
|
||
$this->assertTrue($salesOrder->status()->hasPendingTransitions()); | ||
|
||
/** @var PendingTransition $pendingTransition */ | ||
$pendingTransition = $salesOrder->status()->pendingTransitions()->first(); | ||
|
||
$this->assertEquals('status', $pendingTransition->field); | ||
$this->assertEquals('pending', $pendingTransition->from); | ||
$this->assertEquals('approved', $pendingTransition->to); | ||
|
||
$this->assertEquals(Carbon::tomorrow()->startOfDay(), $pendingTransition->transition_at); | ||
|
||
$this->assertNull($pendingTransition->applied_at); | ||
|
||
$this->assertEquals($salesOrder->id, $pendingTransition->model->id); | ||
} | ||
|
||
/** @test */ | ||
public function can_access_model_state_history_using_enum_casting() | ||
{ | ||
//Arrange | ||
$salesOrder = SalesOrderWithEnumCasting::create(); | ||
|
||
$this->assertTrue($salesOrder->status()->is(StatusEnum::PENDING)); | ||
|
||
$this->assertEquals(StatusEnum::PENDING, $salesOrder->status); | ||
|
||
//Act | ||
$salesOrder->status()->transitionTo(StatusEnum::APPROVED); | ||
|
||
//Assert | ||
$salesOrder->refresh(); | ||
|
||
$this->assertTrue($salesOrder->status()->is(StatusEnum::APPROVED)); | ||
|
||
$this->assertEquals(StatusEnum::APPROVED, $salesOrder->status); | ||
|
||
$this->assertTrue($salesOrder->status()->was(StatusEnum::PENDING)); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\TestEnums; | ||
|
||
enum StatusEnum: string | ||
{ | ||
case APPROVED = 'approved'; | ||
case CANCELLED = 'cancelled'; | ||
case PENDING = 'pending'; | ||
case PROCESSED = 'processed'; | ||
case WAITING = 'waiting'; | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\TestModels; | ||
|
||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestEnums\StatusEnum; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestStateMachines\SalesOrders\StatusWithEnumCastingStateMachine; | ||
use Asantibanez\LaravelEloquentStateMachines\Traits\HasStateMachines; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class SalesOrderWithEnumCasting extends Model | ||
{ | ||
use HasStateMachines; | ||
|
||
protected $table = 'sales_orders'; | ||
|
||
protected $guarded = []; | ||
|
||
protected $casts = [ | ||
'status' => StatusEnum::class, | ||
]; | ||
|
||
public $stateMachines = [ | ||
'status' => StatusWithEnumCastingStateMachine::class, | ||
]; | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/TestStateMachines/SalesOrders/StatusWithEnumCastingStateMachine.php
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,27 @@ | ||
<?php | ||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\TestStateMachines\SalesOrders; | ||
|
||
use Asantibanez\LaravelEloquentStateMachines\StateMachines\StateMachine; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestEnums\StatusEnum; | ||
class StatusWithEnumCastingStateMachine extends StateMachine | ||
{ | ||
public function recordHistory(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function transitions(): array | ||
{ | ||
return [ | ||
StatusEnum::PENDING->value => [StatusEnum::APPROVED, StatusEnum::WAITING], | ||
StatusEnum::APPROVED->value => [StatusEnum::PROCESSED], | ||
StatusEnum::WAITING->value => [StatusEnum::CANCELLED], | ||
]; | ||
} | ||
|
||
public function defaultState(): ?string | ||
{ | ||
return StatusEnum::PENDING->value; | ||
} | ||
} |