diff --git a/application/src/Domain/SourceManagement/SourceMemberManager.php b/application/src/Domain/SourceManagement/SourceMemberManager.php new file mode 100644 index 0000000..36dcd63 --- /dev/null +++ b/application/src/Domain/SourceManagement/SourceMemberManager.php @@ -0,0 +1,50 @@ +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); + } + } +} diff --git a/application/src/Domain/SourceManagement/SourceMemberManagerInterface.php b/application/src/Domain/SourceManagement/SourceMemberManagerInterface.php new file mode 100644 index 0000000..1aeaf54 --- /dev/null +++ b/application/src/Domain/SourceManagement/SourceMemberManagerInterface.php @@ -0,0 +1,28 @@ +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 9bd3a04..608fa39 100644 --- a/application/src/Entity/Source/SourceInterface.php +++ b/application/src/Entity/Source/SourceInterface.php @@ -11,29 +11,8 @@ use App\Entity\Attribut\SlugAttributInterface; use App\Entity\Attribut\MembersAttributInterface; /** - * @todo Move the members functions to an member ship manager - * * @author kevinfrantz */ 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/Domain/SourceManagement/SourceMemberManagerTest.php b/application/tests/Unit/Domain/SourceManagement/SourceMemberManagerTest.php new file mode 100644 index 0000000..7036b96 --- /dev/null +++ b/application/tests/Unit/Domain/SourceManagement/SourceMemberManagerTest.php @@ -0,0 +1,56 @@ +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()); + } +} diff --git a/application/tests/Unit/Entity/Source/AbstractSourceTest.php b/application/tests/Unit/Entity/Source/AbstractSourceTest.php index 25dbce4..dfb1a98 100644 --- a/application/tests/Unit/Entity/Source/AbstractSourceTest.php +++ b/application/tests/Unit/Entity/Source/AbstractSourceTest.php @@ -40,28 +40,6 @@ 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); diff --git a/application/tests/Unit/Repository/UserSourceRepositoryTest.php b/application/tests/Unit/Repository/UserSourceRepositoryTest.php index ca4a887..2785f6b 100644 --- a/application/tests/Unit/Repository/UserSourceRepositoryTest.php +++ b/application/tests/Unit/Repository/UserSourceRepositoryTest.php @@ -5,6 +5,7 @@ namespace tests\Unit\Repository; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Doctrine\ORM\EntityManager; use App\Entity\Source\Complex\UserSource; +use App\Domain\SourceManagement\SourceMemberManager; /** * @todo refactor this to an integration test! @@ -30,7 +31,8 @@ class UserSourceRepositoryTest extends KernelTestCase { $insertSource = new UserSource(); $origineSource = new UserSource(); - $origineSource->addMember($insertSource); + $origineSourceMemberManager = new SourceMemberManager($origineSource); + $origineSourceMemberManager->addMember($insertSource); $this->entityManager->persist($insertSource); $this->entityManager->persist($origineSource); $this->entityManager->flush(); @@ -40,7 +42,7 @@ class UserSourceRepositoryTest extends KernelTestCase ->get(0)); $this->assertEquals($origineSource, $insertSource->getMemberships() ->get(0)); - $this->assertNull($origineSource->removeMember($insertSource)); + $this->assertNull($origineSourceMemberManager->removeMember($insertSource)); $this->assertEquals(0, $origineSource->getMembers() ->count()); $this->assertEquals(0, $insertSource->getMemberships()