finished implementation of members attribute and test

This commit is contained in:
Kevin Frantz 2018-10-31 20:39:41 +01:00
parent 1da8716d15
commit e7b41dab22
3 changed files with 47 additions and 60 deletions

View File

@ -1,18 +1,16 @@
<?php <?php
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
/** /**
*
* @author kevinfrantz * @author kevinfrantz
*/ */
trait MembersAttribut trait MembersAttribut
{ {
/** /**
*
* @var Collection * @var Collection
*/ */
protected $members; protected $members;
@ -28,36 +26,29 @@ trait MembersAttribut
} }
/** /**
* @param int $dimension The dimensions start with 1 for the members of the actuall dimension and NULL for all members
* @param Collection $members A reference to a members list, to which new members should be add
* *
* @param int $dimension
* The dimensions start with 1 for the members of the actuall dimension and NULL for all members
* @param Collection $members
* A reference to a members list, to which new members should be add
*
* @return Collection|MembersAttributInterface[] Returns all members till the defined dimension * @return Collection|MembersAttributInterface[] Returns all members till the defined dimension
*/ */
public function getMembersInclusiveChildren(?int $dimension = null, Collection $members = null): Collection public function getMembersInclusiveChildren(?int $dimension = null, Collection $members = null): Collection
{ {
// print_r('Dimension:'); $dimension = is_int($dimension) ? $dimension - 1 : null;
// var_dump($dimension);
$dimension = is_int($dimension) ? $dimension - 1: null;
// var_dump($dimension);
$members = $members ?? new ArrayCollection(); $members = $members ?? new ArrayCollection();
// print_r('Hello ' . $this . ' (' . $dimension . ')' . $members->count() . "\n");
foreach ($this->members->toArray() as $member) { foreach ($this->members->toArray() as $member) {
if (! $members->contains($member)) { if (!$members->contains($member)) {
$members->add($member); $members->add($member);
if ($dimension > 0) { if ($this->continueIncludeMembersLoop($dimension) && $member instanceof MembersAttributInterface) {
var_dump($dimension);
$member->getMembersInclusiveChildren($dimension, $members); $member->getMembersInclusiveChildren($dimension, $members);
} }
} }
} }
return $members; return $members;
} }
private function continueIncludeMembersLoop(?int $dimension): bool private function continueIncludeMembersLoop(?int $dimension): bool
{ {
return (is_null($dimension) || $dimension > 0); return is_null($dimension) || $dimension > 0;
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
namespace App\Tests; namespace App\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -7,20 +8,21 @@ abstract class AbstractTestCase extends TestCase
{ {
/** /**
* Call protected/private method of a class. * Call protected/private method of a class.
*
* @see https://jtreminio.com/blog/unit-testing-tutorial-part-iii-testing-protected-private-methods-coverage-reports-and-crap/ * @see https://jtreminio.com/blog/unit-testing-tutorial-part-iii-testing-protected-private-methods-coverage-reports-and-crap/
* *
* @param object &$object Instantiated object that we will run method on. * @param object &$object Instantiated object that we will run method on
* @param string $methodName Method name to call * @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method. * @param array $parameters array of parameters to pass into method
* *
* @return mixed Method return. * @return mixed method return
*/ */
public function invokeMethod(&$object, $methodName, array $parameters = array()) public function invokeMethod(&$object, $methodName, array $parameters = [])
{ {
$reflection = new \ReflectionClass(get_class($object)); $reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName); $method = $reflection->getMethod($methodName);
$method->setAccessible(true); $method->setAccessible(true);
return $method->invokeArgs($object, $parameters); return $method->invokeArgs($object, $parameters);
} }
} }

View File

@ -8,7 +8,6 @@ use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Source\AbstractSource; use App\Entity\Source\AbstractSource;
use App\Entity\Source\GroupSource; use App\Entity\Source\GroupSource;
use App\Tests\AbstractTestCase; use App\Tests\AbstractTestCase;
use phpDocumentor\Reflection\Types\Boolean;
class MembersAttributTest extends AbstractTestCase class MembersAttributTest extends AbstractTestCase
{ {
@ -30,12 +29,14 @@ class MembersAttributTest extends AbstractTestCase
$this->membersAttribut->getMembers(); $this->membersAttribut->getMembers();
$this->membersAttribut->getMembersInclusiveChildren(); $this->membersAttribut->getMembersInclusiveChildren();
} }
private function getContinueIncludeMemberLoopResult($dimension):bool{ private function getContinueIncludeMemberLoopResult($dimension): bool
{
return $this->invokeMethod($this->membersAttribut, 'continueIncludeMembersLoop', [$dimension]); return $this->invokeMethod($this->membersAttribut, 'continueIncludeMembersLoop', [$dimension]);
} }
public function testContinueIncludeMemberLoop(){ public function testContinueIncludeMemberLoop()
{
$this->assertTrue($this->getContinueIncludeMemberLoopResult(null)); $this->assertTrue($this->getContinueIncludeMemberLoopResult(null));
$this->assertTrue($this->getContinueIncludeMemberLoopResult(2)); $this->assertTrue($this->getContinueIncludeMemberLoopResult(2));
$this->assertTrue($this->getContinueIncludeMemberLoopResult(1)); $this->assertTrue($this->getContinueIncludeMemberLoopResult(1));
@ -58,20 +59,22 @@ class MembersAttributTest extends AbstractTestCase
$this->assertEquals($members, $this->membersAttribut->getMembers()); $this->assertEquals($members, $this->membersAttribut->getMembers());
$this->assertTrue($this->membersAttribut->getMembers()->contains($source1)); $this->assertTrue($this->membersAttribut->getMembers()->contains($source1));
} }
public function testFirstLevelMembersInclusiveChildren():void{ public function testFirstLevelMembersInclusiveChildren(): void
{
$source1 = new GroupSource(); $source1 = new GroupSource();
$source2 = clone $source1; $source2 = clone $source1;
$source3 = clone $source1; $source3 = clone $source1;
$source4 = clone $source1; $source4 = clone $source1;
$source3->setMembers(new ArrayCollection([$source4])); $source3->setMembers(new ArrayCollection([$source4]));
$members = new ArrayCollection([$source1,$source2,$source3]); $members = new ArrayCollection([$source1, $source2, $source3]);
$this->assertNull($this->membersAttribut->setMembers($members)); $this->assertNull($this->membersAttribut->setMembers($members));
$this->assertEquals(3,$this->membersAttribut->getMembersInclusiveChildren(1)->count()); $this->assertEquals(3, $this->membersAttribut->getMembersInclusiveChildren(1)->count());
} }
public function test3DimensionsMembersInclusiveChildren():void{ public function test3DimensionsMembersInclusiveChildren(): void
$source1= new GroupSource(); {
$source1 = new GroupSource();
$source2 = clone $source1; $source2 = clone $source1;
$source3 = clone $source1; $source3 = clone $source1;
$source4 = clone $source1; $source4 = clone $source1;
@ -79,15 +82,15 @@ class MembersAttributTest extends AbstractTestCase
$source2->setMembers(new ArrayCollection([$source3])); $source2->setMembers(new ArrayCollection([$source3]));
$source3->setMembers(new ArrayCollection([$source4])); $source3->setMembers(new ArrayCollection([$source4]));
$this->membersAttribut->setMembers(new ArrayCollection([$source1])); $this->membersAttribut->setMembers(new ArrayCollection([$source1]));
$this->assertEquals(1,$this->membersAttribut->getMembers()->count()); $this->assertEquals(1, $this->membersAttribut->getMembers()->count());
$this->assertEquals(1,$source1->getMembers()->count()); $this->assertEquals(1, $source1->getMembers()->count());
$this->assertEquals(1,$source2->getMembers()->count()); $this->assertEquals(1, $source2->getMembers()->count());
$this->assertEquals(1,$source3->getMembers()->count()); $this->assertEquals(1, $source3->getMembers()->count());
$this->assertEquals(0,$source4->getMembers()->count()); $this->assertEquals(0, $source4->getMembers()->count());
$this->assertEquals(3,$this->membersAttribut->getMembersInclusiveChildren(3)->count()); $this->assertEquals(3, $this->membersAttribut->getMembersInclusiveChildren(3)->count());
} }
public function testMembersIncludingChildren(): void public function testMembersIncludingChildrenInfinite(): void
{ {
$source1 = new GroupSource(); $source1 = new GroupSource();
$source2 = clone $source1; $source2 = clone $source1;
@ -95,30 +98,21 @@ class MembersAttributTest extends AbstractTestCase
$source4 = clone $source1; $source4 = clone $source1;
$source5 = clone $source1; $source5 = clone $source1;
$source6 = clone $source1; $source6 = clone $source1;
$source7 = clone $source1;
$source8 = clone $source1;
$source9 = new class() extends AbstractSource {
};
//Level 3
$source1->setMembers(new ArrayCollection([$source2])); $source1->setMembers(new ArrayCollection([$source2]));
$source2->setMembers(new ArrayCollection([$source3])); $source2->setMembers(new ArrayCollection([$source1, $source3]));
$source3->setMembers(new ArrayCollection([$source4])); $source3->setMembers(new ArrayCollection([$source4]));
$source4->setMembers(new ArrayCollection([$source5])); $source4->setMembers(new ArrayCollection([$source5]));
$source5->setMembers(new ArrayCollection([$source6])); $source5->setMembers(new ArrayCollection([$source6]));
$level3Elements = [$source1, $source2, $source3]; $this->membersAttribut->setMembers(new ArrayCollection([$source1]));
$this->assertEquals(6, $this->membersAttribut->getMembersInclusiveChildren()->count());
}
//Recursion public function testMemberWithoutMembers(): void
//$source7->setMembers(new ArrayCollection([$source8])); {
//$source8->setMembers(new ArrayCollection([$source7])); $source1 = new class() extends AbstractSource {
};
$allMembers = [$source1, $source2, $source3, $source4, $source5, $source6, $source7, $source8, $source9]; $this->membersAttribut->setMembers(new ArrayCollection([$source1]));
$this->assertEquals(1, $this->membersAttribut->getMembersInclusiveChildren()->count());
//$this->assertArraySubset($source1->getMembersInclusiveChildren(5)->toArray(), $level3Elements);
//$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());
} }
} }