Skip to content

Commit

Permalink
add test using remover
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed May 1, 2024
1 parent 4e2c1b9 commit 1ebb917
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
49 changes: 49 additions & 0 deletions tests/Fixtures/Remove/ObjectWithAdderRemover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Remove;

use Rekalogika\Mapper\Attribute\AllowDelete;

class ObjectWithAdderRemover
{
/**
* @var array<int,Member>
*/
#[AllowDelete]
private array $members = [];

/**
* @return array<int,Member>
*/
public function getMembers(): array
{
return $this->members;
}

public function addMember(Member $member): void
{
if (!in_array($member, $this->members, true)) {
$this->members[] = $member;
}
}

public function removeMember(Member $member): void
{
$key = array_search($member, $this->members, true);

if (false !== $key) {
unset($this->members[$key]);
}
}
}
25 changes: 24 additions & 1 deletion tests/IntegrationTest/RemoveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rekalogika\Mapper\Tests\Common\FrameworkTestCase;
use Rekalogika\Mapper\Tests\Fixtures\Remove\MemberDto;
use Rekalogika\Mapper\Tests\Fixtures\Remove\MemberRepository;
use Rekalogika\Mapper\Tests\Fixtures\Remove\ObjectWithAdderRemover;
use Rekalogika\Mapper\Tests\Fixtures\Remove\ObjectWithArray;
use Rekalogika\Mapper\Tests\Fixtures\Remove\ObjectWithArrayDto;
use Rekalogika\Mapper\Tests\Fixtures\Remove\ObjectWithArrayWithoutAllowDeleteAttribute;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function testAdd(): void
$this->assertSame($this->repository->get('3'), $objectWithArray->members[2]);
}

public function testRemove(): void
public function testRemoveFromArray(): void
{
$objectWithArrayDto = new ObjectWithArrayDto();
$objectWithArrayDto->members[] = new MemberDto('1');
Expand Down Expand Up @@ -98,4 +99,26 @@ public function testNoRemovalWithoutAllowDeleteAttribute(): void
$this->assertSame($this->repository->get('2'), $objectWithArray->members[1]);
$this->assertSame($this->repository->get('3'), $objectWithArray->members[2]);
}

public function testRemoveUsingRemover(): void
{
$objectWithArrayDto = new ObjectWithArrayDto();
$objectWithArrayDto->members[] = new MemberDto('1');
$objectWithArrayDto->members[] = new MemberDto('2');
// 3 is missing, and this should remove 3 from the target object

$objectWithArray = new ObjectWithAdderRemover();
$objectWithArray->addMember($this->repository->get('1'));
$objectWithArray->addMember($this->repository->get('2'));
$objectWithArray->addMember($this->repository->get('3'));
$this->assertCount(3, $objectWithArray->getMembers());

$this->mapper->map($objectWithArrayDto, $objectWithArray);

$this->assertCount(2, $objectWithArray->getMembers());
$this->assertSame('1', $objectWithArray->getMembers()[0]->getId());
$this->assertSame('2', $objectWithArray->getMembers()[1]->getId());
$this->assertSame($this->repository->get('1'), $objectWithArray->getMembers()[0]);
$this->assertSame($this->repository->get('2'), $objectWithArray->getMembers()[1]);
}
}

0 comments on commit 1ebb917

Please sign in to comment.