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
5 changes: 4 additions & 1 deletion src/ChildWorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
84 changes: 84 additions & 0 deletions tests/Unit/ChildWorkflowStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down