|
| 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