Implemented Members attribut and optimized membership

This commit is contained in:
Kevin Frantz 2018-11-24 18:13:04 +01:00
parent a64541a9ad
commit cb1316a380
11 changed files with 116 additions and 28 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface;
/**
* @author kevinfrantz
*/
trait MembersAttribut
{
/**
* @var Collection|SourceInterface[]
*/
protected $memberships;
/**
* @return Collection|SourceInterface[]
*/
public function getMembers(): Collection
{
return $this->memberships;
}
/**
* @param Collection|SourceInterface[] $members
*/
public function setMembers(Collection $members): void
{
$this->memberships = $members;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface;
/**
* @author kevinfrantz
*/
interface MembersAttributInterface
{
/**
* @param Collection|SourceInterface[] $members
*/
public function setMembers(Collection $members): void;
/**
* @return Collection|SourceInterface[]
*/
public function getMembers(): Collection;
}

View File

@ -3,7 +3,7 @@
namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection;
use App\Entity\Source\Complex\Collection\TreeCollectionSourceInterface;
use App\Entity\Source\SourceInterface;
/**
* @author kevinfrantz
@ -11,12 +11,12 @@ use App\Entity\Source\Complex\Collection\TreeCollectionSourceInterface;
trait MembershipsAttribut
{
/**
* @var Collection|TreeCollectionSourceInterface[]
* @var Collection|SourceInterface[]
*/
protected $memberships;
/**
* @return Collection|TreeCollectionSourceInterface[]
* @return Collection|SourceInterface[]
*/
public function getMemberships(): Collection
{
@ -24,7 +24,7 @@ trait MembershipsAttribut
}
/**
* @param Collection|TreeCollectionSourceInterface[] $memberships
* @param Collection|SourceInterface[] $memberships
*/
public function setMemberships(Collection $memberships): void
{

View File

@ -3,7 +3,7 @@
namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection;
use App\Entity\Source\Complex\Collection\TreeCollectionSourceInterface;
use App\Entity\Source\SourceInterface;
/**
* @author kevinfrantz
@ -11,12 +11,12 @@ use App\Entity\Source\Complex\Collection\TreeCollectionSourceInterface;
interface MembershipsAttributInterface
{
/**
* @param Collection|TreeCollectionSourceInterface[] $groups
* @param Collection|SourceInterface[] $groups
*/
public function setMemberships(Collection $memberships): void;
/**
* @return Collection|TreeCollectionSourceInterface[]
* @return Collection|SourceInterface[]
*/
public function getMemberships(): Collection;
}

View File

@ -1,4 +1,5 @@
<?php
namespace App\Entity\Source;
use Doctrine\ORM\Mapping as ORM;
@ -13,15 +14,14 @@ use App\Entity\Meta\LawInterface;
use App\Entity\Meta\Law;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Attribut\MembershipsAttribut;
use App\Entity\Source\Complex\Collection\TreeCollectionSourceInterface;
use App\Entity\Attribut\SlugAttribut;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use App\Entity\Attribut\MembersAttribut;
/**
*
* @author kevinfrantz
*
*
* @ORM\Entity
* @ORM\Table(name="source")
* @ORM\InheritanceType("JOINED")
@ -43,7 +43,7 @@ use Symfony\Component\Validator\Constraints as Assert;
*/
abstract class AbstractSource extends AbstractEntity implements SourceInterface
{
use RelationAttribut,MembershipsAttribut, LawAttribut,SlugAttribut;
use RelationAttribut,MembershipsAttribut, LawAttribut,SlugAttribut,MembersAttribut;
/**
* System slugs should be writen in UPPER CASES
@ -57,7 +57,6 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
protected $slug;
/**
*
* @var RelationInterface
* @ORM\OneToOne(targetEntity="App\Entity\Meta\Relation",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="relation_id", referencedColumnName="id", onDelete="CASCADE")
@ -66,9 +65,10 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
protected $relation;
/**
* Many Sources have many Source Members
* Many Sources have many Source Members.
*
* @var Collection|SourceInterface[]
* @ORM\ManyToMany(targetEntity="AbstractSource", inversedBy="members",cascade={"persist"})
* @ORM\ManyToMany(targetEntity="AbstractSource", inversedBy="memberships",cascade={"persist"})
* @ORM\JoinTable(name="source_members",
* joinColumns={@ORM\JoinColumn(name="source_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="member_id", referencedColumnName="id")}
@ -77,14 +77,12 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
protected $members;
/**
*
* @var Collection|SourceInterface[]
* @ORM\ManyToMany(targetEntity="AbstractSource",mappedBy="members",cascade={"persist"})
*/
protected $memberships;
/**
*
* @ORM\OneToOne(targetEntity="App\Entity\Meta\Law",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="law_id", referencedColumnName="id")
*
@ -99,5 +97,6 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
$this->relation->setSource($this);
$this->law = new Law();
$this->memberships = new ArrayCollection();
$this->members = new ArrayCollection();
}
}

View File

@ -18,8 +18,11 @@ abstract class AbstractCollectionSource extends AbstractSource implements Collec
/**
* @var Collection|SourceInterface[]
* @ORM\ManyToMany(targetEntity="App\Entity\Source\AbstractSource",inversedBy="memberships")
* @ORM\JoinTable(name="source_group_members")
* @ORM\ManyToMany(targetEntity="App\Entity\Source\AbstractSource")
* @ORM\JoinTable(name="collection_source",
* joinColumns={@ORM\JoinColumn(name="collection_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="source_id", referencedColumnName="id")}
* )
*/
protected $collection;

View File

@ -3,7 +3,6 @@
namespace App\Entity\Source\Complex\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Attribut\CollectionAttribut;
/**
* @author kevinfrantz
@ -11,5 +10,4 @@ use App\Entity\Attribut\CollectionAttribut;
*/
class TreeCollectionSource extends AbstractCollectionSource implements TreeCollectionSourceInterface
{
use CollectionAttribut;
}

View File

@ -8,10 +8,11 @@ use App\Entity\Attribut\LawAttributInterface;
use App\Entity\Attribut\RelationAttributInterface;
use App\Entity\Attribut\MembershipsAttributInterface;
use App\Entity\Attribut\SlugAttributInterface;
use App\Entity\Attribut\MembersAttributInterface;
/**
* @author kevinfrantz
*/
interface SourceInterface extends IdAttributInterface, EntityInterface, MembershipsAttributInterface, LawAttributInterface, RelationAttributInterface, SlugAttributInterface
interface SourceInterface extends IdAttributInterface, EntityInterface, MembershipsAttributInterface, LawAttributInterface, RelationAttributInterface, SlugAttributInterface, MembersAttributInterface
{
}

View File

@ -0,0 +1,37 @@
<?php
namespace Tests\Unit\Entity\Attribut;
use PHPUnit\Framework\TestCase;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Attribut\MembersAttributInterface;
use App\Entity\Source\SourceInterface;
use App\Entity\Attribut\MembersAttribut;
class MembersAttributTest extends TestCase
{
/**
* @var MembersAttributInterface
*/
protected $members;
public function setUp(): void
{
$this->members = new class() implements MembersAttributInterface {
use MembersAttribut;
};
}
public function testConstructor(): void
{
$this->expectException(\TypeError::class);
$this->members->getMembers();
}
public function testAccessors(): void
{
$membership = $this->createMock(SourceInterface::class);
$this->assertNull($this->members->setMembers(new ArrayCollection([$membership])));
$this->assertEquals($this->members->getMembers()->get(0), $membership);
}
}

View File

@ -32,6 +32,7 @@ class AbstractSourceTest extends TestCase
$this->assertInstanceOf(RelationInterface::class, $this->source->getRelation());
$this->assertInstanceOf(Collection::class, $this->source->getMemberships());
$this->assertInstanceOf(LawInterface::class, $this->source->getLaw());
$this->assertInstanceOf(Collection::class, $this->source->getMembers());
}
public function testSlugInit(): void

View File

@ -1,4 +1,5 @@
<?php
namespace tests\Unit\Repository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@ -10,33 +11,27 @@ use App\Entity\Source\Complex\UserSource;
class UserSourceRepositoryTest extends KernelTestCase
{
/**
*
* @var EntityManager
*/
protected $entityManager;
/**
*
* @var UserSourceRepository
*/
protected $userSourceRepository;
/**
*
* @var UserSourceInterface
*/
protected $loadedUserSource;
/**
*
* @var UserInterface
*/
protected $user;
/**
*
* @var UserSourceInterface
*/
protected $userSource;
@ -89,7 +84,6 @@ class UserSourceRepositoryTest extends KernelTestCase
}
/**
*
* {@inheritdoc}
*
* @see \Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::tearDown()