Solved tests for SourceMemberManager

This commit is contained in:
Kevin Frantz 2018-11-26 21:55:54 +01:00
parent b23f507da8
commit b183af90e6
2 changed files with 20 additions and 24 deletions

View File

@ -3,6 +3,8 @@
namespace App\Domain\SourceManagement; namespace App\Domain\SourceManagement;
use App\Entity\Source\SourceInterface; use App\Entity\Source\SourceInterface;
use App\Domain\MemberManagement\MemberManagerInterface;
use App\Domain\MemberManagement\MemberManager;
final class SourceMemberManager implements SourceMemberManagerInterface final class SourceMemberManager implements SourceMemberManagerInterface
{ {
@ -11,40 +13,34 @@ final class SourceMemberManager implements SourceMemberManagerInterface
*/ */
private $source; private $source;
/**
* @var MemberManagerInterface
*/
private $memberManager;
public function __construct(SourceInterface $source) public function __construct(SourceInterface $source)
{ {
$this->source = $source; $this->source = $source;
$this->memberManager = new MemberManager($this->source->getMemberRelation());
} }
public function addMember(SourceInterface $member): void public function addMember(SourceInterface $member): void
{ {
if (!$this->source->getMembers()->contains($member)) { $this->memberManager->addMember($member->getMemberRelation());
$this->source->getMembers()[] = $member;
(new self($member))->addMembership($this->source);
}
} }
public function removeMember(SourceInterface $member): void public function removeMember(SourceInterface $member): void
{ {
if ($this->source->getMembers()->contains($member)) { $this->memberManager->removeMember($member->getMemberRelation());
$this->source->getMembers()->removeElement($member);
(new self($member))->removeMembership($this->source);
}
} }
public function addMembership(SourceInterface $membership): void public function addMembership(SourceInterface $membership): void
{ {
if (!$this->source->getMemberships()->contains($membership)) { $this->memberManager->addMembership($membership->getMemberRelation());
$this->source->getMemberships()[] = $membership;
(new self($membership))->addMember($this->source);
}
} }
public function removeMembership(SourceInterface $membership): void public function removeMembership(SourceInterface $membership): void
{ {
if ($this->source->getMemberships()->contains($membership)) { $this->memberManager->removeMembership($membership->getMemberRelation());
$this->source->getMemberships()->removeElement($membership);
(new self($membership))->removeMember($this->source);
}
} }
} }

View File

@ -36,21 +36,21 @@ class SourceMemberManagerTest extends TestCase
{ {
$member = $this->createSource(); $member = $this->createSource();
$this->assertNull($this->sourceMemberManager->addMember($member)); $this->assertNull($this->sourceMemberManager->addMember($member));
$this->assertEquals($member, $this->source->getMembers()->get(0)); $this->assertEquals($member, $this->source->getMemberRelation()->getMembers()->get(0)->getSource());
$this->assertEquals($this->source, $member->getMemberships()->get(0)); $this->assertEquals($this->source, $member->getMemberRelation()->getMemberships()->get(0)->getSource());
$this->assertNull($this->sourceMemberManager->removeMember($member)); $this->assertNull($this->sourceMemberManager->removeMember($member));
$this->assertEquals(0, $this->source->getMembers()->count()); $this->assertEquals(0, $this->source->getMemberRelation()->getMembers()->count());
$this->assertEquals(0, $member->getMemberships()->count()); $this->assertEquals(0, $member->getMemberRelation()->getMemberships()->count());
} }
public function testAddAndRemoveMembership(): void public function testAddAndRemoveMembership(): void
{ {
$membership = $this->createSource(); $membership = $this->createSource();
$this->assertNull($this->sourceMemberManager->addMembership($membership)); $this->assertNull($this->sourceMemberManager->addMembership($membership));
$this->assertEquals($membership, $this->source->getMemberships()->get(0)); $this->assertEquals($membership, $this->source->getMemberRelation()->getMemberships()->get(0)->getSource());
$this->assertEquals($this->source, $membership->getMembers()->get(0)); $this->assertEquals($this->source, $membership->getMemberRelation()->getMembers()->get(0)->getSource());
$this->assertNull($this->sourceMemberManager->removeMembership($membership)); $this->assertNull($this->sourceMemberManager->removeMembership($membership));
$this->assertEquals(0, $this->source->getMemberships()->count()); $this->assertEquals(0, $this->source->getMemberRelation()->getMemberships()->count());
$this->assertEquals(0, $membership->getMembers()->count()); $this->assertEquals(0, $membership->getMemberRelation()->getMembers()->count());
} }
} }