Implemented add and remove for member

This commit is contained in:
Kevin Frantz 2018-11-24 21:21:59 +01:00
parent c927322153
commit e97610893d
3 changed files with 84 additions and 2 deletions

View File

@ -22,6 +22,10 @@ use App\Entity\Attribut\MembersAttribut;
/**
* @author kevinfrantz
*
* For the members\memberships attribut checkout:
*
* @see http://www.inanzzz.com/index.php/post/h0jt/bidirectional-many-to-many-cascade-remove-and-orphan-removal-operations-in-doctrine
*
* @ORM\Entity
* @ORM\Table(name="source")
* @ORM\InheritanceType("JOINED")
@ -99,4 +103,36 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
$this->memberships = 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

@ -15,4 +15,23 @@ use App\Entity\Attribut\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

@ -20,10 +20,15 @@ class AbstractSourceTest extends TestCase
*/
protected $source;
private function getSourceDummy(): SourceInterface
{
return new class() extends AbstractSource {
};
}
public function setUp()
{
$this->source = new class() extends AbstractSource {
};
$this->source = $this->getSourceDummy();
}
public function testConstructor(): void
@ -35,6 +40,28 @@ class AbstractSourceTest extends TestCase
$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
{
$this->expectException(\TypeError::class);