diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml index b1600b38..94972a61 100644 --- a/.github/workflows/code-style.yml +++ b/.github/workflows/code-style.yml @@ -10,7 +10,7 @@ jobs: strategy: fail-fast: false matrix: - php: [8.2] + php: [8.3] os: [ubuntu-latest] steps: - name: Set up PHP ${{ matrix.php }} diff --git a/src/Internal/Client/WorkflowStarter.php b/src/Internal/Client/WorkflowStarter.php index a5da4a58..99232558 100644 --- a/src/Internal/Client/WorkflowStarter.php +++ b/src/Internal/Client/WorkflowStarter.php @@ -191,7 +191,6 @@ private function configureExecutionRequest( ->setTaskQueue(new TaskQueue(['name' => $options->taskQueue])) ->setWorkflowType(new WorkflowType(['name' => $input->workflowType])) ->setWorkflowId($input->workflowId) - ->setWorkflowStartDelay(DateInterval::toDuration($options->workflowStartDelay)) ->setCronSchedule($options->cronSchedule ?? '') ->setRetryPolicy($options->retryOptions ? $options->retryOptions->toWorkflowRetryPolicy() : null) ->setWorkflowIdReusePolicy($options->workflowIdReusePolicy) @@ -202,6 +201,11 @@ private function configureExecutionRequest( ->setSearchAttributes($options->toSearchAttributes($this->converter)) ->setHeader($header->toHeader()); + $delay = DateInterval::toDuration($options->workflowStartDelay); + if ($delay !== null && ($delay->getSeconds() > 0 || $delay->getNanos() > 0)) { + $req->setWorkflowStartDelay($delay); + } + if ($req instanceof StartWorkflowExecutionRequest) { $req->setRequestEagerExecution($options->eagerStart); } diff --git a/tests/Unit/Internal/Client/WorkflowStarterTestCase.php b/tests/Unit/Internal/Client/WorkflowStarterTestCase.php new file mode 100644 index 00000000..734bdcd9 --- /dev/null +++ b/tests/Unit/Internal/Client/WorkflowStarterTestCase.php @@ -0,0 +1,111 @@ +startRequest('test-workflow', $options); + + self::assertNull($request->getWorkflowStartDelay()); + } + + public function testDelayIsNullIfEmpty(): void + { + $options = (new WorkflowOptions())->withWorkflowStartDelay(0); + + $request = $this->startRequest('test-workflow', $options); + + self::assertNull($request->getWorkflowStartDelay()); + } + + public function testDelayIfSpecifiedSeconds(): void + { + $options = (new WorkflowOptions())->withWorkflowStartDelay(10); + + $request = $this->startRequest('test-workflow', $options); + + self::assertNotNull($request->getWorkflowStartDelay()); + self::assertSame(10, $request->getWorkflowStartDelay()->getSeconds()); + self::assertSame(0, $request->getWorkflowStartDelay()->getNanos()); + } + + public function testDelayIfSpecifiedNanos(): void + { + $options = (new WorkflowOptions()) + ->withWorkflowStartDelay(DateInterval::parse(42, DateInterval::FORMAT_MICROSECONDS)); + + $request = $this->startRequest('test-workflow', $options); + + self::assertNotNull($request->getWorkflowStartDelay()); + self::assertSame(0, $request->getWorkflowStartDelay()->getSeconds()); + self::assertSame(42000, $request->getWorkflowStartDelay()->getNanos()); + } + + private function startRequest( + string $workflowType, + WorkflowOptions $options, + array $args = [], + ): StartWorkflowExecutionRequest { + $clientOptions = (new \Temporal\Client\ClientOptions()) + ->withNamespace(self::NAMESPACE) + ->withIdentity(self::IDENTITY); + + $result = null; + $clientMock = $this->createMock(ServiceClientInterface::class); + $clientMock + ->expects($this->once()) + ->method('StartWorkflowExecution') + ->with( + $this->callback(static function (StartWorkflowExecutionRequest $request) use (&$result) { + $result = $request; + return true; + }) + )->willReturn( + (new StartWorkflowExecutionResponse()) + ->setRunId(self::WORKFLOW_RUN_ID) + ); + + $starter = new WorkflowStarter( + serviceClient: $clientMock, + converter: DataConverter::createDefault(), + clientOptions: $clientOptions, + interceptors: Pipeline::prepare([]), + ); + + $execution = $starter->start($workflowType, $options, $args); + + if (self::WORKFLOW_RUN_ID !== $execution->getRunID()) { + $this->fail('Unexpected workflow run ID.'); + } + + return $result; + } +}