infinito/application/tests/Unit/Entity/Attribut/MembersAttributTest.php

109 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Tests\Unit\Entity\Attribut;
use App\Entity\Attribut\MembersAttribut;
use App\Entity\Attribut\MembersAttributInterface;
use Doctrine\Common\Collections\ArrayCollection;
use App\Tests\AbstractTestCase;
use App\Entity\Source\SourceInterface;
class MembersAttributTest extends AbstractTestCase
{
/**
* @var MembersAttributInterface
*/
protected $membersAttribut;
public function setUp(): void
{
2018-11-01 19:59:36 +01:00
$this->membersAttribut = $this->getMembersAttributClassMock();
}
2018-11-01 22:34:26 +01:00
private function getMembersAttributClassMock(): MembersAttributInterface
{
return new class() implements MembersAttributInterface {
use MembersAttribut;
};
}
public function testConstructor(): void
{
$this->expectException(\TypeError::class);
$this->membersAttribut->getMembers();
2018-10-31 20:47:51 +01:00
$this->membersAttribut->getMembersIncludingChildren();
}
public function testMembersAccessors()
{
$source1 = $this->createMock(SourceInterface::class);
$source2 = clone $source1;
$source3 = clone $source1;
$members = new ArrayCollection([
$source1,
$source2,
$source3,
]);
$this->assertNull($this->membersAttribut->setMembers($members));
$this->assertEquals($members, $this->membersAttribut->getMembers());
2018-10-31 20:27:36 +01:00
$this->assertTrue($this->membersAttribut->getMembers()->contains($source1));
}
public function testFirstLevelMembersInclusiveChildren(): void
{
2018-11-01 19:59:36 +01:00
$source1 = $this->getMembersAttributClassMock();
2018-10-31 20:27:36 +01:00
$source2 = clone $source1;
$source3 = clone $source1;
$source4 = clone $source1;
$source3->setMembers(new ArrayCollection([$source4]));
$members = new ArrayCollection([$source1, $source2, $source3]);
2018-10-31 20:27:36 +01:00
$this->assertNull($this->membersAttribut->setMembers($members));
2018-10-31 20:47:51 +01:00
$this->assertEquals(3, $this->membersAttribut->getMembersIncludingChildren(1)->count());
2018-10-31 20:27:36 +01:00
}
public function test3DimensionsMembersInclusiveChildren(): void
{
2018-11-01 19:59:36 +01:00
$source1 = $this->getMembersAttributClassMock();
2018-10-31 20:27:36 +01:00
$source2 = clone $source1;
$source3 = clone $source1;
$source4 = clone $source1;
$source1->setMembers(new ArrayCollection([$source2]));
$source2->setMembers(new ArrayCollection([$source3]));
$source3->setMembers(new ArrayCollection([$source4]));
2018-11-01 19:59:36 +01:00
$source4->setMembers(new ArrayCollection());
2018-10-31 20:27:36 +01:00
$this->membersAttribut->setMembers(new ArrayCollection([$source1]));
$this->assertEquals(1, $this->membersAttribut->getMembers()->count());
$this->assertEquals(1, $source1->getMembers()->count());
$this->assertEquals(1, $source2->getMembers()->count());
$this->assertEquals(1, $source3->getMembers()->count());
$this->assertEquals(0, $source4->getMembers()->count());
2018-10-31 20:47:51 +01:00
$this->assertEquals(3, $this->membersAttribut->getMembersIncludingChildren(3)->count());
}
public function testMembersIncludingChildrenInfinite(): void
{
2018-11-01 19:59:36 +01:00
$source1 = $this->getMembersAttributClassMock();
$source1->setMembers(new ArrayCollection());
$source2 = clone $source1;
$source3 = clone $source1;
$source4 = clone $source1;
$source5 = clone $source1;
$source6 = clone $source1;
$source1->setMembers(new ArrayCollection([$source2]));
$source2->setMembers(new ArrayCollection([$source1, $source3]));
$source3->setMembers(new ArrayCollection([$source4]));
$source4->setMembers(new ArrayCollection([$source5]));
$source5->setMembers(new ArrayCollection([$source6]));
$this->membersAttribut->setMembers(new ArrayCollection([$source1]));
2018-10-31 20:47:51 +01:00
$this->assertEquals(6, $this->membersAttribut->getMembersIncludingChildren()->count());
}
public function testMemberWithoutMembers(): void
{
$source1 = $this->createMock(SourceInterface::class);
$this->membersAttribut->setMembers(new ArrayCollection([$source1]));
2018-10-31 20:47:51 +01:00
$this->assertEquals(1, $this->membersAttribut->getMembersIncludingChildren()->count());
}
}