Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Traits/Continues.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,25 @@ public static function continueAsNew(...$arguments): PromiseInterface
$context = self::$context;

if (! $context->replaying) {
$parentWorkflow = $context->storedWorkflow->parents()
->wherePivot('parent_index', '!=', StoredWorkflow::CONTINUE_PARENT_INDEX)
->wherePivot('parent_index', '!=', StoredWorkflow::ACTIVE_WORKFLOW_INDEX)
->withPivot('parent_index')
->first();

$newWorkflow = self::make($context->storedWorkflow->class);
$newWorkflow->start(...$arguments);

if ($parentWorkflow) {
$parentWorkflow->children()
->attach($newWorkflow->storedWorkflow, [
'parent_index' => $parentWorkflow->pivot->parent_index,
'parent_now' => $context->now,
]);

$parentWorkflow->children()
->wherePivot('parent_index', $parentWorkflow->pivot->parent_index)
->detach($context->storedWorkflow);
}

$newWorkflow->storedWorkflow->parents()
->attach($context->storedWorkflow, [
Expand Down Expand Up @@ -45,6 +62,8 @@ public static function continueAsNew(...$arguments): PromiseInterface
'parent_now' => $context->now,
]);
}

$newWorkflow->start(...$arguments);
}

self::$context = $context;
Expand Down
25 changes: 25 additions & 0 deletions tests/Feature/ParentContinueAsNewChildWorkflowTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\Feature;

use Tests\Fixtures\TestParentContinueAsNewChildWorkflow;
use Tests\TestCase;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\WorkflowStub;

final class ParentContinueAsNewChildWorkflowTest extends TestCase
{
public function testChildWorkflowContinuesAsNew(): void
{
$workflow = WorkflowStub::make(TestParentContinueAsNewChildWorkflow::class);

$workflow->start();

while ($workflow->running());

$this->assertEquals(WorkflowCompletedStatus::class, $workflow->status());
$this->assertEquals('parent_child_done', $workflow->output());
}
}
19 changes: 19 additions & 0 deletions tests/Fixtures/TestChildContinueAsNewWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Workflow\Workflow;
use Workflow\WorkflowStub;

class TestChildContinueAsNewWorkflow extends Workflow
{
public function execute(int $count = 0, int $totalCount = 2)
{
if ($count >= $totalCount) {
return 'child_done';
}
return yield WorkflowStub::continueAsNew($count + 1, $totalCount);
}
}
17 changes: 17 additions & 0 deletions tests/Fixtures/TestParentContinueAsNewChildWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Workflow\ChildWorkflowStub;
use Workflow\Workflow;

class TestParentContinueAsNewChildWorkflow extends Workflow
{
public function execute()
{
$childResult = yield ChildWorkflowStub::make(TestChildContinueAsNewWorkflow::class);
return 'parent_' . $childResult;
}
}