Optimized AbstractSource entity for members

This commit is contained in:
Kevin Frantz 2018-11-24 22:17:03 +01:00
parent 5be4b7df3c
commit 1845e0cdba
3 changed files with 39 additions and 56 deletions

View File

@ -62,7 +62,7 @@ class Relation extends AbstractMeta implements RelationInterface
/** /**
* @ORM\OneToOne(targetEntity="Law",cascade={"persist", "remove"}) * @ORM\OneToOne(targetEntity="Law",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="law_id", referencedColumnName="id") * @ORM\JoinColumn(name="law_id", referencedColumnName="id",onDelete="CASCADE")
* *
* @var LawInterface * @var LawInterface
*/ */

View File

@ -72,17 +72,17 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
* Many Sources have many Source Members. * Many Sources have many Source Members.
* *
* @var Collection|SourceInterface[] * @var Collection|SourceInterface[]
* @ORM\ManyToMany(targetEntity="AbstractSource", inversedBy="memberships",cascade={"persist"}) * @ORM\ManyToMany(targetEntity="AbstractSource", inversedBy="memberships",cascade={"persist", "remove"})
* @ORM\JoinTable(name="source_members", * @ORM\JoinTable(name="source_members",
* joinColumns={@ORM\JoinColumn(name="source_id", referencedColumnName="id")}, * joinColumns={@ORM\JoinColumn(name="source_id", referencedColumnName="id",onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="member_id", referencedColumnName="id")} * inverseJoinColumns={@ORM\JoinColumn(name="member_id", referencedColumnName="id",onDelete="CASCADE")}
* ) * )
*/ */
protected $members; protected $members;
/** /**
* @var Collection|SourceInterface[] * @var Collection|SourceInterface[]
* @ORM\ManyToMany(targetEntity="AbstractSource",mappedBy="members",cascade={"persist"}) * @ORM\ManyToMany(targetEntity="AbstractSource",mappedBy="members")
*/ */
protected $memberships; protected $memberships;

View File

@ -4,11 +4,13 @@ namespace tests\Unit\Repository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use App\Entity\UserInterface;
use App\Repository\UserSourceRepository;
use App\Entity\Source\Complex\UserSourceInterface;
use App\Entity\Source\Complex\UserSource; use App\Entity\Source\Complex\UserSource;
/**
* @todo refactor this to an integration test!
*
* @author kevinfrantz
*/
class UserSourceRepositoryTest extends KernelTestCase class UserSourceRepositoryTest extends KernelTestCase
{ {
/** /**
@ -16,70 +18,51 @@ class UserSourceRepositoryTest extends KernelTestCase
*/ */
protected $entityManager; protected $entityManager;
/**
* @var UserSourceRepository
*/
protected $userSourceRepository;
/**
* @var UserSourceInterface
*/
protected $loadedUserSource;
/**
* @var UserInterface
*/
protected $user;
/**
* @var UserSourceInterface
*/
protected $userSource;
public function setUp(): void public function setUp(): void
{ {
$kernel = self::bootKernel(); $kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer() $this->entityManager = $kernel->getContainer()
->get('doctrine') ->get('doctrine')
->getManager(); ->getManager();
$this->userSourceRepository = $this->entityManager->getRepository(UserSource::class);
$this->userSource = new UserSource();
} }
public function testMemberships(): void public function testAddAndRemoveMember(): void
{ {
$userSource2 = new UserSource(); $insertSource = new UserSource();
$this->userSource->getMemberships()->add($userSource2); $origineSource = new UserSource();
$this->entityManager->persist($this->userSource); $origineSource->addMember($insertSource);
$this->entityManager->persist($insertSource);
$this->entityManager->persist($origineSource);
$this->entityManager->flush(); $this->entityManager->flush();
$userSource2Id = $userSource2->getId(); $this->assertGreaterThan(0, $insertSource->getId());
$userSourceId = $this->userSource->getId(); $this->assertGreaterThan(0, $origineSource->getId());
$this->loadedUserSource = $this->userSourceRepository->find($userSourceId); $this->assertEquals($insertSource, $origineSource->getMembers()
$this->assertGreaterThan(0, $userSource2Id); ->get(0));
$this->assertEquals($userSource2, $this->loadedUserSource->getMemberships()->get(0)); $this->assertEquals($origineSource, $insertSource->getMemberships()
$this->deleteUserSource(); ->get(0));
$this->assertGreaterThan(0, $userSource2Id); $this->assertNull($origineSource->removeMember($insertSource));
$this->entityManager->remove($userSource2); $this->assertEquals(0, $origineSource->getMembers()
->count());
$this->assertEquals(0, $insertSource->getMemberships()
->count());
$this->entityManager->remove($origineSource);
$this->entityManager->flush(); $this->entityManager->flush();
$this->assertNull($this->userSourceRepository->find($userSource2Id)); $this->assertGreaterThan(0, $insertSource->getId());
$this->entityManager->remove($insertSource);
$this->entityManager->flush();
$this->expectException(\TypeError::class);
$insertSource->getId();
} }
public function testCreation(): void public function testCreation(): void
{ {
$this->entityManager->persist($this->userSource); $userSource = new UserSource();
$this->entityManager->persist($userSource);
$this->entityManager->flush(); $this->entityManager->flush();
$userSourceId = $this->userSource->getId(); $userSourceId = $userSource->getId();
/* $loadedUserSource = $this->entityManager->getRepository(UserSource::class)->find($userSourceId);
* @var UserSourceInterface $this->assertEquals($userSourceId, $loadedUserSource->getId());
*/ $this->entityManager->remove($loadedUserSource);
$this->loadedUserSource = $this->userSourceRepository->find($userSourceId);
$this->assertEquals($userSourceId, $this->loadedUserSource->getId());
$this->deleteUserSource();
}
private function deleteUserSource(): void
{
$this->entityManager->remove($this->loadedUserSource);
$this->entityManager->flush(); $this->entityManager->flush();
} }