Skip to content

Commit d27d514

Browse files
authored
feat: support typed results and pass firstExecutionRunId for update handlers (#761)
1 parent 0a6f750 commit d27d514

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

src/Internal/Client/WorkflowStarter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ function (UpdateWithStartInput $input): UpdateWithStartOutput {
158158
->setWaitPolicy(
159159
(new \Temporal\Api\Update\V1\WaitPolicy())
160160
->setLifecycleStage($input->updateInput->waitPolicy->lifecycleStage->value),
161-
);
161+
)
162+
->setFirstExecutionRunId($input->updateInput->firstExecutionRunId);
162163

163164
// Configure Meta
164165
$meta = new \Temporal\Api\Update\V1\Meta();
@@ -268,8 +269,8 @@ function (UpdateWithStartInput $input): UpdateWithStartOutput {
268269
Header::empty(),
269270
$update->waitPolicy,
270271
$update->updateId ?? Uuid::v4(),
271-
'',
272-
null, // todo?
272+
$update->firstExecutionRunId ?? '',
273+
$update->resultType,
273274
),
274275
),
275276
);

tests/Acceptance/Extra/Update/UpdateWithStartTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77
use PHPUnit\Framework\Attributes\Test;
88
use Ramsey\Uuid\Uuid;
9+
use Temporal\Client\Update\LifecycleStage;
10+
use Temporal\Client\Update\UpdateOptions;
911
use Temporal\Client\WorkflowClientInterface;
1012
use Temporal\Client\WorkflowOptions;
1113
use Temporal\Exception\Client\WorkflowExecutionAlreadyStartedException;
1214
use Temporal\Exception\Client\WorkflowFailedException;
15+
use Temporal\Exception\Client\WorkflowServiceException;
1316
use Temporal\Exception\Client\WorkflowUpdateException;
1417
use Temporal\Tests\Acceptance\App\Runtime\Feature;
1518
use Temporal\Tests\Acceptance\App\TestCase;
@@ -41,6 +44,49 @@ public function runInGoodWay(
4144
$this->assertFalse($handle->hasResult());
4245
}
4346

47+
#[Test]
48+
public function returnsTypedUpdateResult(
49+
WorkflowClientInterface $client,
50+
Feature $feature,
51+
): void {
52+
$stub = $client->newUntypedWorkflowStub(
53+
'Extra_Update_UpdateWithStart',
54+
WorkflowOptions::new()->withTaskQueue($feature->taskQueue),
55+
);
56+
57+
$options = UpdateOptions::new('echo', LifecycleStage::StageCompleted)
58+
->withResultType(UpdateResult::class);
59+
60+
/** @see TestWorkflow::echo */
61+
$handle = $client->updateWithStart($stub, $options, ['hello']);
62+
63+
$result = $handle->getResult();
64+
$this->assertInstanceOf(UpdateResult::class, $result);
65+
$this->assertSame('hello', $result->name);
66+
$this->assertSame(5, $result->length);
67+
68+
$stub->signal('exit');
69+
$stub->getResult();
70+
}
71+
72+
#[Test]
73+
public function rejectsFirstExecutionRunId(
74+
WorkflowClientInterface $client,
75+
Feature $feature,
76+
): void {
77+
$stub = $client->newUntypedWorkflowStub(
78+
'Extra_Update_UpdateWithStart',
79+
WorkflowOptions::new()->withTaskQueue($feature->taskQueue),
80+
);
81+
82+
$options = UpdateOptions::new('await', LifecycleStage::StageAccepted)
83+
->withFirstExecutionRunId(Uuid::uuid7()->__toString());
84+
85+
$this->expectException(WorkflowServiceException::class);
86+
$this->expectExceptionMessage('FirstExecutionRunId is not allowed');
87+
$client->updateWithStart($stub, $options, ['key']);
88+
}
89+
4490
#[Test]
4591
public function failWithBadUpdateName(
4692
WorkflowClientInterface $client,
@@ -128,9 +174,24 @@ public function validateAdd(string $name): void
128174
empty($name) and throw new \InvalidArgumentException('Name must not be empty');
129175
}
130176

177+
#[Workflow\UpdateMethod(name: 'echo')]
178+
public function echo(string $name): UpdateResult
179+
{
180+
$this->updateStarted = true;
181+
return new UpdateResult($name, \strlen($name));
182+
}
183+
131184
#[Workflow\SignalMethod]
132185
public function exit(): void
133186
{
134187
$this->exit = true;
135188
}
136189
}
190+
191+
class UpdateResult
192+
{
193+
public function __construct(
194+
public string $name = '',
195+
public int $length = 0,
196+
) {}
197+
}

0 commit comments

Comments
 (0)