mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 22:17:26 +01:00
Implemented test draft for MembersAttribut
This commit is contained in:
parent
e7f82c4835
commit
e2435e5667
@ -33,14 +33,16 @@ trait MembersAttribut
|
|||||||
*/
|
*/
|
||||||
public function getMembersInclusiveChildren(int $dimension = null, Collection &$members = null): Collection
|
public function getMembersInclusiveChildren(int $dimension = null, Collection &$members = null): Collection
|
||||||
{
|
{
|
||||||
// Subtract minus one, so that following members start on a other dimension:
|
if (is_int($dimension)) {
|
||||||
--$dimension;
|
// Subtract minus one, so that following members start on a other dimension:
|
||||||
|
--$dimension;
|
||||||
|
}
|
||||||
|
|
||||||
//Define members if no members are passed
|
//Define members if no members are passed
|
||||||
if (!$members) {
|
if (!$members) {
|
||||||
$members = new ArrayCollection();
|
$members = new ArrayCollection();
|
||||||
}
|
}
|
||||||
foreach ($this->members 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 || null === $dimension) {
|
if ($dimension > 0 || null === $dimension) {
|
||||||
|
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace tests\unit\Entity\Attribut;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use App\Entity\Attribut\MembersAttribut;
|
||||||
|
use App\Entity\Attribut\MembersAttributInterface;
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
use App\Entity\Source\AbstractSource;
|
||||||
|
use App\Entity\Source\GroupSource;
|
||||||
|
|
||||||
|
class MembersAttributTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var MembersAttributInterface
|
||||||
|
*/
|
||||||
|
protected $membersAttribut;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user