Skip to content

Commit 794173d

Browse files
authored
Child workflow options (#343)
1 parent 89ae847 commit 794173d

2 files changed

Lines changed: 70 additions & 16 deletions

File tree

src/ChildWorkflowStub.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ public static function make($workflow, ...$arguments): PromiseInterface
6060
->contains(static fn ($argument): bool => $argument instanceof WorkflowOptions);
6161

6262
if (! $hasOptions) {
63-
$options = new WorkflowOptions(
64-
$context->storedWorkflow->effectiveConnection(),
65-
$context->storedWorkflow->effectiveQueue()
66-
);
63+
$options = $context->storedWorkflow->workflowOptions();
6764

6865
if ($options->connection !== null || $options->queue !== null) {
6966
$arguments[] = $options;

tests/Unit/ChildWorkflowStubTest.php

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,9 @@ public function startAsChild(...$arguments): void
142142
$storedWorkflow->shouldReceive('children')
143143
->once()
144144
->andReturn($children);
145-
$storedWorkflow->shouldReceive('effectiveConnection')
145+
$storedWorkflow->shouldReceive('workflowOptions')
146146
->once()
147-
->andReturn(null);
148-
$storedWorkflow->shouldReceive('effectiveQueue')
149-
->once()
150-
->andReturn(null);
147+
->andReturn(new WorkflowOptions());
151148

152149
WorkflowStub::setContext([
153150
'storedWorkflow' => $storedWorkflow,
@@ -164,20 +161,16 @@ public function startAsChild(...$arguments): void
164161
public function testUsesParentContextForInheritedWorkflowOptions(): void
165162
{
166163
$childContextStoredWorkflow = Mockery::mock();
167-
$childContextStoredWorkflow->shouldNotReceive('effectiveConnection');
168-
$childContextStoredWorkflow->shouldNotReceive('effectiveQueue');
164+
$childContextStoredWorkflow->shouldNotReceive('workflowOptions');
169165

170166
$parentStoredWorkflow = Mockery::mock();
171167
$parentStoredWorkflow->shouldReceive('findLogByIndex')
172168
->once()
173169
->with(0)
174170
->andReturn(null);
175-
$parentStoredWorkflow->shouldReceive('effectiveConnection')
176-
->once()
177-
->andReturn('sync');
178-
$parentStoredWorkflow->shouldReceive('effectiveQueue')
171+
$parentStoredWorkflow->shouldReceive('workflowOptions')
179172
->once()
180-
->andReturn('parent-queue');
173+
->andReturn(new WorkflowOptions('sync', 'parent-queue'));
181174

182175
$childWorkflow = Mockery::mock();
183176
$childWorkflow->shouldReceive('running')
@@ -244,6 +237,70 @@ static function (...$arguments) use ($parentStoredWorkflow): bool {
244237
$this->assertSame($parentStoredWorkflow, WorkflowStub::getContext()->storedWorkflow);
245238
}
246239

240+
public function testDoesNotPassWorkflowOptionsWhenParentOptionsAreUnset(): void
241+
{
242+
$parentStoredWorkflow = Mockery::mock();
243+
$parentStoredWorkflow->shouldReceive('findLogByIndex')
244+
->once()
245+
->with(0)
246+
->andReturn(null);
247+
$parentStoredWorkflow->shouldReceive('workflowOptions')
248+
->once()
249+
->andReturn(new WorkflowOptions());
250+
251+
$childWorkflow = Mockery::mock();
252+
$childWorkflow->shouldReceive('running')
253+
->once()
254+
->andReturn(false);
255+
$childWorkflow->shouldReceive('completed')
256+
->once()
257+
->andReturn(false);
258+
$childWorkflow->shouldReceive('startAsChild')
259+
->once()
260+
->withArgs(
261+
static function (...$arguments) use ($parentStoredWorkflow): bool {
262+
if (count($arguments) !== 3) {
263+
return false;
264+
}
265+
266+
[$parentWorkflow, $index, $_now] = $arguments;
267+
268+
return $parentWorkflow === $parentStoredWorkflow
269+
&& $index === 0;
270+
}
271+
);
272+
273+
$storedChildWorkflow = Mockery::mock();
274+
$storedChildWorkflow->shouldReceive('toWorkflow')
275+
->once()
276+
->andReturn($childWorkflow);
277+
278+
$children = Mockery::mock();
279+
$children->shouldReceive('wherePivot')
280+
->once()
281+
->with('parent_index', 0)
282+
->andReturnSelf();
283+
$children->shouldReceive('first')
284+
->once()
285+
->andReturn($storedChildWorkflow);
286+
287+
$parentStoredWorkflow->shouldReceive('children')
288+
->once()
289+
->andReturn($children);
290+
291+
WorkflowStub::setContext([
292+
'storedWorkflow' => $parentStoredWorkflow,
293+
'index' => 0,
294+
'now' => now(),
295+
'replaying' => false,
296+
]);
297+
298+
ChildWorkflowStub::make(TestChildWorkflow::class);
299+
300+
$this->assertSame(1, WorkflowStub::getContext()->index);
301+
$this->assertSame($parentStoredWorkflow, WorkflowStub::getContext()->storedWorkflow);
302+
}
303+
247304
public function testAll(): void
248305
{
249306
$workflow = WorkflowStub::load(WorkflowStub::make(TestParentWorkflow::class)->id());

0 commit comments

Comments
 (0)