diff --git a/src/Internal/Marshaller/Type/ArrayType.php b/src/Internal/Marshaller/Type/ArrayType.php index bbdb4b22..5855485a 100644 --- a/src/Internal/Marshaller/Type/ArrayType.php +++ b/src/Internal/Marshaller/Type/ArrayType.php @@ -58,6 +58,10 @@ public static function match(\ReflectionNamedType $type): bool */ public function parse($value, $current) { + if ($value === null) { + return null; + } + if (!\is_array($value)) { throw new \InvalidArgumentException(\sprintf(self::ERROR_INVALID_TYPE, \get_debug_type($value))); } diff --git a/tests/Fixtures/src/Activity/SimpleActivity.php b/tests/Fixtures/src/Activity/SimpleActivity.php index 8b327683..729cada2 100644 --- a/tests/Fixtures/src/Activity/SimpleActivity.php +++ b/tests/Fixtures/src/Activity/SimpleActivity.php @@ -18,8 +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; +use Temporal\Tests\Unit\DTO\Type\EnumType\ScalarEnum; +use Temporal\Tests\Unit\DTO\Type\EnumType\SimpleEnum; #[ActivityInterface(prefix: "SimpleActivity.")] class SimpleActivity diff --git a/tests/Fixtures/src/Workflow/ScalarEnumWorkflow.php b/tests/Fixtures/src/Workflow/ScalarEnumWorkflow.php index 9e4100ff..98404bb0 100644 --- a/tests/Fixtures/src/Workflow/ScalarEnumWorkflow.php +++ b/tests/Fixtures/src/Workflow/ScalarEnumWorkflow.php @@ -11,12 +11,12 @@ namespace Temporal\Tests\Workflow; -use Temporal\Workflow\WorkflowMethod; -use Temporal\Workflow; -use Temporal\Tests\Activity\SimpleActivity; use Temporal\Activity\ActivityOptions; use Temporal\Common\RetryOptions; -use Temporal\Tests\Unit\DTO\Enum\ScalarEnum; +use Temporal\Tests\Activity\SimpleActivity; +use Temporal\Tests\Unit\DTO\Type\EnumType\ScalarEnum; +use Temporal\Workflow; +use Temporal\Workflow\WorkflowMethod; #[Workflow\WorkflowInterface] class ScalarEnumWorkflow diff --git a/tests/Fixtures/src/Workflow/SimpleEnumWorkflow.php b/tests/Fixtures/src/Workflow/SimpleEnumWorkflow.php index a5ecec1b..e078adcf 100644 --- a/tests/Fixtures/src/Workflow/SimpleEnumWorkflow.php +++ b/tests/Fixtures/src/Workflow/SimpleEnumWorkflow.php @@ -11,12 +11,12 @@ namespace Temporal\Tests\Workflow; -use Temporal\Workflow\WorkflowMethod; -use Temporal\Workflow; -use Temporal\Tests\Activity\SimpleActivity; use Temporal\Activity\ActivityOptions; use Temporal\Common\RetryOptions; -use Temporal\Tests\Unit\DTO\Enum\SimpleEnum; +use Temporal\Tests\Activity\SimpleActivity; +use Temporal\Tests\Unit\DTO\Type\EnumType\SimpleEnum; +use Temporal\Workflow; +use Temporal\Workflow\WorkflowMethod; #[Workflow\WorkflowInterface] class SimpleEnumWorkflow diff --git a/tests/Functional/Client/EnumTestCase.php b/tests/Functional/Client/EnumTestCase.php index 574f4083..39f9d7c2 100644 --- a/tests/Functional/Client/EnumTestCase.php +++ b/tests/Functional/Client/EnumTestCase.php @@ -11,26 +11,19 @@ namespace Temporal\Tests\Functional\Client; -use Temporal\Tests\Workflow\SimpleEnumWorkflow; -use Temporal\Tests\Unit\DTO\Enum\SimpleEnum; +use Temporal\Tests\Unit\DTO\Type\EnumType\ScalarEnum; +use Temporal\Tests\Unit\DTO\Type\EnumType\SimpleEnum; use Temporal\Tests\Workflow\ScalarEnumWorkflow; -use Temporal\Tests\Unit\DTO\Enum\ScalarEnum; +use Temporal\Tests\Workflow\SimpleEnumWorkflow; /** * @group client * @group functional + * + * @requires PHP >= 8.1 */ class EnumTestCase extends ClientTestCase { - public function setUp(): void - { - parent::setUp(); - - if (PHP_VERSION_ID < 80104) { - $this->markTestSkipped(); - } - } - public function testSimpleEnum(): void { $client = $this->createClient(); diff --git a/tests/Unit/DTO/Enum/EnumTestCase.php b/tests/Unit/DTO/Enum/EnumTestCase.php deleted file mode 100644 index 63e07984..00000000 --- a/tests/Unit/DTO/Enum/EnumTestCase.php +++ /dev/null @@ -1,32 +0,0 @@ -markTestSkipped(); - } - - $dto = new EnumDTO(); - $dto->simpleEnum = SimpleEnum::TEST; - $dto->scalarEnum = ScalarEnum::TESTED_ENUM; - - $result = $this->marshal($dto); - $this->assertEquals($dto->simpleEnum, $result['simpleEnum']); - $this->assertEquals($dto->scalarEnum, $result['scalarEnum']); - } -} diff --git a/tests/Unit/DTO/Readonly/ReadonlyPropertiesDTO.php b/tests/Unit/DTO/Readonly/ReadonlyPropertiesDTO.php index fe106110..7301b4b9 100644 --- a/tests/Unit/DTO/Readonly/ReadonlyPropertiesDTO.php +++ b/tests/Unit/DTO/Readonly/ReadonlyPropertiesDTO.php @@ -11,8 +11,6 @@ namespace Temporal\Tests\Unit\DTO\Readonly; -use Temporal\Tests\Unit\DTO\Enum\ScalarEnum; - class ReadonlyPropertiesDTO { readonly public string $propertiesString; diff --git a/tests/Unit/DTO/Type/ArrayType/ArrayDTO.php b/tests/Unit/DTO/Type/ArrayType/ArrayDTO.php new file mode 100644 index 00000000..bbc27ab2 --- /dev/null +++ b/tests/Unit/DTO/Type/ArrayType/ArrayDTO.php @@ -0,0 +1,32 @@ +foo = ['foo']; + $dto->bar = ['bar']; + $dto->baz = null; + $dto->autoArray = ['foo']; + $dto->nullableFoo = ['bar']; + $dto->nullableBar = null; + + $result = $this->marshal($dto); + $this->assertSame([ + 'foo' => ['foo'], + 'bar' => ['bar'], + 'baz' => null, + 'autoArray' => ['foo'], + 'nullableFoo' => ['bar'], + 'nullableBar' => null, + ], $result); + } + + public function testUnmarshalling(): void + { + $dto = $this->unmarshal([ + 'foo' => ['foo'], + 'bar' => ['bar'], + 'baz' => null, + 'autoArray' => ['foo'], + 'nullableFoo' => ['bar'], + 'nullableBar' => null, + ], new ArrayDTO()); + + $this->assertSame(['foo'], $dto->foo); + $this->assertSame(['bar'], $dto->bar); + $this->assertSame(null, $dto->baz); + $this->assertSame(['foo'], $dto->autoArray); + $this->assertSame(['bar'], $dto->nullableFoo); + $this->assertSame(null, $dto->nullableBar); + } +} diff --git a/tests/Unit/DTO/Enum/EnumDTO.php b/tests/Unit/DTO/Type/EnumType/EnumDTO.php similarity index 81% rename from tests/Unit/DTO/Enum/EnumDTO.php rename to tests/Unit/DTO/Type/EnumType/EnumDTO.php index 6de9ef62..ed32b152 100644 --- a/tests/Unit/DTO/Enum/EnumDTO.php +++ b/tests/Unit/DTO/Type/EnumType/EnumDTO.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Temporal\Tests\Unit\DTO\Enum; +namespace Temporal\Tests\Unit\DTO\Type\EnumType; use Temporal\Internal\Marshaller\Meta\Marshal; use Temporal\Internal\Marshaller\Type\EnumType; @@ -21,4 +21,8 @@ class EnumDTO #[Marshal(name: 'scalarEnum', type: EnumType::class, of: ScalarEnum::class)] public ScalarEnum $scalarEnum; + + public SimpleEnum $autoSimpleEnum; + + public ScalarEnum $autoScalarEnum; } diff --git a/tests/Unit/DTO/Type/EnumType/EnumTestCase.php b/tests/Unit/DTO/Type/EnumType/EnumTestCase.php new file mode 100644 index 00000000..05210a46 --- /dev/null +++ b/tests/Unit/DTO/Type/EnumType/EnumTestCase.php @@ -0,0 +1,38 @@ += 8.1 + */ +class EnumTestCase extends DTOMarshallingTestCase +{ + public function testUnmarshalBackedEnum(): void + { + $dto = $this->unmarshal([ + 'scalarEnum' => ScalarEnum::TESTED_ENUM->value, + ], new EnumDTO()); + + $this->assertSame(ScalarEnum::TESTED_ENUM, $dto->scalarEnum); + } + + public function testUnmarshalNonBackedEnum(): void + { + $this->expectError(); + + $this->unmarshal([ + 'simpleEnum' => SimpleEnum::TEST->name, + ], new EnumDTO()); + } +} diff --git a/tests/Unit/DTO/Enum/ScalarEnum.php b/tests/Unit/DTO/Type/EnumType/ScalarEnum.php similarity index 84% rename from tests/Unit/DTO/Enum/ScalarEnum.php rename to tests/Unit/DTO/Type/EnumType/ScalarEnum.php index 60ee9c97..9df53b3f 100644 --- a/tests/Unit/DTO/Enum/ScalarEnum.php +++ b/tests/Unit/DTO/Type/EnumType/ScalarEnum.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Temporal\Tests\Unit\DTO\Enum; +namespace Temporal\Tests\Unit\DTO\Type\EnumType; enum ScalarEnum: string { diff --git a/tests/Unit/DTO/Enum/SimpleEnum.php b/tests/Unit/DTO/Type/EnumType/SimpleEnum.php similarity index 83% rename from tests/Unit/DTO/Enum/SimpleEnum.php rename to tests/Unit/DTO/Type/EnumType/SimpleEnum.php index b1c20de7..2b3fb260 100644 --- a/tests/Unit/DTO/Enum/SimpleEnum.php +++ b/tests/Unit/DTO/Type/EnumType/SimpleEnum.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Temporal\Tests\Unit\DTO\Enum; +namespace Temporal\Tests\Unit\DTO\Type\EnumType; enum SimpleEnum {