From ce6607fa1ac80eee21ddcd81ecac92d1966b1c56 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sat, 24 Nov 2018 16:42:51 +0100 Subject: [PATCH] Implemented SourceMembershipInformation and tests --- .../SourceMembershipInformation.php | 46 +++++++++++++++ .../SourceMembershipInformationInterface.php | 10 ++++ .../src/Exception/RecursiveException.php | 7 +++ .../SourceMembershipInformationTest.php | 57 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 application/src/Domain/SourceManagement/SourceMembershipInformation.php create mode 100644 application/src/Domain/SourceManagement/SourceMembershipInformationInterface.php create mode 100644 application/src/Exception/RecursiveException.php create mode 100644 application/tests/Unit/Domain/SourceManagement/SourceMembershipInformationTest.php diff --git a/application/src/Domain/SourceManagement/SourceMembershipInformation.php b/application/src/Domain/SourceManagement/SourceMembershipInformation.php new file mode 100644 index 0000000..1397b8a --- /dev/null +++ b/application/src/Domain/SourceManagement/SourceMembershipInformation.php @@ -0,0 +1,46 @@ +source = $source; + } + + private function itterateOverMemberships(Collection $memberships): void + { + /** + * @var SourceInterface + */ + foreach ($memberships as $membership) { + if (!$this->memberships->contains($membership)) { + $this->memberships->add($membership); + $this->itterateOverMemberships($membership->getMemberships()); + } + } + } + + public function getAllMemberships(): Collection + { + $this->memberships = new ArrayCollection(); + $this->itterateOverMemberships($this->source->getMemberships()); + + return $this->memberships; + } +} diff --git a/application/src/Domain/SourceManagement/SourceMembershipInformationInterface.php b/application/src/Domain/SourceManagement/SourceMembershipInformationInterface.php new file mode 100644 index 0000000..c5587c3 --- /dev/null +++ b/application/src/Domain/SourceManagement/SourceMembershipInformationInterface.php @@ -0,0 +1,10 @@ +source = new UserSource(); + $this->sourceMembershipInformation = new SourceMembershipInformation($this->source); + } + + public function testOneDimension(): void + { + $this->source->getMemberships()->add(new TextSource()); + $this->assertEquals(1, $this->sourceMembershipInformation->getAllMemberships()->count()); + } + + public function testThreeDimension(): void + { + $source1 = new TextSource(); + $source2 = new FirstNameSource(); + $source2->setMemberships(new ArrayCollection([$source1])); + $source3 = new FullPersonNameSource(); + $source3->getMemberships()->add($source2); + $this->source->getMemberships()->add($source3); + $this->assertEquals(3, $this->sourceMembershipInformation->getAllMemberships()->count()); + } + + public function testRecursion(): void + { + $recursiveSource = new UserSource(); + $recursiveSource->getMemberships()->add($this->source); + $this->source->getMemberships()->add($recursiveSource); + $this->assertEquals(2, $this->sourceMembershipInformation->getAllMemberships()->count()); + } +}