Optimized repository tests

This commit is contained in:
Kevin Frantz
2019-01-16 20:55:49 +01:00
parent 3ca5f3dc36
commit 665f0044cc
3 changed files with 52 additions and 2 deletions

View File

@@ -0,0 +1,50 @@
<?php
namespace tests\Unit\Repository\Source;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use App\Repository\Source\SourceRepositoryInterface;
use App\Entity\Source\AbstractSource;
use App\Domain\RequestManagement\RequestedSourceInterface;
use App\DBAL\Types\SystemSlugType;
use App\Entity\Source\SourceInterface;
/**
* @author kevinfrantz
*/
class SourceRepositoryTest extends KernelTestCase
{
/**
* @var SourceRepositoryInterface
*/
protected $sourceRepository;
public function setUp(): void
{
$kernel = self::bootKernel();
$entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
$this->sourceRepository = $entityManager->getRepository(AbstractSource::class);
}
public function testLoadBySlugOrId(): void
{
$requestedSource = $this->createMock(RequestedSourceInterface::class);
$requestedSource->method('hasSlug')->willReturn(true);
$requestedSource->method('getSlug')->willReturn(SystemSlugType::IMPRINT);
$imprint = $this->sourceRepository->findOneByIdOrSlug($requestedSource);
$this->assertInstanceOf(SourceInterface::class, $imprint);
$requestedSource2 = $this->createMock(RequestedSourceInterface::class);
$requestedSource2->method('hasId')->willReturn(true);
$requestedSource2->method('getId')->willReturn($imprint->getId());
$imprint2 = $this->sourceRepository->findOneByIdOrSlug($requestedSource2);
$this->assertInstanceOf(SourceInterface::class, $imprint2);
}
public function testLoadBySlug(): void
{
$imprint = $this->sourceRepository->findOneBySlug(SystemSlugType::IMPRINT);
$this->assertInstanceOf(SourceInterface::class, $imprint);
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace tests\Unit\Repository\Source;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\EntityManager;
use App\Entity\Source\Complex\UserSource;
use App\Domain\SourceManagement\SourceMemberManager;
/**
* @todo refactor this to an integration test!
*
* @author kevinfrantz
*/
class UserSourceRepositoryTest extends KernelTestCase
{
/**
* @var EntityManager
*/
protected $entityManager;
public function setUp(): void
{
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
}
public function testAddAndRemoveMember(): void
{
$insertSource = new UserSource();
$origineSource = new UserSource();
$origineSourceMemberManager = new SourceMemberManager($origineSource);
$origineSourceMemberManager->addMember($insertSource);
$this->entityManager->persist($insertSource);
$this->entityManager->persist($origineSource);
$this->entityManager->flush();
$this->assertGreaterThan(0, $insertSource->getId());
$this->assertGreaterThan(0, $origineSource->getId());
$this->assertEquals($insertSource, $origineSource->getMemberRelation()->getMembers()
->get(0)->getSource());
$this->assertEquals($origineSource, $insertSource->getMemberRelation()->getMemberships()
->get(0)->getSource());
$this->assertNull($origineSourceMemberManager->removeMember($insertSource));
$this->assertEquals(0, $origineSource->getMemberRelation()->getMembers()
->count());
$this->assertEquals(0, $insertSource->getMemberRelation()->getMemberships()
->count());
$this->entityManager->remove($origineSource);
$this->entityManager->flush();
$this->assertGreaterThan(0, $insertSource->getId());
$this->entityManager->remove($insertSource);
$this->entityManager->flush();
$this->expectException(\TypeError::class);
$insertSource->getId();
}
public function testCreation(): void
{
$userSource = new UserSource();
$this->entityManager->persist($userSource);
$this->entityManager->flush();
$userSourceId = $userSource->getId();
$loadedUserSource = $this->entityManager->getRepository(UserSource::class)->find($userSourceId);
$this->assertEquals($userSourceId, $loadedUserSource->getId());
$this->assertGreaterThan(0, $userSource->getCreatorRelation()->getId());
$this->assertGreaterThan(0, $userSource->getLaw()->getId());
$this->assertFalse($userSource->hasPersonIdentitySource());
$this->entityManager->remove($loadedUserSource);
$this->entityManager->flush();
}
/**
* {@inheritdoc}
*
* @see \Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::tearDown()
*/
protected function tearDown(): void
{
parent::tearDown();
$this->entityManager->close();
$this->entityManager = null;
}
}