Skip to content

Commit

Permalink
fix: Setter does not get called if the property is also in the constr…
Browse files Browse the repository at this point in the history
…uctor
  • Loading branch information
priyadi committed May 1, 2024
1 parent 33a0e48 commit 0dcd851
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/Fixtures/PropertyInSetterAndConstructor/ChildObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\Fixtures\PropertyInSetterAndConstructor;

class ChildObject
{
public function __construct(
private string $a,
) {
}

public function getA(): string
{
return $this->a;
}

public function setA(string $a): void
{
$this->a = $a;
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/PropertyInSetterAndConstructor/ChildObjectDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\Fixtures\PropertyInSetterAndConstructor;

/** @psalm-suppress MissingConstructor */
class ChildObjectDto
{
public string $a;
}
43 changes: 43 additions & 0 deletions tests/Fixtures/PropertyInSetterAndConstructor/ParentObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\Fixtures\PropertyInSetterAndConstructor;

class ParentObject
{
public function __construct(
private string $name,
private ChildObject $child,
) {
}

public function getChild(): ChildObject
{
return $this->child;
}

public function setChild(ChildObject $child): void
{
$this->child = $child;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): void
{
$this->name = $name;
}
}
21 changes: 21 additions & 0 deletions tests/Fixtures/PropertyInSetterAndConstructor/ParentObjectDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\Fixtures\PropertyInSetterAndConstructor;

/** @psalm-suppress MissingConstructor */
class ParentObjectDto
{
public string $name;
public ChildObjectDto $child;
}
50 changes: 50 additions & 0 deletions tests/IntegrationTest/PropertyInSetterAndConstructorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\IntegrationTest;

use Rekalogika\Mapper\Tests\Common\FrameworkTestCase;
use Rekalogika\Mapper\Tests\Fixtures\PropertyInSetterAndConstructor\ChildObject;
use Rekalogika\Mapper\Tests\Fixtures\PropertyInSetterAndConstructor\ChildObjectDto;
use Rekalogika\Mapper\Tests\Fixtures\PropertyInSetterAndConstructor\ParentObject;
use Rekalogika\Mapper\Tests\Fixtures\PropertyInSetterAndConstructor\ParentObjectDto;

class PropertyInSetterAndConstructorTest extends FrameworkTestCase
{
public function testIssue57(): void
{
$dto = new ParentObjectDto();
$dto->name = 'dto-name';
$dto->child = new ChildObjectDto();
$dto->child->a = 'dto-a';

$entity = $this->mapper->map($dto, ParentObject::class);

$this->assertSame('dto-name', $entity->getName());
$this->assertSame('dto-a', $entity->getChild()->getA());
}

public function testIssue57Preinitialized(): void
{
$dto = new ParentObjectDto();
$dto->name = 'dto-name';
$dto->child = new ChildObjectDto();
$dto->child->a = 'dto-a';

$entity = new ParentObject('entity-name', new ChildObject('entity-a'));
$this->mapper->map($dto, $entity);

$this->assertSame('dto-name', $entity->getName());
$this->assertSame('dto-a', $entity->getChild()->getA());
}
}

0 comments on commit 0dcd851

Please sign in to comment.