Skip to content

Commit 62a0128

Browse files
authored
Add ChildWorkflow id test (#617)
1 parent 0c7b97e commit 62a0128

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

src/Workflow/WorkflowExecution.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class WorkflowExecution
2323
{
2424
/**
25+
* @var non-empty-string
2526
* @psalm-readonly
2627
* @psalm-allow-private-mutation
2728
*/
@@ -30,6 +31,7 @@ class WorkflowExecution
3031
private string $id;
3132

3233
/**
34+
* @var non-empty-string|null
3335
* @psalm-readonly
3436
* @psalm-allow-private-mutation
3537
*/
@@ -43,11 +45,17 @@ public function __construct(?string $id = null, ?string $runId = null)
4345
$this->runId = $runId;
4446
}
4547

48+
/**
49+
* @return non-empty-string
50+
*/
4651
public function getID(): string
4752
{
4853
return $this->id;
4954
}
5055

56+
/**
57+
* @return non-empty-string|null
58+
*/
5159
public function getRunID(): ?string
5260
{
5361
return $this->runId;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Tests\Acceptance\Extra\Workflow\ChildWorkflowId;
6+
7+
use PHPUnit\Framework\Attributes\CoversFunction;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use Temporal\Client\WorkflowClientInterface;
10+
use Temporal\Client\WorkflowStubInterface;
11+
use Temporal\Tests\Acceptance\App\Attribute\Stub;
12+
use Temporal\Tests\Acceptance\App\TestCase;
13+
use Temporal\Workflow;
14+
use Temporal\Workflow\WorkflowInterface;
15+
use Temporal\Workflow\WorkflowMethod;
16+
17+
#[CoversFunction('Temporal\Internal\Workflow\Process\Process::logRunningHandlers')]
18+
class ChildWorkflowIdTest extends TestCase
19+
{
20+
#[Test]
21+
public function updateHandlersWithOneCall(
22+
#[Stub('Extra_Workflow_ChildWorkflowId', args: [true])] WorkflowStubInterface $stub,
23+
WorkflowClientInterface $client,
24+
): void {
25+
$deadline = \microtime(true) + 5.0; // 5-second timeout
26+
do {
27+
$childId = $stub->query('getChildId')->getValue(0);
28+
if ($childId !== null) {
29+
break;
30+
}
31+
} while (\microtime(true) < $deadline);
32+
33+
$childId ?? $this->fail('Child workflow not started.');
34+
35+
// Get child workflow stub
36+
$child = $client->newRunningWorkflowStub(TestWorkflow::class, $childId);
37+
38+
$this->assertSame($stub->getExecution()->getID(), $child->getParentId());
39+
}
40+
}
41+
42+
#[WorkflowInterface]
43+
class TestWorkflow
44+
{
45+
/** @var non-empty-string|null */
46+
private ?string $childId = null;
47+
48+
private bool $exit = false;
49+
50+
#[WorkflowMethod(name: "Extra_Workflow_ChildWorkflowId")]
51+
public function handle(bool $createChild = false)
52+
{
53+
// Start a child workflow and store its ID
54+
if ($createChild) {
55+
$child = Workflow::newUntypedChildWorkflowStub("Extra_Workflow_ChildWorkflowId");
56+
$result = yield $child->start(false);
57+
$this->childId = $result->getID();
58+
}
59+
60+
yield Workflow::await(
61+
fn(): bool => $this->exit,
62+
);
63+
}
64+
65+
/**
66+
* @return null|non-empty-string
67+
*/
68+
#[Workflow\QueryMethod]
69+
public function getChildId(): ?string
70+
{
71+
return $this->childId;
72+
}
73+
74+
/**
75+
* @return null|non-empty-string
76+
*/
77+
#[Workflow\QueryMethod]
78+
public function getParentId(): ?string
79+
{
80+
return Workflow::getInfo()->parentExecution?->getID();
81+
}
82+
83+
#[Workflow\SignalMethod]
84+
public function exit(): void
85+
{
86+
$this->exit = true;
87+
}
88+
}

0 commit comments

Comments
 (0)