Skip to content
Merged
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
6 changes: 0 additions & 6 deletions src/Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\Type\TemplateType;

use function is_array;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
final class ObjectNormalizer implements Normalizer, TypeAwareNormalizer, HydratorAwareNormalizer
{
Expand Down Expand Up @@ -56,10 +54,6 @@ public function denormalize(mixed $value, array $context): object|null
return null;
}

if (!is_array($value)) {
throw InvalidArgument::withWrongType('array<string, mixed>|null', $value);
}

$className = $this->className();

return $this->hydrator->hydrate($className, $value, $context);
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Fixture/ExplicitObjectNormalizerDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

use Patchlevel\Hydrator\Normalizer\ObjectNormalizer;

final class ExplicitObjectNormalizerDto
{
public function __construct(
#[ObjectNormalizer(ProfileId::class)]
public ProfileId $profileId,
) {
}
}
21 changes: 21 additions & 0 deletions tests/Unit/Normalizer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ public function testDenormalizeWithValue(): void
);
}

public function testDenormalizePassesNonArrayValueToHydrator(): void
{
$hydrator = $this->createMock(Hydrator::class);

$expected = ProfileId::fromString('1');

$hydrator->expects($this->once())->method('hydrate')->with(
ProfileId::class,
'1',
)
->willReturn($expected);

$normalizer = new ObjectNormalizer(ProfileId::class);
$normalizer->setHydrator($hydrator);

$this->assertEquals(
$expected,
$normalizer->denormalize('1', []),
);
}

public function testNormalizePassesContextToHydrator(): void
{
$context = ['key' => 'value'];
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/StackHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Patchlevel\Hydrator\Tests\Unit\Fixture\DummyMiddleware;
use Patchlevel\Hydrator\Tests\Unit\Fixture\Email;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ExplicitArrayShapeNormalizerDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ExplicitObjectNormalizerDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithArrayShapeDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithIterablesDto;
Expand Down Expand Up @@ -746,6 +747,30 @@ public function testHydrateClassOfObjectMap(): void
);
}

public function testRoundTripWithExplicitObjectNormalizerAndClassNormalizer(): void
{
$object = new ExplicitObjectNormalizerDto(ProfileId::fromString('1'));

$data = $this->hydrator->extract($object);

self::assertEquals(['profileId' => '1'], $data);
self::assertEquals($object, $this->hydrator->hydrate(ExplicitObjectNormalizerDto::class, $data));
}

public function testHydrateWithExplicitObjectNormalizerAndNonArrayData(): void
{
try {
$this->hydrator->hydrate(
ProfileCreatedWrapper::class,
['event' => 'foo'],
);

self::fail('DenormalizationFailure was not thrown');
} catch (DenormalizationFailure $e) {
self::assertInstanceOf(ArrayDataRequired::class, $e->getPrevious());
}
}

#[RequiresPhp('>=8.4')]
public function testLazyHydrate(): void
{
Expand Down
Loading