-
Notifications
You must be signed in to change notification settings - Fork 60
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 #15 from asantibanez/features/add-before-transitio…
…n-hooks Add before transition hooks and refactored after transition hooks
- Loading branch information
Showing
14 changed files
with
348 additions
and
36 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 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,65 @@ | ||
<?php | ||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\Feature; | ||
|
||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestCase; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestJobs\AfterTransitionJob; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestModels\SalesOrderWithAfterTransitionHook; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestModels\SalesOrderWithBeforeTransitionHook; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Queue; | ||
|
||
class AfterTransitionHookTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
use WithFaker; | ||
|
||
/** @test */ | ||
public function should_call_after_transition_hooks() | ||
{ | ||
//Arrange | ||
Queue::fake(); | ||
|
||
$salesOrder = SalesOrderWithAfterTransitionHook::create([ | ||
'total' => 100, | ||
'notes' => 'before', | ||
]); | ||
|
||
//Act | ||
$salesOrder->status()->transitionTo('approved'); | ||
|
||
//Assert | ||
$salesOrder->refresh(); | ||
|
||
$this->assertEquals(200, $salesOrder->total); | ||
$this->assertEquals('after', $salesOrder->notes); | ||
|
||
Queue::assertPushed(AfterTransitionJob::class); | ||
} | ||
|
||
/** @test */ | ||
public function should_not_call_after_transition_hooks_if_not_defined() | ||
{ | ||
//Arrange | ||
Queue::fake(); | ||
|
||
$salesOrder = SalesOrderWithAfterTransitionHook::create([ | ||
'status' => 'approved' | ||
]); | ||
|
||
$this->assertNull($salesOrder->total); | ||
$this->assertNull($salesOrder->notes); | ||
|
||
//Act | ||
$salesOrder->status()->transitionTo('processed'); | ||
|
||
//Assert | ||
$salesOrder->refresh(); | ||
|
||
$this->assertNull($salesOrder->total); | ||
$this->assertNull($salesOrder->notes); | ||
|
||
Queue::assertNotPushed(AfterTransitionJob::class); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\Feature; | ||
|
||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestCase; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestJobs\BeforeTransitionJob; | ||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestModels\SalesOrderWithBeforeTransitionHook; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Queue; | ||
|
||
class BeforeTransitionHookTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
use WithFaker; | ||
|
||
/** @test */ | ||
public function should_call_before_transition_hooks() | ||
{ | ||
//Arrange | ||
Queue::fake(); | ||
|
||
$salesOrder = SalesOrderWithBeforeTransitionHook::create(); | ||
|
||
$this->assertNull($salesOrder->total); | ||
$this->assertNull($salesOrder->notes); | ||
|
||
//Act | ||
$salesOrder->status()->transitionTo('approved'); | ||
|
||
//Assert | ||
$salesOrder->refresh(); | ||
|
||
$this->assertEquals(100, $salesOrder->total); | ||
$this->assertEquals('Notes updated', $salesOrder->notes); | ||
|
||
Queue::assertPushed(BeforeTransitionJob::class); | ||
} | ||
|
||
/** @test */ | ||
public function should_not_call_before_transition_hooks_if_not_defined() | ||
{ | ||
//Arrange | ||
Queue::fake(); | ||
|
||
$salesOrder = SalesOrderWithBeforeTransitionHook::create([ | ||
'status' => 'approved' | ||
]); | ||
|
||
$this->assertNull($salesOrder->total); | ||
$this->assertNull($salesOrder->notes); | ||
|
||
//Act | ||
$salesOrder->status()->transitionTo('processed'); | ||
|
||
//Assert | ||
$salesOrder->refresh(); | ||
|
||
$this->assertNull($salesOrder->total); | ||
$this->assertNull($salesOrder->notes); | ||
|
||
Queue::assertNotPushed(BeforeTransitionJob::class); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\TestJobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class AfterTransitionJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
} |
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,15 @@ | ||
<?php | ||
|
||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\TestJobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class BeforeTransitionJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\TestModels; | ||
|
||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestStateMachines\SalesOrders\StatusWithAfterTransitionHookStateMachine; | ||
use Asantibanez\LaravelEloquentStateMachines\Traits\HasStateMachines; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class SalesOrderWithAfterTransitionHook extends Model | ||
{ | ||
use HasStateMachines; | ||
|
||
protected $table = 'sales_orders'; | ||
|
||
protected $guarded = []; | ||
|
||
public $stateMachines = [ | ||
'status' => StatusWithAfterTransitionHookStateMachine::class, | ||
]; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Asantibanez\LaravelEloquentStateMachines\Tests\TestModels; | ||
|
||
use Asantibanez\LaravelEloquentStateMachines\Tests\TestStateMachines\SalesOrders\StatusWithBeforeTransitionHookStateMachine; | ||
use Asantibanez\LaravelEloquentStateMachines\Traits\HasStateMachines; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class SalesOrderWithBeforeTransitionHook extends Model | ||
{ | ||
use HasStateMachines; | ||
|
||
protected $table = 'sales_orders'; | ||
|
||
protected $guarded = []; | ||
|
||
public $stateMachines = [ | ||
'status' => StatusWithBeforeTransitionHookStateMachine::class, | ||
]; | ||
} |
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
Oops, something went wrong.