diff --git a/src/ChildWorkflowStub.php b/src/ChildWorkflowStub.php index a0cba64..8b40c17 100644 --- a/src/ChildWorkflowStub.php +++ b/src/ChildWorkflowStub.php @@ -60,7 +60,10 @@ public static function make($workflow, ...$arguments): PromiseInterface ->contains(static fn ($argument): bool => $argument instanceof WorkflowOptions); if (! $hasOptions) { - $options = new WorkflowOptions(WorkflowStub::connection(), WorkflowStub::queue()); + $options = new WorkflowOptions( + $context->storedWorkflow->effectiveConnection(), + $context->storedWorkflow->effectiveQueue() + ); if ($options->connection !== null || $options->queue !== null) { $arguments[] = $options; diff --git a/tests/Unit/ChildWorkflowStubTest.php b/tests/Unit/ChildWorkflowStubTest.php index 3aaf74d..2cf4cf4 100644 --- a/tests/Unit/ChildWorkflowStubTest.php +++ b/tests/Unit/ChildWorkflowStubTest.php @@ -13,6 +13,7 @@ use Workflow\Models\StoredWorkflow; use Workflow\Serializers\Serializer; use Workflow\States\WorkflowPendingStatus; +use Workflow\WorkflowOptions; use Workflow\WorkflowStub; final class ChildWorkflowStubTest extends TestCase @@ -160,6 +161,89 @@ public function startAsChild(...$arguments): void $this->assertSame(1, WorkflowStub::getContext()->index); } + public function testUsesParentContextForInheritedWorkflowOptions(): void + { + $childContextStoredWorkflow = Mockery::mock(); + $childContextStoredWorkflow->shouldNotReceive('effectiveConnection'); + $childContextStoredWorkflow->shouldNotReceive('effectiveQueue'); + + $parentStoredWorkflow = Mockery::mock(); + $parentStoredWorkflow->shouldReceive('findLogByIndex') + ->once() + ->with(0) + ->andReturn(null); + $parentStoredWorkflow->shouldReceive('effectiveConnection') + ->once() + ->andReturn('sync'); + $parentStoredWorkflow->shouldReceive('effectiveQueue') + ->once() + ->andReturn('parent-queue'); + + $childWorkflow = Mockery::mock(); + $childWorkflow->shouldReceive('running') + ->once() + ->andReturn(false); + $childWorkflow->shouldReceive('completed') + ->once() + ->andReturn(false); + $childWorkflow->shouldReceive('startAsChild') + ->once() + ->withArgs( + static function (...$arguments) use ($parentStoredWorkflow): bool { + if (count($arguments) !== 4) { + return false; + } + + [$parentWorkflow, $index, $_now, $options] = $arguments; + + return $parentWorkflow === $parentStoredWorkflow + && $index === 0 + && $options instanceof WorkflowOptions + && $options->connection === 'sync' + && $options->queue === 'parent-queue'; + } + ); + + $storedChildWorkflow = Mockery::mock(); + $storedChildWorkflow->shouldReceive('toWorkflow') + ->once() + ->andReturnUsing(static function () use ($childWorkflow, $childContextStoredWorkflow) { + WorkflowStub::setContext([ + 'storedWorkflow' => $childContextStoredWorkflow, + 'index' => 0, + 'now' => now(), + 'replaying' => false, + ]); + + return $childWorkflow; + }); + + $children = Mockery::mock(); + $children->shouldReceive('wherePivot') + ->once() + ->with('parent_index', 0) + ->andReturnSelf(); + $children->shouldReceive('first') + ->once() + ->andReturn($storedChildWorkflow); + + $parentStoredWorkflow->shouldReceive('children') + ->once() + ->andReturn($children); + + WorkflowStub::setContext([ + 'storedWorkflow' => $parentStoredWorkflow, + 'index' => 0, + 'now' => now(), + 'replaying' => false, + ]); + + ChildWorkflowStub::make(TestChildWorkflow::class); + + $this->assertSame(1, WorkflowStub::getContext()->index); + $this->assertSame($parentStoredWorkflow, WorkflowStub::getContext()->storedWorkflow); + } + public function testAll(): void { $workflow = WorkflowStub::load(WorkflowStub::make(TestParentWorkflow::class)->id());