Skip to content

Commit 1aac5d4

Browse files
authored
Improve test coverage (#227)
1 parent 9ffb0de commit 1aac5d4

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures;
6+
7+
use Workflow\Workflow;
8+
9+
class TestThrowOnReturnWorkflow extends Workflow
10+
{
11+
public function execute()
12+
{
13+
return new class() {
14+
public function valid()
15+
{
16+
return false;
17+
}
18+
};
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures;
6+
7+
use Workflow\Workflow;
8+
9+
class TestYieldNonPromiseWorkflow extends Workflow
10+
{
11+
public function execute()
12+
{
13+
yield 'not-a-promise';
14+
}
15+
}

tests/Unit/WorkflowTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
namespace Tests\Unit;
66

7+
use BadMethodCallException;
78
use Illuminate\Support\Carbon;
89
use Illuminate\Support\Facades\Event;
910
use Tests\Fixtures\TestActivity;
1011
use Tests\Fixtures\TestChildWorkflow;
1112
use Tests\Fixtures\TestOtherActivity;
1213
use Tests\Fixtures\TestParentWorkflow;
14+
use Tests\Fixtures\TestThrowOnReturnWorkflow;
1315
use Tests\Fixtures\TestWorkflow;
16+
use Tests\Fixtures\TestYieldNonPromiseWorkflow;
1417
use Tests\TestCase;
1518
use Workflow\Events\WorkflowFailed;
1619
use Workflow\Exception;
@@ -276,4 +279,52 @@ public function testParentPending(): void
276279
$this->assertSame(WorkflowCompletedStatus::class, $childWorkflow->status());
277280
$this->assertSame('other', $childWorkflow->output());
278281
}
282+
283+
public function testThrowsWhenExecuteMethodIsMissing(): void
284+
{
285+
$stub = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
286+
$storedWorkflow = StoredWorkflow::findOrFail($stub->id());
287+
$storedWorkflow->update([
288+
'arguments' => Serializer::serialize([]),
289+
'status' => WorkflowPendingStatus::class,
290+
]);
291+
292+
$this->expectException(BadMethodCallException::class);
293+
$this->expectExceptionMessage('Execute method not implemented.');
294+
295+
$workflow = new Workflow($storedWorkflow);
296+
$workflow->handle();
297+
}
298+
299+
public function testThrowsWhenYieldNonPromise(): void
300+
{
301+
$stub = WorkflowStub::load(WorkflowStub::make(TestYieldNonPromiseWorkflow::class)->id());
302+
$storedWorkflow = StoredWorkflow::findOrFail($stub->id());
303+
$storedWorkflow->update([
304+
'arguments' => Serializer::serialize([]),
305+
'status' => WorkflowPendingStatus::class,
306+
]);
307+
308+
$this->expectException(\Exception::class);
309+
$this->expectExceptionMessage('something went wrong');
310+
311+
$workflow = new TestYieldNonPromiseWorkflow($storedWorkflow);
312+
$workflow->handle();
313+
}
314+
315+
public function testThrowsWrappedException(): void
316+
{
317+
$stub = WorkflowStub::load(WorkflowStub::make(TestThrowOnReturnWorkflow::class)->id());
318+
$storedWorkflow = StoredWorkflow::findOrFail($stub->id());
319+
$storedWorkflow->update([
320+
'arguments' => Serializer::serialize([]),
321+
'status' => WorkflowPendingStatus::class,
322+
]);
323+
324+
$this->expectException(\Exception::class);
325+
$this->expectExceptionMessage('Workflow failed.');
326+
327+
$workflow = new TestThrowOnReturnWorkflow($storedWorkflow);
328+
$workflow->handle();
329+
}
279330
}

0 commit comments

Comments
 (0)