Skip to content

Commit

Permalink
Convert datetime SA from client request into RFC3339 string; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 14, 2025
1 parent 37247c5 commit ec88dde
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/DataConverter/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function toPayload($value): ?Payload
$value = match (true) {
$value instanceof \stdClass => $value,
$value instanceof UuidInterface => $value->toString(),
$value instanceof \DateTimeInterface => $value->format(\DateTimeInterface::RFC3339),
default => $this->marshaller->marshal($value),
};
}
Expand Down Expand Up @@ -133,6 +134,11 @@ public function fromPayload(Payload $payload, Type $type)
}

return Uuid::fromString($data);

case \DateTimeImmutable::class:
case \DateTimeInterface::class:
\is_string($data) or throw $this->errorInvalidType($type, $data);
return new \DateTimeImmutable($data);
case Type::TYPE_OBJECT:
if (!\is_object($data)) {
throw $this->errorInvalidType($type, $data);
Expand Down
3 changes: 3 additions & 0 deletions tests/Acceptance/App/Runtime/TemporalStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ public function start(): void
$this->environment->startTemporalServer(parameters: [
'--search-attribute', 'foo=text',
'--search-attribute', 'bar=int',
'--search-attribute', 'testBool=bool',
'--search-attribute', 'testInt=int',
'--search-attribute', 'testFloat=double',
'--search-attribute', 'testString=text',
'--search-attribute', 'testKeyword=keyword',
'--search-attribute', 'testKeywordList=keywordList',
'--search-attribute', 'testDatetime=datetime',
]);
$this->started = true;
}
Expand Down
37 changes: 35 additions & 2 deletions tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function testStartWithTypedSearchAttributes(
->withTypedSearchAttributes(
TypedSearchAttributes::empty()
->withValue(SearchAttributeKey::forFloat('testFloat'), 1.1)
->withValue(SearchAttributeKey::forInteger('testInt'), -2)
->withValue(SearchAttributeKey::forBool('testBool'), false)
->withValue(SearchAttributeKey::forString('testString'), 'foo')
->withValue(SearchAttributeKey::forKeyword('testKeyword'), 'bar')
->withValue(SearchAttributeKey::forKeywordList('testKeywordList'), ['baz'])
->withValue(
SearchAttributeKey::forDatetime('testDatetime'),
new \DateTimeImmutable('2019-01-01T00:00:00Z'),
)
),
);

Expand All @@ -42,7 +51,16 @@ public function testStartWithTypedSearchAttributes(
$stub->signal('exit');
$result = $stub->getResult();

$this->assertSame(['testFloat' => 1.1], (array)$result);
$this->assertEquals([
'testBool' => false,
'testInt' => -2,
'testFloat' => 1.1,
'testString' => 'foo',
'testKeyword' => 'bar',
'testKeywordList' => ['baz'],
'testDatetime' => (new \DateTimeImmutable('2019-01-01T00:00:00Z'))
->format(\DateTimeInterface::RFC3339),
], (array)$result);
}

#[Test]
Expand All @@ -54,11 +72,26 @@ public function testUpsertTypedSearchAttributes(
'Extra_Workflow_TypedSearchAttributes',
WorkflowOptions::new()
->withTaskQueue($feature->taskQueue)
->withSearchAttributes(['testFloat' => 1.1])
->withSearchAttributes([
'testBool' => false,
'testInt' => -2,
'testFloat' => 1.1,
'testString' => 'foo',
'testKeyword' => 'bar',
'testKeywordList' => ['baz'],
'testDatetime' => (new \DateTimeImmutable('2019-01-01T00:00:00Z'))
->format(\DateTimeInterface::RFC3339),
])
);

$toSend = [
'testBool' => true,
'testInt' => 42,
'testFloat' => 3.25,
'testString' => 'foo bar baz',
'testKeyword' => 'foo-bar-baz',
'testKeywordList' => ['foo', 'bar', 'baz'],
'testDatetime' => (new \DateTimeImmutable('2021-01-01T00:00:00Z'))->format(\DateTimeInterface::RFC3339),
];

/** @see TestWorkflow::handle() */
Expand Down

0 comments on commit ec88dde

Please sign in to comment.