From e30145d792772a41f1d496d8e5f04c2a4d985908 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sun, 30 Dec 2018 17:09:44 +0100 Subject: [PATCH] Optimized SourceMembershipInformation --- .../SourceMemberInformation.php | 3 +-- .../SourceMembershipInformation.php | 15 ++++++++++++--- .../SourceMemberManagerIntegrationTest.php | 11 +++++++++++ .../SourceMembershipInformationTest.php | 19 +++++++++++++------ 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/application/src/Domain/SourceManagement/SourceMemberInformation.php b/application/src/Domain/SourceManagement/SourceMemberInformation.php index 1b52f31..e6d4501 100644 --- a/application/src/Domain/SourceManagement/SourceMemberInformation.php +++ b/application/src/Domain/SourceManagement/SourceMemberInformation.php @@ -32,8 +32,7 @@ final class SourceMemberInformation implements SourceMemberInformationInterface foreach ($members as $member) { if (!$this->members->contains($member->getSource())) { $this->members->add($member->getSource()); - $memberMembers = $member->getMembers(); - $this->itterateOverMembers($memberMembers); + $this->itterateOverMembers($member->getMembers()); } } } diff --git a/application/src/Domain/SourceManagement/SourceMembershipInformation.php b/application/src/Domain/SourceManagement/SourceMembershipInformation.php index e3ee3f0..dfcae48 100644 --- a/application/src/Domain/SourceManagement/SourceMembershipInformation.php +++ b/application/src/Domain/SourceManagement/SourceMembershipInformation.php @@ -5,6 +5,7 @@ namespace App\Domain\SourceManagement; use Doctrine\Common\Collections\Collection; use App\Entity\Source\SourceInterface; use Doctrine\Common\Collections\ArrayCollection; +use App\Entity\Meta\Relation\Member\MemberRelationInterface; final class SourceMembershipInformation implements SourceMembershipInformationInterface { @@ -23,16 +24,24 @@ final class SourceMembershipInformation implements SourceMembershipInformationIn $this->source = $source; } + /** + * @param Collection|MemberRelationInterface[] $memberships + */ private function itterateOverMemberships(Collection $memberships): void { foreach ($memberships as $membership) { - if (!$this->memberships->contains($membership)) { - $this->memberships->add($membership); - $this->itterateOverMemberships($membership->getMemberRelation()->getMemberships()); + if (!$this->memberships->contains($membership->getSource())) { + $this->memberships->add($membership->getSource()); + $this->itterateOverMemberships($membership->getMemberships()); } } } + /** + * {@inheritdoc} + * + * @see \App\Domain\SourceManagement\SourceMembershipInformationInterface::getAllMemberships() + */ public function getAllMemberships(): Collection { $this->memberships = new ArrayCollection(); diff --git a/application/tests/Integration/Domain/SourceManagement/SourceMemberManagerIntegrationTest.php b/application/tests/Integration/Domain/SourceManagement/SourceMemberManagerIntegrationTest.php index 50b9353..5b352c5 100644 --- a/application/tests/Integration/Domain/SourceManagement/SourceMemberManagerIntegrationTest.php +++ b/application/tests/Integration/Domain/SourceManagement/SourceMemberManagerIntegrationTest.php @@ -8,6 +8,7 @@ use App\Domain\SourceManagement\SourceMemberManagerInterface; use App\Entity\Source\AbstractSource; use App\Domain\SourceManagement\SourceMemberManager; use App\Domain\SourceManagement\SourceMemberInformation; +use App\Domain\SourceManagement\SourceMembershipInformation; class SourceMemberManagerIntegrationTest extends TestCase { @@ -42,4 +43,14 @@ class SourceMemberManagerIntegrationTest extends TestCase $this->sourceMemberManager->removeMember($childSource); $this->assertEquals(0, $sourceMemberInformation->getAllMembers()->count()); } + + public function testSourceMembershipInformationIntegration(): void + { + $parentSource = $this->createSource(); + $sourceMemberInformation = new SourceMembershipInformation($this->source); + $this->sourceMemberManager->addMembership($parentSource); + $this->assertEquals($parentSource, $sourceMemberInformation->getAllMemberships()->get(0)); + $this->sourceMemberManager->removeMembership($parentSource); + $this->assertEquals(0, $sourceMemberInformation->getAllMemberships()->count()); + } } diff --git a/application/tests/Unit/Domain/SourceManagement/SourceMembershipInformationTest.php b/application/tests/Unit/Domain/SourceManagement/SourceMembershipInformationTest.php index 73e0250..a547046 100644 --- a/application/tests/Unit/Domain/SourceManagement/SourceMembershipInformationTest.php +++ b/application/tests/Unit/Domain/SourceManagement/SourceMembershipInformationTest.php @@ -32,7 +32,7 @@ class SourceMembershipInformationTest extends TestCase public function testOneDimension(): void { - $this->source->getMemberRelation()->getMemberships()->add(new TextSource()); + $this->source->getMemberRelation()->getMemberships()->add((new TextSource())->getMemberRelation()); $this->assertEquals(1, $this->sourceMembershipInformation->getAllMemberships()->count()); } @@ -40,18 +40,25 @@ class SourceMembershipInformationTest extends TestCase { $source1 = new TextSource(); $source2 = new FirstNameSource(); - $source2->getMemberRelation()->setMemberships(new ArrayCollection([$source1])); + $source2->getMemberRelation()->setMemberships(new ArrayCollection([$source1->getMemberRelation()])); $source3 = new FullPersonNameSource(); - $source3->getMemberRelation()->getMemberships()->add($source2); - $this->source->getMemberRelation()->getMemberships()->add($source3); + $source3->getMemberRelation()->getMemberships()->add($source2->getMemberRelation()); + $this->source->getMemberRelation()->getMemberships()->add($source3->getMemberRelation()); $this->assertEquals(3, $this->sourceMembershipInformation->getAllMemberships()->count()); } public function testRecursion(): void { $recursiveSource = new UserSource(); - $recursiveSource->getMemberRelation()->getMemberships()->add($this->source); - $this->source->getMemberRelation()->getMemberships()->add($recursiveSource); + $recursiveSource->getMemberRelation()->getMemberships()->add($this->source->getMemberRelation()); + $this->source->getMemberRelation()->getMemberships()->add($recursiveSource->getMemberRelation()); $this->assertEquals(2, $this->sourceMembershipInformation->getAllMemberships()->count()); } + + public function testError(): void + { + $this->expectException(\Error::class); + $this->source->getMemberRelation()->getMemberships()->add($this->createSourceMock()); + $this->sourceMembershipInformation->getAllMemberships(); + } }