Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Reflection/Types/PropertyTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static function create(RuntimeReflectionProperty $property)

return new TransformableType(
$type,
$isScalar,
$isNullable,
);
}
}
25 changes: 25 additions & 0 deletions tests/Integration/ClassTransformerFromArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Tests\Integration\DTO\UserEmptyTypeDTO;
use ClassTransformer\Exceptions\ClassNotFoundException;

use Tests\Integration\DTO\UserNullableArrayDTO;
use function count;

/**
Expand Down Expand Up @@ -221,4 +222,28 @@ public function testEmptyTypeObject(): void
self::assertEquals($data['email'], $userDTO->email);
self::assertEquals($data['balance'], $userDTO->balance);
}

public function testNullableTransformableProperty(): void
{
$data = [
'user' => null,
];

$userNullableDTO = Hydrator::init()->create(UserNullableArrayDTO::class, $data);

self::assertInstanceOf(UserNullableArrayDTO::class, $userNullableDTO);
self::assertNull($userNullableDTO->user);
}

public function testNotNullableTransformableProperty(): void
{
$data = [
'user' => $this->getBaseArrayData(),
];

$userNullableDTO = Hydrator::init()->create(UserNullableArrayDTO::class, $data);

self::assertInstanceOf(UserNullableArrayDTO::class, $userNullableDTO);
self::assertInstanceOf(UserDTO::class, $userNullableDTO->user);
}
}
9 changes: 9 additions & 0 deletions tests/Integration/DTO/UserNullableArrayDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Tests\Integration\DTO;

class UserNullableArrayDTO
{
public ?UserDTO $user;
}
2 changes: 1 addition & 1 deletion tests/Units/DTO/TypesDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class TypesDto
public ?string $emptyString;
public ?float $nullableFloat;
public ?bool $nullableBool;

public ?UserDTO $nullableObject;
}
4 changes: 4 additions & 0 deletions tests/Units/ValueCastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public function testCreateProperty(): void
$value = $caster->castAttribute(null);
$this->assertNull($value);

$caster = new ValueCasting(new RuntimeReflectionProperty(new \ReflectionProperty(TypesDto::class, 'nullableObject')));
$value = $caster->castAttribute(null);
$this->assertNull($value);

}

public function testCreateArrayProperty(): void
Expand Down