diff --git a/src/ChildWorkflowStub.php b/src/ChildWorkflowStub.php index 8b40c17..b4eb6ee 100644 --- a/src/ChildWorkflowStub.php +++ b/src/ChildWorkflowStub.php @@ -60,10 +60,7 @@ public static function make($workflow, ...$arguments): PromiseInterface ->contains(static fn ($argument): bool => $argument instanceof WorkflowOptions); if (! $hasOptions) { - $options = new WorkflowOptions( - $context->storedWorkflow->effectiveConnection(), - $context->storedWorkflow->effectiveQueue() - ); + $options = $context->storedWorkflow->workflowOptions(); if ($options->connection !== null || $options->queue !== null) { $arguments[] = $options; diff --git a/tests/Unit/ChildWorkflowStubTest.php b/tests/Unit/ChildWorkflowStubTest.php index 2cf4cf4..af4ceb7 100644 --- a/tests/Unit/ChildWorkflowStubTest.php +++ b/tests/Unit/ChildWorkflowStubTest.php @@ -142,12 +142,9 @@ public function startAsChild(...$arguments): void $storedWorkflow->shouldReceive('children') ->once() ->andReturn($children); - $storedWorkflow->shouldReceive('effectiveConnection') + $storedWorkflow->shouldReceive('workflowOptions') ->once() - ->andReturn(null); - $storedWorkflow->shouldReceive('effectiveQueue') - ->once() - ->andReturn(null); + ->andReturn(new WorkflowOptions()); WorkflowStub::setContext([ 'storedWorkflow' => $storedWorkflow, @@ -164,20 +161,16 @@ public function startAsChild(...$arguments): void public function testUsesParentContextForInheritedWorkflowOptions(): void { $childContextStoredWorkflow = Mockery::mock(); - $childContextStoredWorkflow->shouldNotReceive('effectiveConnection'); - $childContextStoredWorkflow->shouldNotReceive('effectiveQueue'); + $childContextStoredWorkflow->shouldNotReceive('workflowOptions'); $parentStoredWorkflow = Mockery::mock(); $parentStoredWorkflow->shouldReceive('findLogByIndex') ->once() ->with(0) ->andReturn(null); - $parentStoredWorkflow->shouldReceive('effectiveConnection') - ->once() - ->andReturn('sync'); - $parentStoredWorkflow->shouldReceive('effectiveQueue') + $parentStoredWorkflow->shouldReceive('workflowOptions') ->once() - ->andReturn('parent-queue'); + ->andReturn(new WorkflowOptions('sync', 'parent-queue')); $childWorkflow = Mockery::mock(); $childWorkflow->shouldReceive('running') @@ -244,6 +237,70 @@ static function (...$arguments) use ($parentStoredWorkflow): bool { $this->assertSame($parentStoredWorkflow, WorkflowStub::getContext()->storedWorkflow); } + public function testDoesNotPassWorkflowOptionsWhenParentOptionsAreUnset(): void + { + $parentStoredWorkflow = Mockery::mock(); + $parentStoredWorkflow->shouldReceive('findLogByIndex') + ->once() + ->with(0) + ->andReturn(null); + $parentStoredWorkflow->shouldReceive('workflowOptions') + ->once() + ->andReturn(new WorkflowOptions()); + + $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) !== 3) { + return false; + } + + [$parentWorkflow, $index, $_now] = $arguments; + + return $parentWorkflow === $parentStoredWorkflow + && $index === 0; + } + ); + + $storedChildWorkflow = Mockery::mock(); + $storedChildWorkflow->shouldReceive('toWorkflow') + ->once() + ->andReturn($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());