Skip to content

Commit

Permalink
fix: handle case where class target has adder and remover AND constru…
Browse files Browse the repository at this point in the history
…ctor arguments
  • Loading branch information
wuchen90 committed Dec 6, 2024
1 parent dbaaa77 commit 09bb409
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Transformer/AbstractArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
/* Get the transform statements for the source property */
[$output, $itemStatements] = $this->itemTransformer->transform($loopValueVar, $target, $propertyMapping, $uniqueVariableScope, $source);

if ($propertyMapping->target->writeMutator && $propertyMapping->target->writeMutator->type === WriteMutator::TYPE_ADDER_AND_REMOVER) {
if (null === $propertyMapping->target->parameterInConstructor && $propertyMapping->target->writeMutator && $propertyMapping->target->writeMutator->type === WriteMutator::TYPE_ADDER_AND_REMOVER) {
/**
* Use add and remove methods.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use AutoMapper\Tests\Fixtures\ObjectWithDateTime;
use AutoMapper\Tests\Fixtures\Order;
use AutoMapper\Tests\Fixtures\PetOwner;
use AutoMapper\Tests\Fixtures\PetOwnerWithConstructorArguments;
use AutoMapper\Tests\Fixtures\PrivatePropertyInConstructors\ChildClass;
use AutoMapper\Tests\Fixtures\PrivatePropertyInConstructors\OtherClass;
use AutoMapper\Tests\Fixtures\Provider\CustomProvider;
Expand Down Expand Up @@ -1088,6 +1089,26 @@ public function testAdderAndRemoverWithNull(): void
self::assertCount(0, $petOwnerData->getPets());
}

public function testAdderAndRemoverWithConstructorArguments(): void
{
if (!class_exists(ClassDiscriminatorFromClassMetadata::class)) {
self::markTestSkipped('Symfony Serializer is required to run this test.');
}

$petOwner = [
'pets' => [
['type' => 'cat', 'name' => 'Félix'],
],
];

$petOwnerData = $this->autoMapper->map($petOwner, PetOwnerWithConstructorArguments::class);

self::assertIsArray($petOwnerData->getPets());
self::assertCount(1, $petOwnerData->getPets());
self::assertSame('Félix', $petOwnerData->getPets()[0]->name);
self::assertSame('cat', $petOwnerData->getPets()[0]->type);
}

public function testIssueTargetToPopulate(): void
{
$source = new Fixtures\IssueTargetToPopulate\VatModel();
Expand Down
38 changes: 38 additions & 0 deletions tests/Fixtures/PetOwnerWithConstructorArguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures;

class PetOwnerWithConstructorArguments
{
/** @var array<int, Pet> */
private $pets;

public function __construct(array $pets)
{
$this->pets = $pets;
}

/**
* @return Pet[]
*/
public function getPets(): array
{
return $this->pets;
}

public function addPet(Pet $pet): void
{
$this->pets[] = $pet;
}

public function removePet(Pet $pet): void
{
$index = array_search($pet, $this->pets);

if ($index !== false) {
unset($this->pets[$index]);
}
}
}

0 comments on commit 09bb409

Please sign in to comment.