Created SourceMemberManager

This commit is contained in:
Kevin Frantz 2018-11-25 21:27:25 +01:00
parent 6ebcb28dd0
commit a72b9b0357
7 changed files with 138 additions and 77 deletions

View File

@ -0,0 +1,50 @@
<?php
namespace App\Domain\SourceManagement;
use App\Entity\Source\SourceInterface;
final class SourceMemberManager implements SourceMemberManagerInterface
{
/**
* @var SourceInterface
*/
private $source;
public function __construct(SourceInterface $source)
{
$this->source = $source;
}
public function addMember(SourceInterface $member): void
{
if (!$this->source->getMembers()->contains($member)) {
$this->source->getMembers()[] = $member;
(new self($member))->addMembership($this->source);
}
}
public function removeMember(SourceInterface $member): void
{
if ($this->source->getMembers()->contains($member)) {
$this->source->getMembers()->removeElement($member);
(new self($member))->removeMembership($this->source);
}
}
public function addMembership(SourceInterface $membership): void
{
if (!$this->source->getMemberships()->contains($membership)) {
$this->source->getMemberships()[] = $membership;
(new self($membership))->addMember($this->source);
}
}
public function removeMembership(SourceInterface $membership): void
{
if ($this->source->getMemberships()->contains($membership)) {
$this->source->getMemberships()->removeElement($membership);
(new self($membership))->removeMember($this->source);
}
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Domain\SourceManagement;
use App\Entity\Source\SourceInterface;
interface SourceMemberManagerInterface
{
/**
* @param SourceInterface $member
*/
public function addMember(SourceInterface $member): void;
/**
* @param SourceInterface $member
*/
public function removeMember(SourceInterface $member): void;
/**
* @param SourceInterface $membership
*/
public function addMembership(SourceInterface $membership): void;
/**
* @param SourceInterface $membership
*/
public function removeMembership(SourceInterface $membership): void;
}

View File

@ -103,36 +103,4 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
$this->memberships = new ArrayCollection(); $this->memberships = new ArrayCollection();
$this->members = new ArrayCollection(); $this->members = new ArrayCollection();
} }
public function addMember(SourceInterface $member): void
{
if (!$this->members->contains($member)) {
$this->members[] = $member;
$member->addMembership($this);
}
}
public function removeMember(SourceInterface $member): void
{
if ($this->members->contains($member)) {
$this->members->removeElement($member);
$member->removeMembership($this);
}
}
public function addMembership(SourceInterface $membership): void
{
if (!$this->memberships->contains($membership)) {
$this->memberships[] = $membership;
$membership->addMember($this);
}
}
public function removeMembership(SourceInterface $membership): void
{
if ($this->memberships->contains($membership)) {
$this->memberships->removeElement($membership);
$membership->removeMember($this);
}
}
} }

View File

@ -11,29 +11,8 @@ use App\Entity\Attribut\SlugAttributInterface;
use App\Entity\Attribut\MembersAttributInterface; use App\Entity\Attribut\MembersAttributInterface;
/** /**
* @todo Move the members functions to an member ship manager
*
* @author kevinfrantz * @author kevinfrantz
*/ */
interface SourceInterface extends IdAttributInterface, EntityInterface, MembershipsAttributInterface, LawAttributInterface, RelationAttributInterface, SlugAttributInterface, MembersAttributInterface interface SourceInterface extends IdAttributInterface, EntityInterface, MembershipsAttributInterface, LawAttributInterface, RelationAttributInterface, SlugAttributInterface, MembersAttributInterface
{ {
/**
* @param SourceInterface $member
*/
public function addMember(SourceInterface $member): void;
/**
* @param SourceInterface $member
*/
public function removeMember(SourceInterface $member): void;
/**
* @param SourceInterface $membership
*/
public function addMembership(SourceInterface $membership): void;
/**
* @param SourceInterface $membership
*/
public function removeMembership(SourceInterface $membership): void;
} }

View File

