diff --git a/application/src/Entity/Attribut/MembersAttribut.php b/application/src/Entity/Attribut/MembersAttribut.php index 3c3529f..5172ffd 100644 --- a/application/src/Entity/Attribut/MembersAttribut.php +++ b/application/src/Entity/Attribut/MembersAttribut.php @@ -3,8 +3,7 @@ namespace App\Entity\Attribut; use Doctrine\Common\Collections\Collection; -use Doctrine\Common\Collections\ArrayCollection; -use App\Helper\Dimension; +use App\Helper\DimensionHelper; /** * @author kevinfrantz @@ -34,8 +33,9 @@ trait MembersAttribut */ public function getMembersIncludingChildren(?int $dimension = null, Collection $members = null): Collection { - $dimensionHelper = new Dimension(__FUNCTION__, MembersAttributInterface::class, $this, 'members'); - return $dimensionHelper->getDimensions($dimension,$members); + $dimensionHelper = new DimensionHelper(__FUNCTION__, MembersAttributInterface::class, $this, 'members'); + + return $dimensionHelper->getDimensions($dimension, $members); } private function continueIncludeMembersLoop(?int $dimension): bool diff --git a/application/src/Entity/Attribut/MembershipsAttribut.php b/application/src/Entity/Attribut/MembershipsAttribut.php index f91dced..89173eb 100644 --- a/application/src/Entity/Attribut/MembershipsAttribut.php +++ b/application/src/Entity/Attribut/MembershipsAttribut.php @@ -3,7 +3,6 @@ namespace App\Entity\Attribut; use Doctrine\Common\Collections\Collection; -use App\Entity\Source\GroupSourceInterface; use App\Entity\Source\Collection\MemberCollectionSourceInterface; /** diff --git a/application/src/Entity/Attribut/SourceCollectionAttribut.php b/application/src/Entity/Attribut/SourceCollectionAttribut.php index d7c2035..35d1789 100644 --- a/application/src/Entity/Attribut/SourceCollectionAttribut.php +++ b/application/src/Entity/Attribut/SourceCollectionAttribut.php @@ -1,4 +1,5 @@ method = $method; - $this->interface = $interface; - $this->object = $object; - $this->attribut = $attribut; + public function __construct(string $method, string $interface, object $object, string $attribut) + { + $this->method = $method; + $this->interface = $interface; + $this->object = $object; + $this->attribut = $attribut; } - + /** * @param int $dimension The dimensions start with 1 for the elements of the actuall dimension and NULL for all elements - * @param Collection $elements A elements collection, to which new elements should be add + * @param Collection $elements A elements collection, to which new elements should be add * * @return Collection Returns all elements till the defined dimension */ @@ -62,7 +63,7 @@ final class Dimension implements DimensionInterface { $this->setDimension($dimension); $elements = $elements ?? new ArrayCollection(); - foreach ($this->object->{$this->attributGetterName()}()->toArray() as $element) { + foreach ($this->object->{$this->attributGetterName()}()->toArray() as $element) { if (!$elements->contains($element)) { $elements->add($element); if ($this->continueLoop() && $element instanceof $this->interface) { @@ -70,28 +71,32 @@ final class Dimension implements DimensionInterface } } } + return $elements; } - - private function setDimension(?int $dimension):void{ + + private function setDimension(?int $dimension): void + { $this->dimension = is_int($dimension) ? $dimension - 1 : null; } - - private function attributGetterName():string{ + + private function attributGetterName(): string + { return 'get'.ucfirst($this->attribut); } - - private function includeInfiniteDimensions():bool{ + + private function includeInfiniteDimensions(): bool + { return is_null($this->dimension); } - - private function isNotLastDimension():bool{ + + private function isNotLastDimension(): bool + { return $this->dimension > 0; } - + private function continueLoop(): bool { - return $this->includeInfiniteDimensions() || $this->isNotLastDimension(); + return $this->includeInfiniteDimensions() || $this->isNotLastDimension(); } } - diff --git a/application/src/Helper/DimensionInterface.php b/application/src/Helper/DimensionHelperInterface.php similarity index 83% rename from application/src/Helper/DimensionInterface.php rename to application/src/Helper/DimensionHelperInterface.php index cefa3eb..807c872 100644 --- a/application/src/Helper/DimensionInterface.php +++ b/application/src/Helper/DimensionHelperInterface.php @@ -1,10 +1,10 @@ getReflectionClassByObject($object); $method = $reflection->getMethod($methodName); $method->setAccessible(true); return $method->invokeArgs($object, $parameters); } + + /** + * @param object $object + * @param string $property + * @param mixed $value + */ + public function setProperty(object &$object, string $property, $value): void + { + $reflectionClass = $this->getReflectionClassByObject($object); + $reflectionProperty = $reflectionClass->getProperty($property); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($object, $value); + } + + private function getReflectionClassByObject(object &$object): \ReflectionClass + { + return new \ReflectionClass(get_class($object)); + } } diff --git a/application/tests/Unit/Entity/Attribut/IdAttributTest.php b/application/tests/Unit/Entity/Attribut/IdAttributTest.php index f1ac8d6..69709aa 100644 --- a/application/tests/Unit/Entity/Attribut/IdAttributTest.php +++ b/application/tests/Unit/Entity/Attribut/IdAttributTest.php @@ -1,4 +1,5 @@ id = new class implements IdAttributInterface{ + + public function setUp(): void + { + $this->id = new class() implements IdAttributInterface { use IdAttribut; }; } - - public function testConstruct():void{ + + public function testConstruct(): void + { $this->expectException(\TypeError::class); $this->id->getId(); } - - public function testAccessors():void{ + + public function testAccessors(): void + { $id = 1234; $this->assertNull($this->id->setId($id)); $this->assertEquals($id, $this->id->getId()); } - } - diff --git a/application/tests/Unit/Entity/Attribut/MembersAttributTest.php b/application/tests/Unit/Entity/Attribut/MembersAttributTest.php index 0882909..3a4956b 100644 --- a/application/tests/Unit/Entity/Attribut/MembersAttributTest.php +++ b/application/tests/Unit/Entity/Attribut/MembersAttributTest.php @@ -5,7 +5,6 @@ namespace App\Tests\Unit\Entity\Attribut; use App\Entity\Attribut\MembersAttribut; use App\Entity\Attribut\MembersAttributInterface; use Doctrine\Common\Collections\ArrayCollection; -use App\Entity\Source\AbstractSource; use App\Tests\AbstractTestCase; use App\Entity\Source\SourceInterface; @@ -20,9 +19,10 @@ class MembersAttributTest extends AbstractTestCase { $this->membersAttribut = $this->getMembersAttributClassMock(); } - - private function getMembersAttributClassMock():MembersAttributInterface{ - return new class implements MembersAttributInterface{ + + private function getMembersAttributClassMock(): MembersAttributInterface + { + return new class() implements MembersAttributInterface { use MembersAttribut; }; } @@ -34,20 +34,6 @@ class MembersAttributTest extends AbstractTestCase $this->membersAttribut->getMembersIncludingChildren(); } - private function getContinueIncludeMemberLoopResult($dimension): bool - { - return $this->invokeMethod($this->membersAttribut, 'continueIncludeMembersLoop', [$dimension]); - } - - public function testContinueIncludeMemberLoop() - { - $this->assertTrue($this->getContinueIncludeMemberLoopResult(null)); - $this->assertTrue($this->getContinueIncludeMemberLoopResult(2)); - $this->assertTrue($this->getContinueIncludeMemberLoopResult(1)); - $this->assertFalse($this->getContinueIncludeMemberLoopResult(0)); - $this->assertFalse($this->getContinueIncludeMemberLoopResult(-1)); - } - public function testMembersAccessors() { $source1 = $this->createMock(SourceInterface::class); diff --git a/application/tests/Unit/Entity/Attribut/MembershipsAttributTest.php b/application/tests/Unit/Entity/Attribut/MembershipsAttributTest.php index c0d2c92..09d0270 100644 --- a/application/tests/Unit/Entity/Attribut/MembershipsAttributTest.php +++ b/application/tests/Unit/Entity/Attribut/MembershipsAttributTest.php @@ -1,4 +1,5 @@ memberships = new class implements MembershipsAttributInterface{ + + public function setUp(): void + { + $this->memberships = new class() implements MembershipsAttributInterface { use MembershipsAttribut; }; } - - public function testConstructor():void { + + public function testConstructor(): void + { $this->expectException(\TypeError::class); $this->memberships->getMemberships(); } - - public function testAccessors():void { + + public function testAccessors(): void + { $membership = $this->createMock(MemberCollectionSourceInterface::class); $this->assertNull($this->memberships->setMemberships(new ArrayCollection([$membership]))); $this->assertEquals($this->memberships->getMemberships()->get(0), $membership); } } - diff --git a/application/tests/Unit/Entity/Source/AbstractSourceTest.php b/application/tests/Unit/Entity/Source/AbstractSourceTest.php index 6c95669..24d2299 100644 --- a/application/tests/Unit/Entity/Source/AbstractSourceTest.php +++ b/application/tests/Unit/Entity/Source/AbstractSourceTest.php @@ -33,4 +33,4 @@ class AbstractSourceTest extends TestCase $this->assertInstanceOf(Collection::class, $this->source->getMemberships()); $this->assertInstanceOf(LawInterface::class, $this->source->getLaw()); } -} \ No newline at end of file +} diff --git a/application/tests/Unit/Entity/Source/Collection/MemberCollectionSourceTest.php b/application/tests/Unit/Entity/Source/Collection/MemberCollectionSourceTest.php index fe567ae..7453f8b 100644 --- a/application/tests/Unit/Entity/Source/Collection/MemberCollectionSourceTest.php +++ b/application/tests/Unit/Entity/Source/Collection/MemberCollectionSourceTest.php @@ -1,4 +1,5 @@ groupSource->setMembers(new ArrayCollection([ - $member + $member, ])); $this->assertEquals($member, $this->groupSource->getMembers() ->get(0)); diff --git a/application/tests/Unit/HelperTest.php b/application/tests/Unit/HelperTest.php new file mode 100644 index 0000000..4b3f0d7 --- /dev/null +++ b/application/tests/Unit/HelperTest.php @@ -0,0 +1,64 @@ +dimensionMock = new class() implements DimensionHelperInterface { + /** + * @var ArrayCollection + */ + public $dimensionElements; + + /** + * @var DimensionHelper + */ + public $dimensionHelper; + + public function __construct() + { + $this->dimensionElements = new ArrayCollection(); + $this->dimensionHelper = new DimensionHelper('getDimensions', DimensionHelperInterface::class, $this, 'dimensionElements'); + } + + public function getDimensionElements(): Collection + { + return $this->dimensionElements; + } + + public function getDimensions(?int $dimension = null, Collection $elements = null): Collection + { + return $this->dimensionHelper->getDimensions($dimension, $elements); + } + }; + } + + private function getContinueLoopResult($dimension): bool + { + $this->setProperty($this->dimensionMock->dimensionHelper, 'dimension', $dimension); + + return $this->invokeMethod($this->dimensionMock->dimensionHelper, 'continueLoop'); + } + + public function testContinueLoop(): void + { + $this->assertTrue($this->getContinueLoopResult(null)); + $this->assertTrue($this->getContinueLoopResult(2)); + $this->assertTrue($this->getContinueLoopResult(1)); + $this->assertFalse($this->getContinueLoopResult(0)); + $this->assertFalse($this->getContinueLoopResult(-1)); + } +}