Skip to content

Commit 9ffb0de

Browse files
authored
Improve test coverage (#226)
1 parent 2bad80f commit 9ffb0de

4 files changed

Lines changed: 218 additions & 0 deletions

File tree

tests/Unit/ActivityTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
use BadMethodCallException;
88
use Exception;
9+
use Tests\Fixtures\NonRetryableTestExceptionActivity;
910
use Tests\Fixtures\TestExceptionActivity;
1011
use Tests\Fixtures\TestInvalidActivity;
1112
use Tests\Fixtures\TestOtherActivity;
1213
use Tests\Fixtures\TestWorkflow;
1314
use Tests\TestCase;
15+
use Workflow\Exceptions\NonRetryableException;
1416
use Workflow\Models\StoredWorkflow;
1517
use Workflow\Serializers\Serializer;
1618
use Workflow\States\WorkflowCreatedStatus;
@@ -64,6 +66,24 @@ public function testExceptionActivity(): void
6466
$this->assertSame(WorkflowFailedStatus::class, $workflow->status());
6567
}
6668

69+
public function testNonRetryableExceptionActivity(): void
70+
{
71+
$this->expectException(NonRetryableException::class);
72+
73+
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
74+
$activity = new NonRetryableTestExceptionActivity(0, now()->toDateTimeString(), StoredWorkflow::findOrFail(
75+
$workflow->id()
76+
));
77+
78+
$activity->handle();
79+
80+
$workflow->fresh();
81+
82+
$this->assertSame(1, $workflow->exceptions()->count());
83+
$this->assertSame(0, $workflow->logs()->count());
84+
$this->assertSame(WorkflowFailedStatus::class, $workflow->status());
85+
}
86+
6787
public function testFailedActivity(): void
6888
{
6989
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());

tests/Unit/ExceptionTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use Tests\Fixtures\TestWorkflow;
8+
use Tests\TestCase;
9+
use Workflow\Exception;
10+
use Workflow\Middleware\WithoutOverlappingMiddleware;
11+
use Workflow\Models\StoredWorkflow;
12+
use Workflow\Serializers\Serializer;
13+
use Workflow\States\WorkflowRunningStatus;
14+
use Workflow\WorkflowStub;
15+
16+
final class ExceptionTest extends TestCase
17+
{
18+
public function testMiddleware(): void
19+
{
20+
$exception = new Exception(0, now()->toDateTimeString(), new StoredWorkflow(), new \Exception(
21+
'Test exception'
22+
));
23+
24+
$middleware = collect($exception->middleware())
25+
->map(static fn ($middleware) => is_object($middleware) ? get_class($middleware) : $middleware)
26+
->values();
27+
28+
$this->assertCount(1, $middleware);
29+
$this->assertSame([WithoutOverlappingMiddleware::class], $middleware->all());
30+
}
31+
32+
public function testExceptionWorkflowRunning(): void
33+
{
34+
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
35+
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
36+
$storedWorkflow->update([
37+
'arguments' => Serializer::serialize([]),
38+
'status' => WorkflowRunningStatus::$name,
39+
]);
40+
41+
$exception = new Exception(0, now()->toDateTimeString(), $storedWorkflow, new \Exception('Test exception'));
42+
$exception->handle();
43+
44+
$this->assertSame(WorkflowRunningStatus::class, $workflow->status());
45+
}
46+
}

tests/Unit/SignalTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use Tests\Fixtures\TestWorkflow;
8+
use Tests\TestCase;
9+
use Workflow\Middleware\WithoutOverlappingMiddleware;
10+
use Workflow\Models\StoredWorkflow;
11+
use Workflow\Serializers\Serializer;
12+
use Workflow\Signal;
13+
use Workflow\States\WorkflowRunningStatus;
14+
use Workflow\WorkflowStub;
15+
16+
final class SignalTest extends TestCase
17+
{
18+
public function testMiddleware(): void
19+
{
20+
$signal = new Signal(new StoredWorkflow());
21+
22+
$middleware = collect($signal->middleware())
23+
->map(static fn ($middleware) => is_object($middleware) ? get_class($middleware) : $middleware)
24+
->values();
25+
26+
$this->assertCount(1, $middleware);
27+
$this->assertSame([WithoutOverlappingMiddleware::class], $middleware->all());
28+
}
29+
30+
public function testSignalWorkflowRunning(): void
31+
{
32+
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
33+
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
34+
$storedWorkflow->update([
35+
'arguments' => Serializer::serialize([]),
36+
'status' => WorkflowRunningStatus::$name,
37+
]);
38+
39+
$signal = new Signal($storedWorkflow);
40+
$signal->handle();
41+
42+
$this->assertSame(WorkflowRunningStatus::class, $workflow->status());
43+
}
44+
}

tests/Unit/WorkflowTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,129 @@
55
namespace Tests\Unit;
66

77
use Illuminate\Support\Carbon;
8+
use Illuminate\Support\Facades\Event;
89
use Tests\Fixtures\TestActivity;
910
use Tests\Fixtures\TestChildWorkflow;
1011
use Tests\Fixtures\TestOtherActivity;
1112
use Tests\Fixtures\TestParentWorkflow;
1213
use Tests\Fixtures\TestWorkflow;
1314
use Tests\TestCase;
15+
use Workflow\Events\WorkflowFailed;
1416
use Workflow\Exception;
1517
use Workflow\Models\StoredWorkflow;
1618
use Workflow\Serializers\Serializer;
1719
use Workflow\States\WorkflowCompletedStatus;
20+
use Workflow\States\WorkflowFailedStatus;
1821
use Workflow\States\WorkflowPendingStatus;
22+
use Workflow\Workflow;
1923
use Workflow\WorkflowStub;
2024

2125
final class WorkflowTest extends TestCase
2226
{
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+
23131
public function testException(): void
24132
{
25133
$exception = new \Exception('test');

0 commit comments

Comments
 (0)