From ef98df7c5b2b64d876d1779c4d8086fd3ca2fbf5 Mon Sep 17 00:00:00 2001 From: Richard McDaniel Date: Wed, 25 Feb 2026 15:10:24 -0600 Subject: [PATCH 1/3] Use context --- src/ChildWorkflowStub.php | 5 +- tests/Unit/ChildWorkflowStubTest.php | 84 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) 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()); From 15fc0452b071bb4a246c3a503155b41e97a6a774 Mon Sep 17 00:00:00 2001 From: Richard McDaniel Date: Fri, 27 Feb 2026 07:22:04 -0600 Subject: [PATCH 2/3] Fix options passing --- src/ChildWorkflowStub.php | 5 +- tests/Unit/ChildWorkflowStubTest.php | 81 +++++++++++++++++++++++----- 2 files changed, 70 insertions(+), 16 deletions(-) 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()); From bbd7c6184e18f66e801f5d0835f3dcfcb349dcd0 Mon Sep 17 00:00:00 2001 From: Richard McDaniel Date: Fri, 27 Feb 2026 08:08:46 -0600 Subject: [PATCH 3/3] Remove old test --- tests/Unit/ChildWorkflowStubTest.php | 83 ---------------------------- 1 file changed, 83 deletions(-) diff --git a/tests/Unit/ChildWorkflowStubTest.php b/tests/Unit/ChildWorkflowStubTest.php index e47fe0e..af4ceb7 100644 --- a/tests/Unit/ChildWorkflowStubTest.php +++ b/tests/Unit/ChildWorkflowStubTest.php @@ -301,89 +301,6 @@ static function (...$arguments) use ($parentStoredWorkflow): bool { $this->assertSame($parentStoredWorkflow, WorkflowStub::getContext()->storedWorkflow); } - 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());