|
5 | 5 | namespace Tests\Unit; |
6 | 6 |
|
7 | 7 | use Illuminate\Support\Carbon; |
| 8 | +use Illuminate\Support\Facades\Event; |
8 | 9 | use Tests\Fixtures\TestActivity; |
9 | 10 | use Tests\Fixtures\TestChildWorkflow; |
10 | 11 | use Tests\Fixtures\TestOtherActivity; |
11 | 12 | use Tests\Fixtures\TestParentWorkflow; |
12 | 13 | use Tests\Fixtures\TestWorkflow; |
13 | 14 | use Tests\TestCase; |
| 15 | +use Workflow\Events\WorkflowFailed; |
14 | 16 | use Workflow\Exception; |
15 | 17 | use Workflow\Models\StoredWorkflow; |
16 | 18 | use Workflow\Serializers\Serializer; |
17 | 19 | use Workflow\States\WorkflowCompletedStatus; |
| 20 | +use Workflow\States\WorkflowFailedStatus; |
18 | 21 | use Workflow\States\WorkflowPendingStatus; |
| 22 | +use Workflow\Workflow; |
19 | 23 | use Workflow\WorkflowStub; |
20 | 24 |
|
21 | 25 | final class WorkflowTest extends TestCase |
22 | 26 | { |
| 27 | + public function testFailed(): void |
| 28 | + { |
| 29 | + Event::fake(); |
| 30 | + |
| 31 | + $parentWorkflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); |
| 32 | + $storedParentWorkflow = StoredWorkflow::findOrFail($parentWorkflow->id()); |
| 33 | + $storedParentWorkflow->update([ |
| 34 | + 'arguments' => Serializer::serialize([]), |
| 35 | + 'status' => WorkflowPendingStatus::$name, |
| 36 | + ]); |
| 37 | + |
| 38 | + $stub = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); |
| 39 | + $storedWorkflow = StoredWorkflow::findOrFail($stub->id()); |
| 40 | + $storedWorkflow->update([ |
| 41 | + 'arguments' => Serializer::serialize([]), |
| 42 | + 'status' => WorkflowPendingStatus::class, |
| 43 | + ]); |
| 44 | + |
| 45 | + $storedWorkflow->parents() |
| 46 | + ->attach($storedParentWorkflow, [ |
| 47 | + 'parent_index' => 0, |
| 48 | + 'parent_now' => now(), |
| 49 | + ]); |
| 50 | + |
| 51 | + $workflow = new Workflow($storedWorkflow); |
| 52 | + |
| 53 | + $workflow->failed(new \Exception('Test exception')); |
| 54 | + |
| 55 | + $this->assertSame(WorkflowFailedStatus::class, $stub->status()); |
| 56 | + $this->assertSame( |
| 57 | + 'Test exception', |
| 58 | + Serializer::unserialize($stub->exceptions()->first()->exception)['message'] |
| 59 | + ); |
| 60 | + |
| 61 | + Event::assertDispatched(WorkflowFailed::class, static function ($event) use ($stub) { |
| 62 | + return $event->workflowId === $stub->id(); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + public function testFailedTwice(): void |
| 67 | + { |
| 68 | + Event::fake(); |
| 69 | + |
| 70 | + $stub = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); |
| 71 | + $storedWorkflow = StoredWorkflow::findOrFail($stub->id()); |
| 72 | + $storedWorkflow->update([ |
| 73 | + 'arguments' => Serializer::serialize([]), |
| 74 | + 'status' => WorkflowFailedStatus::class, |
| 75 | + ]); |
| 76 | + |
| 77 | + $workflow = new Workflow($storedWorkflow); |
| 78 | + |
| 79 | + $workflow->failed(new \Exception('Test exception')); |
| 80 | + |
| 81 | + $this->assertSame(WorkflowFailedStatus::class, $stub->status()); |
| 82 | + $this->assertSame( |
| 83 | + 'Test exception', |
| 84 | + Serializer::unserialize($stub->exceptions()->first()->exception)['message'] |
| 85 | + ); |
| 86 | + |
| 87 | + Event::assertNotDispatched(WorkflowFailed::class, static function ($event) use ($stub) { |
| 88 | + return $event->workflowId === $stub->id(); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + public function testFailedWithParentFailed(): void |
| 93 | + { |
| 94 | + Event::fake(); |
| 95 | + |
| 96 | + $parentWorkflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); |
| 97 | + $storedParentWorkflow = StoredWorkflow::findOrFail($parentWorkflow->id()); |
| 98 | + $storedParentWorkflow->update([ |
| 99 | + 'arguments' => Serializer::serialize([]), |
| 100 | + 'status' => WorkflowFailedStatus::$name, |
| 101 | + ]); |
| 102 | + |
| 103 | + $stub = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); |
| 104 | + $storedWorkflow = StoredWorkflow::findOrFail($stub->id()); |
| 105 | + $storedWorkflow->update([ |
| 106 | + 'arguments' => Serializer::serialize([]), |
| 107 | + 'status' => WorkflowPendingStatus::class, |
| 108 | + ]); |
| 109 | + |
| 110 | + $storedWorkflow->parents() |
| 111 | + ->attach($storedParentWorkflow, [ |
| 112 | + 'parent_index' => 0, |
| 113 | + 'parent_now' => now(), |
| 114 | + ]); |
| 115 | + |
| 116 | + $workflow = new Workflow($storedWorkflow); |
| 117 | + |
| 118 | + $workflow->failed(new \Exception('Test exception')); |
| 119 | + |
| 120 | + $this->assertSame(WorkflowFailedStatus::class, $stub->status()); |
| 121 | + $this->assertSame( |
| 122 | + 'Test exception', |
| 123 | + Serializer::unserialize($stub->exceptions()->first()->exception)['message'] |
| 124 | + ); |
| 125 | + |
| 126 | + Event::assertDispatched(WorkflowFailed::class, static function ($event) use ($stub) { |
| 127 | + return $event->workflowId === $stub->id(); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
23 | 131 | public function testException(): void |
24 | 132 | { |
25 | 133 | $exception = new \Exception('test'); |
|
0 commit comments