From e97610893de9fb074f3597e15a0140019ede66f9 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sat, 24 Nov 2018 21:21:59 +0100 Subject: [PATCH] Implemented add and remove for member --- .../src/Entity/Source/AbstractSource.php | 36 +++++++++++++++++++ .../src/Entity/Source/SourceInterface.php | 19 ++++++++++ .../Unit/Entity/Source/AbstractSourceTest.php | 31 ++++++++++++++-- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/application/src/Entity/Source/AbstractSource.php b/application/src/Entity/Source/AbstractSource.php index 62d393c..e9957d5 100644 --- a/application/src/Entity/Source/AbstractSource.php +++ b/application/src/Entity/Source/AbstractSource.php @@ -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); + } + } } diff --git a/application/src/Entity/Source/SourceInterface.php b/application/src/Entity/Source/SourceInterface.php index 608fa39..2af2325 100644 --- a/application/src/Entity/Source/SourceInterface.php +++ b/application/src/Entity/Source/SourceInterface.php @@ -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; } diff --git a/application/tests/Unit/Entity/Source/AbstractSourceTest.php b/application/tests/Unit/Entity/Source/AbstractSourceTest.php index 362d253..25dbce4 100644 --- a/application/tests/Unit/Entity/Source/AbstractSourceTest.php +++ b/application/tests/Unit/Entity/Source/AbstractSourceTest.php @@ -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);