@ -0,0 +1,56 @@
<?php
namespace Tests\Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Source\SourceInterface;
use App\Domain\SourceManagement\SourceMemberManagerInterface;
use App\Entity\Source\AbstractSource;
use App\Domain\SourceManagement\SourceMemberManager;
class SourceMemberManagerTest extends TestCase
{
/**
* @var SourceInterface
*/
private $source;
/**
* @var SourceMemberManagerInterface
*/
private $sourceMemberManager;
private function createSource(): SourceInterface
{
return new class() extends AbstractSource {
};
}
public function setUp(): void
{
$this->source = $this->createSource();
$this->sourceMemberManager = new SourceMemberManager($this->source);
}
public function testAddAndRemoveMember(): void
{
$member = $this->createSource();
$this->assertNull($this->sourceMemberManager->addMember($member));
$this->assertEquals($member, $this->source->getMembers()->get(0));
$this->assertEquals($this->source, $member->getMemberships()->get(0));
$this->assertNull($this->sourceMemberManager->removeMember($member));
$this->assertEquals(0, $this->source->getMembers()->count());
$this->assertEquals(0, $member->getMemberships()->count());
}
public function testAddAndRemoveMembership(): void
{
$membership = $this->createSource();
$this->assertNull($this->sourceMemberManager->addMembership($membership));
$this->assertEquals($membership, $this->source->getMemberships()->get(0));
$this->assertEquals($this->source, $membership->getMembers()->get(0));
$this->assertNull($this->sourceMemberManager->removeMembership($membership));
$this->assertEquals(0, $this->source->getMemberships()->count());
$this->assertEquals(0, $membership->getMembers()->count());
}
}

View File

@ -40,28 +40,6 @@ class AbstractSourceTest extends TestCase
$this->assertInstanceOf(Collection::class, $this->source->getMembers()); $this->assertInstanceOf(Collection::class, $this->source->getMembers());
} }
public function testAddAndRemoveMember(): void
{
$member = $this->getSourceDummy();
$this->assertNull($this->source->addMember($member));
$this->assertEquals($member, $this->source->getMembers()->get(0));
$this->assertEquals($this->source, $member->getMemberships()->get(0));
$this->assertNull($this->source->removeMember($member));
$this->assertEquals(0, $this->source->getMembers()->count());
$this->assertEquals(0, $member->getMemberships()->count());
}
public function testAddAndRemoveMembership(): void
{
$membership = $this->getSourceDummy();
$this->assertNull($this->source->addMembership($membership));
$this->assertEquals($membership, $this->source->getMemberships()->get(0));
$this->assertEquals($this->source, $membership->getMembers()->get(0));
$this->assertNull($this->source->removeMembership($membership));
$this->assertEquals(0, $this->source->getMemberships()->count());
$this->assertEquals(0, $membership->getMembers()->count());
}
public function testSlugInit(): void public function testSlugInit(): void
{ {
$this->expectException(\TypeError::class); $this->expectException(\TypeError::class);

View File

@ -5,6 +5,7 @@ namespace tests\Unit\Repository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use App\Entity\Source\Complex\UserSource; use App\Entity\Source\Complex\UserSource;
use App\Domain\SourceManagement\SourceMemberManager;
/** /**
* @todo refactor this to an integration test! * @todo refactor this to an integration test!
@ -30,7 +31,8 @@ class UserSourceRepositoryTest extends KernelTestCase
{ {
$insertSource = new UserSource(); $insertSource = new UserSource();
$origineSource = new UserSource(); $origineSource = new UserSource();
$origineSource->addMember($insertSource); $origineSourceMemberManager = new SourceMemberManager($origineSource);
$origineSourceMemberManager->addMember($insertSource);
$this->entityManager->persist($insertSource); $this->entityManager->persist($insertSource);
$this->entityManager->persist($origineSource); $this->entityManager->persist($origineSource);
$this->entityManager->flush(); $this->entityManager->flush();
@ -40,7 +42,7 @@ class UserSourceRepositoryTest extends KernelTestCase
->get(0)); ->get(0));
$this->assertEquals($origineSource, $insertSource->getMemberships() $this->assertEquals($origineSource, $insertSource->getMemberships()
->get(0)); ->get(0));
$this->assertNull($origineSource->removeMember($insertSource)); $this->assertNull($origineSourceMemberManager->removeMember($insertSource));
$this->assertEquals(0, $origineSource->getMembers() $this->assertEquals(0, $origineSource->getMembers()
->count()); ->count());
$this->assertEquals(0, $insertSource->getMemberships() $this->assertEquals(0, $insertSource->getMemberships()