Skip to content

Commit

Permalink
Test for constructor with default values as objects
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMeshok committed Sep 13, 2024
1 parent c3a8fb1 commit 8044780
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use AutoMapper\Tests\Fixtures\ClassWithNullablePropertyInConstructor;
use AutoMapper\Tests\Fixtures\ClassWithPrivateProperty;
use AutoMapper\Tests\Fixtures\ConstructorWithDefaultValues;
use AutoMapper\Tests\Fixtures\ConstructorWithDefaultValuesAsObjects;
use AutoMapper\Tests\Fixtures\DifferentSetterGetterType;
use AutoMapper\Tests\Fixtures\Dog;
use AutoMapper\Tests\Fixtures\Fish;
Expand Down Expand Up @@ -561,6 +562,27 @@ public function testConstructorWithDefault(): void
self::assertSame(30, $userDto->getAge());
}

public function testConstructorWithDefaultsAsObjects(): void
{
$data = ['baz' => 'baz'];
/** @var ConstructorWithDefaultValuesAsObjects $object */
$object = $this->autoMapper->map($data, ConstructorWithDefaultValuesAsObjects::class);

self::assertInstanceOf(ConstructorWithDefaultValuesAsObjects::class, $object);
self::assertInstanceOf(\DateTimeImmutable::class, $object->date);
self::assertInstanceOf(\Fixtures\IntDTO::class, $object->IntDTO);
self::assertSame('baz', $object->baz);

$stdClassData = (object) $data;
/** @var ConstructorWithDefaultValuesAsObjects $object */
$object = $this->autoMapper->map($stdClassData, ConstructorWithDefaultValuesAsObjects::class);

self::assertInstanceOf(ConstructorWithDefaultValuesAsObjects::class, $object);
self::assertInstanceOf(\DateTimeImmutable::class, $object->date);
self::assertInstanceOf(\Fixtures\IntDTO::class, $object->IntDTO);
self::assertSame('baz', $object->baz);
}

public function testConstructorDisable(): void
{
$this->buildAutoMapper(mapPrivatePropertiesAndMethod: true);
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/ConstructorWithDefaultValuesAsObjects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures;

readonly class ConstructorWithDefaultValuesAsObjects
{
public function __construct(
public string $baz,
public IntDTO $IntDTO = new IntDTO(1),
public \DateTimeImmutable $date = new \DateTimeImmutable,
) {
}
}

0 comments on commit 8044780

Please sign in to comment.