Skip to content

Commit 8ede13d

Browse files
authored
Merge pull request #594: fix incorrect updateWithStart signature
2 parents 4f17d8e + 9e7cbe7 commit 8ede13d

File tree

8 files changed

+135
-27
lines changed

8 files changed

+135
-27
lines changed

src/Client/WorkflowClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,19 @@ public function updateWithStart(
250250
);
251251
$workflowStub->hasExecution() and throw new InvalidArgumentException(self::ERROR_WORKFLOW_START_DUPLICATION);
252252

253-
[$execution, $handle] = $this->getStarter()->updateWithStart(
253+
$output = $this->getStarter()->updateWithStart(
254254
$workflowType,
255255
$workflowStub->getOptions() ?? WorkflowOptions::new(),
256256
$update,
257257
$updateArgs,
258258
$startArgs,
259259
);
260260

261-
$workflowStub->setExecution($execution);
261+
$workflowStub->setExecution($output->execution);
262262

263-
return $handle instanceof \Throwable
264-
? throw $handle
265-
: $handle;
263+
return $output->handle instanceof UpdateHandle
264+
? $output->handle
265+
: throw $output->handle;
266266
}
267267

268268
/**

src/Interceptor/Trait/WorkflowClientCallsInterceptorTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Temporal\Interceptor\Trait;
1313

14-
use Temporal\Client\Update\UpdateHandle;
1514
use Temporal\Client\Workflow\WorkflowExecutionDescription;
1615
use Temporal\DataConverter\EncodedValues;
1716
use Temporal\Interceptor\WorkflowClient\CancelInput;
@@ -25,6 +24,7 @@
2524
use Temporal\Interceptor\WorkflowClient\TerminateInput;
2625
use Temporal\Interceptor\WorkflowClient\UpdateInput;
2726
use Temporal\Interceptor\WorkflowClient\UpdateWithStartInput;
27+
use Temporal\Interceptor\WorkflowClient\UpdateWithStartOutput;
2828
use Temporal\Interceptor\WorkflowClientCallsInterceptor;
2929
use Temporal\Workflow\WorkflowExecution;
3030

@@ -80,7 +80,7 @@ public function signalWithStart(SignalWithStartInput $input, callable $next): Wo
8080
*
8181
* @see WorkflowClientCallsInterceptor::updateWithStart()
8282
*/
83-
public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateHandle
83+
public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateWithStartOutput
8484
{
8585
return $next($input);
8686
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Temporal package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Temporal\Interceptor\WorkflowClient;
13+
14+
use Temporal\Client\Update\UpdateHandle;
15+
use Temporal\Workflow\WorkflowExecution;
16+
17+
final class UpdateWithStartOutput
18+
{
19+
public function __construct(
20+
public readonly WorkflowExecution $execution,
21+
public readonly UpdateHandle|\Throwable $handle,
22+
) {}
23+
}

src/Interceptor/WorkflowClientCallsInterceptor.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Temporal\Interceptor;
1313

14-
use Temporal\Client\Update\UpdateHandle;
1514
use Temporal\Client\Workflow\WorkflowExecutionDescription;
1615
use Temporal\DataConverter\ValuesInterface;
1716
use Temporal\Interceptor\Trait\WorkflowClientCallsInterceptorTrait;
@@ -26,6 +25,7 @@
2625
use Temporal\Interceptor\WorkflowClient\TerminateInput;
2726
use Temporal\Interceptor\WorkflowClient\UpdateInput;
2827
use Temporal\Interceptor\WorkflowClient\UpdateWithStartInput;
28+
use Temporal\Interceptor\WorkflowClient\UpdateWithStartOutput;
2929
use Temporal\Internal\Interceptor\Interceptor;
3030
use Temporal\Workflow\WorkflowExecution;
3131

@@ -74,10 +74,8 @@ public function signalWithStart(SignalWithStartInput $input, callable $next): Wo
7474
/**
7575
* @param UpdateWithStartInput $input
7676
* @param callable(UpdateWithStartInput): WorkflowExecution $next
77-
*
78-
* @return UpdateHandle
7977
*/
80-
public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateHandle;
78+
public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateWithStartOutput;
8179

8280
/**
8381
* @param callable(GetResultInput): ?ValuesInterface $next

src/Internal/Client/WorkflowStarter.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Temporal\Interceptor\WorkflowClient\StartInput;
4242
use Temporal\Interceptor\WorkflowClient\UpdateInput;
4343
use Temporal\Interceptor\WorkflowClient\UpdateWithStartInput;
44+
use Temporal\Interceptor\WorkflowClient\UpdateWithStartOutput;
4445
use Temporal\Interceptor\WorkflowClientCallsInterceptor;
4546
use Temporal\Internal\Interceptor\Pipeline;
4647
use Temporal\Internal\Support\DateInterval;
@@ -128,21 +129,19 @@ function (SignalWithStartInput $input): WorkflowExecution {
128129

129130
/**
130131
* @param non-empty-string $workflowType
131-
*
132-
* @return array{WorkflowExecution, UpdateHandle|\Throwable}
133132
*/
134133
public function updateWithStart(
135134
string $workflowType,
136135
WorkflowOptions $options,
137136
UpdateOptions $update,
138137
array $updateArgs = [],
139138
array $startArgs = [],
140-
): array {
139+
): UpdateWithStartOutput {
141140
$arguments = EncodedValues::fromValues($startArgs, $this->converter);
142141
$updateArguments = EncodedValues::fromValues($updateArgs, $this->converter);
143142

144143
return $this->interceptors->with(
145-
function (UpdateWithStartInput $input): array {
144+
function (UpdateWithStartInput $input): UpdateWithStartOutput {
146145
$startRequest = $this->configureExecutionRequest(
147146
new StartWorkflowExecutionRequest(),
148147
$input->workflowStartInput,
@@ -234,20 +233,23 @@ function (UpdateWithStartInput $input): array {
234233
workflowExecution: $execution,
235234
);
236235
} catch (\RuntimeException $e) {
237-
return [$execution, $e];
236+
return new UpdateWithStartOutput($execution, $e);
238237
}
239238

240-
return [$execution, new UpdateHandle(
241-
client: $this->serviceClient,
242-
clientOptions: $this->clientOptions,
243-
converter: $this->converter,
244-
execution: $updateResult->getReference()->workflowExecution,
245-
workflowType: $input->updateInput->workflowType,
246-
updateName: $input->updateInput->updateName,
247-
resultType: $input->updateInput->resultType,
248-
updateId: $updateResult->getReference()->updateId,
249-
result: $updateResult->getResult(),
250-
)];
239+
return new UpdateWithStartOutput(
240+
$execution,
241+
new UpdateHandle(
242+
client: $this->serviceClient,
243+
clientOptions: $this->clientOptions,
244+
converter: $this->converter,
245+
execution: $updateResult->getReference()->workflowExecution,
246+
workflowType: $input->updateInput->workflowType,
247+
updateName: $input->updateInput->updateName,
248+
resultType: $input->updateInput->resultType,
249+
updateId: $updateResult->getReference()->updateId,
250+
result: $updateResult->getResult(),
251+
),
252+
);
251253
},
252254
/** @see WorkflowClientCallsInterceptor::updateWithStart() */
253255
'updateWithStart',

tests/Fixtures/src/Interceptor/InterceptorCallsCounter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
use Temporal\Interceptor\Trait\WorkflowOutboundRequestInterceptorTrait;
2222
use Temporal\Interceptor\WorkflowClient\SignalWithStartInput;
2323
use Temporal\Interceptor\WorkflowClient\StartInput;
24+
use Temporal\Interceptor\WorkflowClient\UpdateWithStartInput;
25+
use Temporal\Interceptor\WorkflowClient\UpdateWithStartOutput;
2426
use Temporal\Interceptor\WorkflowClientCallsInterceptor;
2527
use Temporal\Interceptor\WorkflowInbound\SignalInput;
28+
use Temporal\Interceptor\WorkflowInbound\UpdateInput;
2629
use Temporal\Interceptor\WorkflowInbound\WorkflowInput;
2730
use Temporal\Interceptor\WorkflowInboundCallsInterceptor;
2831
use Temporal\Interceptor\WorkflowOutboundRequestInterceptor;
@@ -92,4 +95,15 @@ public function signalWithStart(SignalWithStartInput $input, callable $next): Wo
9295
),
9396
);
9497
}
98+
99+
public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateWithStartOutput
100+
{
101+
return $next(
102+
$input->with(
103+
workflowStartInput: $input->workflowStartInput->with(
104+
header: $this->increment($input->workflowStartInput->header, __FUNCTION__),
105+
),
106+
),
107+
);
108+
}
95109
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Temporal package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Temporal\Tests\Workflow\Interceptor;
13+
14+
use Temporal\Workflow;
15+
use Temporal\Workflow\WorkflowMethod;
16+
17+
#[Workflow\WorkflowInterface]
18+
class UpdateHeadersWorkflow
19+
{
20+
private ?array $headers = null;
21+
private bool $updated = false;
22+
23+
#[WorkflowMethod(name: 'InterceptorUpdateHeadersWorkflow')]
24+
public function handler(): mixed
25+
{
26+
yield Workflow::await(fn() => $this->updated);
27+
28+
$this->headers = \iterator_to_array(Workflow::getCurrentContext()->getHeader());
29+
30+
return $this->headers;
31+
}
32+
33+
#[Workflow\UpdateMethod]
34+
public function update(): void
35+
{
36+
$this->updated = true;
37+
}
38+
39+
#[Workflow\QueryMethod]
40+
public function headers(): mixed
41+
{
42+
return $this->headers;
43+
}
44+
}

tests/Functional/Interceptor/InterceptorsTestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
namespace Temporal\Tests\Functional\Interceptor;
1313

1414
use Carbon\CarbonInterval;
15+
use Temporal\Client\Update\LifecycleStage;
16+
use Temporal\Client\Update\UpdateOptions;
1517
use Temporal\Client\WorkflowOptions;
1618
use Temporal\Testing\WithoutTimeSkipping;
1719
use Temporal\Tests\Workflow\Interceptor\HeadersWorkflow;
1820
use Temporal\Tests\Workflow\Interceptor\QueryHeadersWorkflow;
1921
use Temporal\Tests\Workflow\Interceptor\SignalHeadersWorkflow;
22+
use Temporal\Tests\Workflow\Interceptor\UpdateHeadersWorkflow;
2023

2124
/**
2225
* @group client
@@ -111,6 +114,30 @@ public function testSignalWithStartMethod(): void
111114
], (array)$run->getResult());
112115
}
113116

117+
public function testUpdateWithStartMethod(): void
118+
{
119+
$client = $this->createClient();
120+
$workflow = $client->newWorkflowStub(
121+
UpdateHeadersWorkflow::class,
122+
WorkflowOptions::new()
123+
->withWorkflowExecutionTimeout(CarbonInterval::seconds(5)),
124+
);
125+
126+
$run = $client->updateWithStart($workflow, 'update');
127+
128+
// Workflow header
129+
$this->assertEquals([
130+
/** @see \Temporal\Tests\Interceptor\InterceptorCallsCounter::updateWithStart() */
131+
'updateWithStart' => '1',
132+
/**
133+
* Inherited from handler run
134+
*
135+
* @see \Temporal\Tests\Interceptor\InterceptorCallsCounter::execute()
136+
*/
137+
'execute' => '1',
138+
], (array) $workflow->headers());
139+
}
140+
114141
// todo: rewrite tests because there is no header in query call
115142
// todo: add test about dynamic query
116143
// public function testQueryMethod(): void

0 commit comments

Comments
 (0)