From 6e1067e9d7f9d39af4e58d29e48e36f550573250 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Mon, 1 Aug 2022 15:37:14 +0200 Subject: [PATCH] Enum as a input for workflow (#216) * Enum as a input for workflow * Skip test for lower PHP version * Supress enum warning Co-authored-by: Sergey Zhuk --- src/DataConverter/JsonConverter.php | 9 ++-- src/DataConverter/Type.php | 5 ++ .../Fixtures/src/Activity/SimpleActivity.php | 14 +++++ .../src/Workflow/ScalarEnumWorkflow.php | 38 +++++++++++++ .../src/Workflow/SimpleEnumWorkflow.php | 38 +++++++++++++ tests/Functional/Client/EnumTestCase.php | 53 +++++++++++++++++++ 6 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 tests/Fixtures/src/Workflow/ScalarEnumWorkflow.php create mode 100644 tests/Fixtures/src/Workflow/SimpleEnumWorkflow.php create mode 100644 tests/Functional/Client/EnumTestCase.php diff --git a/src/DataConverter/JsonConverter.php b/src/DataConverter/JsonConverter.php index d0c4bac7..638382a8 100644 --- a/src/DataConverter/JsonConverter.php +++ b/src/DataConverter/JsonConverter.php @@ -137,9 +137,12 @@ public function fromPayload(Payload $payload, Type $type) if ((\is_object($data) || \is_array($data)) && $type->isClass()) { try { - $instance = (new \ReflectionClass($type->getName())) - ->newInstanceWithoutConstructor() - ; + $reflection = new \ReflectionClass($type->getName()); + if (PHP_VERSION_ID >= 80104 && $reflection->isEnum()) { + return $reflection->getConstant($data->name); + } + + $instance = $reflection->newInstanceWithoutConstructor(); } catch (\ReflectionException $e) { throw new DataConverterException($e->getMessage(), $e->getCode(), $e); } diff --git a/src/DataConverter/Type.php b/src/DataConverter/Type.php index 162bf44b..250022f0 100644 --- a/src/DataConverter/Type.php +++ b/src/DataConverter/Type.php @@ -101,6 +101,11 @@ public static function fromReflectionType(\ReflectionType $type): self if ($type instanceof \ReflectionNamedType) { $name = $type->getName(); + /** @psalm-suppress UndefinedClass */ + if (PHP_VERSION_ID >= 80104 && is_subclass_of($name, \UnitEnum::class)) { + return new self($type->getName(), true); + } + // Traversable types (i.e. Generator) not allowed if (!$name instanceof \Traversable && $name !== 'array' && $name !== 'iterable') { return new self($type->getName(), $type->allowsNull()); diff --git a/tests/Fixtures/src/Activity/SimpleActivity.php b/tests/Fixtures/src/Activity/SimpleActivity.php index 7bee38a6..8b327683 100644 --- a/tests/Fixtures/src/Activity/SimpleActivity.php +++ b/tests/Fixtures/src/Activity/SimpleActivity.php @@ -18,6 +18,8 @@ use Temporal\DataConverter\Bytes; use Temporal\Tests\DTO\Message; use Temporal\Tests\DTO\User; +use Temporal\Tests\Unit\DTO\Enum\SimpleEnum; +use Temporal\Tests\Unit\DTO\Enum\ScalarEnum; #[ActivityInterface(prefix: "SimpleActivity.")] class SimpleActivity @@ -99,4 +101,16 @@ public function fail() { throw new \Error("failed activity"); } + + #[ActivityMethod] + public function simpleEnum(SimpleEnum $enum): SimpleEnum + { + return $enum; + } + + #[ActivityMethod] + public function scalarEnum(ScalarEnum $enum): ScalarEnum + { + return $enum; + } } diff --git a/tests/Fixtures/src/Workflow/ScalarEnumWorkflow.php b/tests/Fixtures/src/Workflow/ScalarEnumWorkflow.php new file mode 100644 index 00000000..9e4100ff --- /dev/null +++ b/tests/Fixtures/src/Workflow/ScalarEnumWorkflow.php @@ -0,0 +1,38 @@ +withStartToCloseTimeout(5) + ->withRetryOptions( + RetryOptions::new()->withMaximumAttempts(2) + ) + ); + + return yield $simple->scalarEnum($enum); + } +} diff --git a/tests/Fixtures/src/Workflow/SimpleEnumWorkflow.php b/tests/Fixtures/src/Workflow/SimpleEnumWorkflow.php new file mode 100644 index 00000000..a5ecec1b --- /dev/null +++ b/tests/Fixtures/src/Workflow/SimpleEnumWorkflow.php @@ -0,0 +1,38 @@ +withStartToCloseTimeout(5) + ->withRetryOptions( + RetryOptions::new()->withMaximumAttempts(2) + ) + ); + + return yield $simple->simpleEnum($enum); + } +} diff --git a/tests/Functional/Client/EnumTestCase.php b/tests/Functional/Client/EnumTestCase.php new file mode 100644 index 00000000..574f4083 --- /dev/null +++ b/tests/Functional/Client/EnumTestCase.php @@ -0,0 +1,53 @@ +markTestSkipped(); + } + } + + public function testSimpleEnum(): void + { + $client = $this->createClient(); + $workflow = $client->newWorkflowStub(SimpleEnumWorkflow::class); + + $result = $client->start($workflow, SimpleEnum::TEST); + + $this->assertSame(SimpleEnum::TEST, $result->getResult(SimpleEnum::class)); + } + + public function testScalarEnum(): void + { + $client = $this->createClient(); + $workflow = $client->newWorkflowStub(ScalarEnumWorkflow::class); + + $result = $client->start($workflow, ScalarEnum::TESTED_ENUM); + + $this->assertSame(ScalarEnum::TESTED_ENUM, $result->getResult(ScalarEnum::class)); + } +}