From e2435e566735c35620089a0b328d58a9f876eadd Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Wed, 31 Oct 2018 18:10:54 +0100 Subject: [PATCH] Implemented test draft for MembersAttribut --- .../src/Entity/Attribut/MembersAttribut.php | 8 +- .../Entity/Attribut/MembersAttributTest.php | 80 +++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 application/tests/unit/Entity/Attribut/MembersAttributTest.php diff --git a/application/src/Entity/Attribut/MembersAttribut.php b/application/src/Entity/Attribut/MembersAttribut.php index 5cf4024..bda5cf1 100644 --- a/application/src/Entity/Attribut/MembersAttribut.php +++ b/application/src/Entity/Attribut/MembersAttribut.php @@ -33,14 +33,16 @@ trait MembersAttribut */ public function getMembersInclusiveChildren(int $dimension = null, Collection &$members = null): Collection { - // Subtract minus one, so that following members start on a other dimension: - --$dimension; + if (is_int($dimension)) { + // Subtract minus one, so that following members start on a other dimension: + --$dimension; + } //Define members if no members are passed if (!$members) { $members = new ArrayCollection(); } - foreach ($this->members as $member) { + foreach ($this->members->toArray() as $member) { if (!$members->contains($member)) { $members->add($member); if ($dimension > 0 || null === $dimension) { diff --git a/application/tests/unit/Entity/Attribut/MembersAttributTest.php b/application/tests/unit/Entity/Attribut/MembersAttributTest.php new file mode 100644 index 0000000..0908ad9 --- /dev/null +++ b/application/tests/unit/Entity/Attribut/MembersAttributTest.php @@ -0,0 +1,80 @@ +membersAttribut = new class() implements MembersAttributInterface { + use MembersAttribut; + }; + } + + public function testConstructor():void { + $this->expectException(\TypeError::class); + $this->membersAttribut->getMembers(); + $this->membersAttribut->getMembersInclusiveChildren(); + } + + public function testMembersAccessors() + { + $source1 = new class() extends AbstractSource { + }; + $source2 = clone $source1; + $source3 = clone $source1; + $members = new ArrayCollection([ + $source1, + $source2, + $source3, + ]); + $this->assertNull($this->membersAttribut->setMembers($members)); + $this->assertEquals($members, $this->membersAttribut->getMembers()); + } + + public function testMembersIncludingChildren(): void + { + $source1 = new GroupSource(); + + //Level 3 + $source2 = clone $source1; + $source3 = clone $source1; + $source4 = clone $source1; + $source5 = clone $source1; + $source6 = $this->membersAttribut; + $source1->setMembers(new ArrayCollection([$source2])); + $source2->setMembers(new ArrayCollection([$source3])); + $source3->setMembers(new ArrayCollection([$source4])); + $source4->setMembers(new ArrayCollection([$source5])); + $source5->setMembers(new ArrayCollection([$source6])); + + $level3Elements = [$source1, $source2, $source3]; + + //Recursion + $source7 = clone $source1; + $source8 = clone $source1; + $source7->setMembers(new ArrayCollection([$source8])); + $source8->setMembers(new ArrayCollection([$source7])); + + //Source without members: + $source9 = new class() extends AbstractSource { + }; + $allMembers = [$source1, $source2, $source3, $source4, $source5, $source6, $source7, $source8, $source9]; + $this->assertArraySubset($source1->getMembersInclusiveChildren(3)->toArray(), $level3Elements); + $this->assertArraySubset($source1->getMembersInclusiveChildren()->toArray(), $allMembers); + $this->assertArraySubset($source1->getMembers()->toArray(), $source1->getMembersInclusiveChildren(1)->toArray()); + $this->assertArraySubset($source1->getMembersInclusiveChildren(1)->toArray(), $source1->getMembers()->toArray()); + } +}