Skip to content

Commit

Permalink
Fix supporting for ?array type in DTOs without attributes (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Dec 1, 2022
2 parents 2fb1077 + 5054f74 commit 1d07158
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 59 deletions.
4 changes: 4 additions & 0 deletions src/Internal/Marshaller/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/src/Activity/SimpleActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/Fixtures/src/Workflow/ScalarEnumWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/Fixtures/src/Workflow/SimpleEnumWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 5 additions & 12 deletions tests/Functional/Client/EnumTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
32 changes: 0 additions & 32 deletions tests/Unit/DTO/Enum/EnumTestCase.php

This file was deleted.

2 changes: 0 additions & 2 deletions tests/Unit/DTO/Readonly/ReadonlyPropertiesDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Temporal\Tests\Unit\DTO\Readonly;

use Temporal\Tests\Unit\DTO\Enum\ScalarEnum;

class ReadonlyPropertiesDTO
{
readonly public string $propertiesString;
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/DTO/Type/ArrayType/ArrayDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Type\ArrayType;

use Temporal\Internal\Marshaller\Meta\MarshalArray;

class ArrayDTO
{
#[MarshalArray(name: 'foo')]
public array $foo;

#[MarshalArray(name: 'bar')]
public ?array $bar;

#[MarshalArray(name: 'baz')]
public ?array $baz;

public array $autoArray;

public ?array $nullableFoo;

public ?array $nullableBar;
}
57 changes: 57 additions & 0 deletions tests/Unit/DTO/Type/ArrayType/ArrayTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Type\ArrayType;

use Temporal\Tests\Unit\DTO\DTOMarshallingTestCase;

class ArrayTestCase extends DTOMarshallingTestCase
{
public function testMarshalling(): void
{
$dto = new ArrayDTO();
$dto->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,4 +21,8 @@ class EnumDTO

#[Marshal(name: 'scalarEnum', type: EnumType::class, of: ScalarEnum::class)]
public ScalarEnum $scalarEnum;

public SimpleEnum $autoSimpleEnum;

public ScalarEnum $autoScalarEnum;
}
38 changes: 38 additions & 0 deletions tests/Unit/DTO/Type/EnumType/EnumTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Type\EnumType;

use Temporal\Tests\Unit\DTO\DTOMarshallingTestCase;

/**
* @requires PHP >= 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Enum;
namespace Temporal\Tests\Unit\DTO\Type\EnumType;

enum ScalarEnum: string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Enum;
namespace Temporal\Tests\Unit\DTO\Type\EnumType;

enum SimpleEnum
{
Expand Down

0 comments on commit 1d07158

Please sign in to comment.