From 556c919f1f052e5f06bc42384cb614e684a597eb Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sat, 3 Nov 2018 15:14:04 +0100 Subject: [PATCH] Wrote tests for dimension helper method and optimized dimension helper --- .../CollectionDimensionHelperMethod.php | 4 +- application/src/Helper/DimensionHelper.php | 31 +++++--- .../CollectionDimensionHelperMethodTest.php | 77 +++++++++++++++++++ 3 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 application/tests/Unit/Entity/Method/CollectionDimensionHelperMethodTest.php diff --git a/application/src/Entity/Method/CollectionDimensionHelperMethod.php b/application/src/Entity/Method/CollectionDimensionHelperMethod.php index 8756bbb..e6d88ab 100644 --- a/application/src/Entity/Method/CollectionDimensionHelperMethod.php +++ b/application/src/Entity/Method/CollectionDimensionHelperMethod.php @@ -4,7 +4,7 @@ namespace App\Entity\Method; use Doctrine\Common\Collections\Collection; use App\Helper\DimensionHelper; -use App\Entity\Attribut\MembersAttributInterface; +use App\Helper\DimensionHelperInterface; /** * @todo Create test for trait! @@ -15,7 +15,7 @@ trait CollectionDimensionHelperMethod { public function getDimensions(?int $dimension = null, Collection $elements = null): Collection { - $dimensionHelper = new DimensionHelper(__FUNCTION__, MembersAttributInterface::class, $this, 'collection'); + $dimensionHelper = new DimensionHelper(__FUNCTION__, DimensionHelperInterface::class, $this, 'collection'); return $dimensionHelper->getDimensions($dimension, $elements); } diff --git a/application/src/Helper/DimensionHelper.php b/application/src/Helper/DimensionHelper.php index 5bb81bc..e65d8e4 100644 --- a/application/src/Helper/DimensionHelper.php +++ b/application/src/Helper/DimensionHelper.php @@ -40,10 +40,14 @@ final class DimensionHelper implements DimensionHelperInterface private $dimension = null; /** - * @param string $method which uses the dimension helper - * @param string $interface which is implemented in all classes which have dimensions - * @param object $object which calls the dimension helper - * @param string $attribut which represents dimensions + * @param string $method + * which uses the dimension helper + * @param string $interface + * which is implemented in all classes which have dimensions + * @param object $object + * which calls the dimension helper + * @param string $attribut + * which represents dimensions */ public function __construct(string $method, string $interface, object $object, string $attribut) { @@ -54,8 +58,10 @@ final class DimensionHelper implements DimensionHelperInterface } /** - * @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 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 * * @return Collection Returns all elements till the defined dimension */ @@ -63,11 +69,14 @@ final class DimensionHelper implements DimensionHelperInterface { $this->setDimension($dimension); $elements = $elements ?? new ArrayCollection(); - foreach ($this->object->{$this->attributGetterName()}()->toArray() as $element) { - if (!$elements->contains($element)) { - $elements->add($element); - if ($this->continueLoop() && $element instanceof $this->interface) { - $element->{$this->method}($this->dimension, $elements); + if ($this->dimension >= 0) { + foreach ($this->object->{$this->attributGetterName()}() + ->toArray() as $element) { + if (!$elements->contains($element)) { + $elements->add($element); + if ($this->continueLoop() && $element instanceof $this->interface) { + $element->{$this->method}($this->dimension, $elements); + } } } } diff --git a/application/tests/Unit/Entity/Method/CollectionDimensionHelperMethodTest.php b/application/tests/Unit/Entity/Method/CollectionDimensionHelperMethodTest.php new file mode 100644 index 0000000..512f4be --- /dev/null +++ b/application/tests/Unit/Entity/Method/CollectionDimensionHelperMethodTest.php @@ -0,0 +1,77 @@ +collection = new ArrayCollection(); + } + }; + } + + public function setUp(): void + { + $this->method = $this->getClassMock(); + $clone1 = $this->getClassMock(); + $clone2 = $this->getClassMock(); + $clone3 = $this->getClassMock(); + $clone1->getCollection()->add($clone2); + $clone2->getCollection()->add($clone3); + $this->method->getCollection()->add($clone1); + } + + public function testTestSetUp(): void + { + $this->assertEquals(1, $this->method->getCollection()->count()); + $this->assertEquals(1, $this->method->getCollection()->get(0)->getCollection()->count()); + } + + public function testThatZeroAndOneDimensionAreUnique(): void + { + $this->assertFalse($this->method->getDimensions(0)->count() == $this->method->getDimensions(1)->count()); + } + + public function testZeroDimension(): void + { + $this->assertEquals(0, $this->method->getDimensions(0)->count()); + } + + public function testFirstDimension(): void + { + $this->assertEquals(1, $this->method->getDimensions(1)->count()); + } + + public function testSecondDimensionl(): void + { + $this->assertEquals(2, $this->method->getDimensions(2)->count()); + } + + public function testThirdtDimension(): void + { + $this->assertEquals(3, $this->method->getDimensions(3)->count()); + } + + public function testInfiniteDimension(): void + { + $this->assertEquals(3, $this->method->getDimensions()->count()); + } +}