Optimized RequestManagement and tests

This commit is contained in:
Kevin Frantz 2019-01-26 19:39:36 +01:00
parent 4731ef22a2
commit 4dd7ce8331
12 changed files with 115 additions and 36 deletions

View File

@ -6,6 +6,8 @@ use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
/**
* @author kevinfrantz
*
* @see RequestedEntityAttributInterface
*/
trait RequestedEntityAttribut
{
@ -29,4 +31,12 @@ trait RequestedEntityAttribut
{
$this->requestedEntity = $requestedEntity;
}
/**
* @return bool
*/
public function hasRequestedEntity(): bool
{
return isset($this->requestedEntity);
}
}

View File

@ -9,6 +9,11 @@ use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
*/
interface RequestedEntityAttributInterface
{
/**
* @return bool
*/
public function hasRequestedEntity(): bool;
/**
* @return RequestedEntityInterface
*/

View File

@ -24,6 +24,14 @@ trait RequestedRightAttribut
$this->requestedRight = $requestedRight;
}
/**
* @return bool
*/
public function hasRequestedRight(): bool
{
return isset($this->requestedRight);
}
/**
* @return RequestedRightInterface
*/

View File

@ -9,6 +9,11 @@ use App\Domain\RequestManagement\Right\RequestedRightInterface;
*/
interface RequestedRightAttributInterface
{
/**
* @return bool
*/
public function hasRequestedRight(): bool;
/**
* @param RequestedRightInterface $requestedRight
*/

View File

@ -14,6 +14,7 @@ use App\Entity\Source\AbstractSource;
use App\Exception\NotSetException;
use App\Repository\RepositoryInterface;
use App\Entity\Source\SourceInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @author kevinfrantz
@ -53,19 +54,44 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
}
/**
* @throws NotSetException
*/
private function validateHasIdentity(): void
{
if (!($this->hasId() || $this->hasSlug())) {
throw new NotSetException('No identity attribut like id or slug was set!');
}
}
/**
* @param EntityInterface|null $entity
*
* @throws NotFoundHttpException
*/
private function validateLoadedEntity(?EntityInterface $entity): void
{
if (!$entity) {
throw new NotFoundHttpException('Entity with {id:"'.$this->id.'",slug:"'.$this->slug.'"} not found');
}
}
/**
* Sorry for the messed function, but should work ;)
* {@inheritdoc}
*
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity()
*/
public function getEntity(): EntityInterface
{
$this->validateHasIdentity();
if ($this->hasSlug()) {
return $this->loadBySlug();
$entity = $this->loadBySlug();
} elseif ($this->hasId()) {
$entity = $this->loadById();
}
if ($this->hasId()) {
return $this->loadById();
}
throw new NotSetException('No identity attribut like id or slug was set!');
$this->validateLoadedEntity($entity);
return $entity;
}
/**
@ -82,9 +108,9 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
/**
* @throws NotCorrectInstanceException
*
* @return SourceInterface
* @return SourceInterface|null
*/
private function loadBySlug(): SourceInterface
private function loadBySlug(): ?SourceInterface
{
$repository = $this->getEntityRepository();
if ($repository instanceof SourceRepositoryInterface) {
@ -94,9 +120,9 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
}
/**
* @return EntityInterface
* @return EntityInterface|null
*/
private function loadById(): EntityInterface
private function loadById(): ?EntityInterface
{
$repository = $this->getEntityRepository();
@ -112,7 +138,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
public function setRequestedRight($requestedRight): void
{
$this->requestedRight = $requestedRight;
if ($this->requestedRight !== $this) {
if (!$this->requestedRight->hasRequestedEntity()) {
$this->requestedRight->setRequestedEntity($this);
}
}

View File

@ -114,4 +114,14 @@ abstract class AbstractRequestedRightFacade implements RequestedRightInterface
{
$this->requestedRight->setReciever($reciever);
}
/**
* {@inheritdoc}
*
* @see \App\Attribut\RequestedEntityAttributInterface::hasRequestedEntity()
*/
public function hasRequestedEntity(): bool
{
$this->requestedRight->hasRequestedEntity();
}
}

View File

@ -27,11 +27,6 @@ class RequestedRight implements RequestedRightInterface
*/
private $source;
/**
* @var RequestedEntityInterface
*/
private $requestedEntity;
/**
* @throws NotCorrectInstanceException
*/
@ -40,11 +35,15 @@ class RequestedRight implements RequestedRightInterface
$entity = $this->requestedEntity->getEntity();
if ($entity instanceof SourceInterface) {
$this->source = $entity;
return;
}
if ($entity instanceof MetaInterface) {
$this->source = $entity->getSource();
return;
}
throw new NotCorrectInstanceException('The entity instance can\'t be processed');
throw new NotCorrectInstanceException('The entity instance '.get_class($entity).' can\'t be processed');
}
/**
@ -92,7 +91,7 @@ class RequestedRight implements RequestedRightInterface
final public function setRequestedEntity(RequestedEntityInterface $requestedEntity): void
{
$this->requestedEntity = $requestedEntity;
if ($requestedEntity->getRequestedRight() !== $this) {
if (!$requestedEntity->hasRequestedRight()) {
$this->requestedEntity->setRequestedRight($this);
}
}

View File

@ -7,6 +7,9 @@ use App\Attribut\RightAttributInterface;
use App\Attribut\RightAttribut;
use App\Entity\Meta\RightInterface;
/**
* @author kevinfrantz
*/
class RightAttributTest extends TestCase
{
/***

View File

@ -7,6 +7,7 @@ use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryService;
use App\Repository\RepositoryInterface;
use App\Exception\NotSetException;
use App\Domain\LayerManagement\LayerClassMap;
/**
* @author kevinfrantz
@ -27,7 +28,7 @@ class LayerRepositoryFactoryServiceTest extends KernelTestCase
public function testGetRepository(): void
{
foreach (array_keys(LayerRepositoryFactoryService::LAYER_CLASS_MAP) as $layer) {
foreach (array_keys(LayerClassMap::LAYER_CLASS_MAP) as $layer) {
$repositoy = $this->layerRepositoryFactoryService->getRepository($layer);
$this->assertInstanceOf(RepositoryInterface::class, $repositoy);
}

View File

@ -4,6 +4,7 @@ namespace tests\Unit\Domain\RequestManagement\Entity;
use PHPUnit\Framework\TestCase;
use App\Domain\RequestManagement\Entity\RequestedEntity;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
/**
* @author kevinfrantz
@ -12,7 +13,8 @@ class RequestedEntityTest extends TestCase
{
public function testSetByIdentity(): void
{
$requestedEntity = new RequestedEntity();
$layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class);
$requestedEntity = new RequestedEntity($layerRepositoryFactoryService);
$slug = 'test';
$requestedEntity->setIdentity($slug);
$this->assertEquals($slug, $requestedEntity->getSlug());

View File

@ -5,13 +5,15 @@ namespace tests\Unit\Domain\RequestManagement\Entity;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use App\Domain\RequestManagement\Right\RequestedRightInterface;
use App\Domain\RequestManagement\Right\RequestedRight;
use App\Entity\Source\AbstractSource;
use App\DBAL\Types\Meta\Right\LayerType;
use App\Domain\RequestManagement\Entity\RequestedEntity;
use App\DBAL\Types\SystemSlugType;
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
use App\Exception\PreconditionFailedException;
use App\Exception\NotSetException;
use App\Entity\Source\PureSource;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @author kevinfrantz
@ -23,12 +25,17 @@ class RequestedRightTest extends KernelTestCase
*/
private $requestedRight;
/**
* @var LayerRepositoryFactoryServiceInterface
*/
private $layerRepositoryFactoryService;
public function setUp(): void
{
self::bootKernel();
$entityManager = self::$container->get('doctrine.orm.default_entity_manager');
$sourceRepository = $entityManager->getRepository(AbstractSource::class);
$this->requestedRight = new RequestedRight($sourceRepository);
$this->layerRepositoryFactoryService = new LayerRepositoryFactoryService($entityManager);
$this->requestedRight = new RequestedRight();
}
public function testLayer(): void
@ -54,24 +61,27 @@ class RequestedRightTest extends KernelTestCase
public function testKnownSource(): void
{
$requestedSource = new RequestedEntity();
$requestedSource->setSlug(SystemSlugType::IMPRINT);
$this->requestedRight->setRequestedEntity($requestedSource);
$requestedEntity = new RequestedEntity($this->layerRepositoryFactoryService);
$requestedEntity->setSlug(SystemSlugType::IMPRINT);
$this->requestedRight->setRequestedEntity($requestedEntity);
$this->requestedRight->setLayer(LayerType::SOURCE);
$sourceResponse1 = $this->requestedRight->getSource();
$this->assertGreaterThan(0, $sourceResponse1->getId());
$requestedSource->setSlug('');
$this->expectException(NotSetException::class);
$requestedEntity->setSlug('');
$this->expectException(NotFoundHttpException::class);
$this->requestedRight->getSource();
}
public function testEqualsSlug(): void
{
$slug = SystemSlugType::IMPRINT;
$requestedSource = $this->createMock(RequestedEntityInterface::class);
$requestedSource->method('getSlug')->willReturn($slug);
$requestedSource->method('hasSlug')->willReturn(true);
$this->assertEquals($slug, $requestedSource->getSlug());
$this->requestedRight->setRequestedEntity($requestedSource);
$requestedEntityEntity = new PureSource();
$requestedEntity = $this->createMock(RequestedEntityInterface::class);
$requestedEntity->method('getSlug')->willReturn($slug);
$requestedEntity->method('hasSlug')->willReturn(true);
$requestedEntity->method('getEntity')->willReturn($requestedEntityEntity);
$this->assertEquals($slug, $requestedEntity->getSlug());
$this->requestedRight->setRequestedEntity($requestedEntity);
$responseSource1 = $this->requestedRight->getSource();
$responseSource2 = $this->requestedRight->getSource();
$this->assertEquals($responseSource1, $responseSource2);

View File

@ -15,8 +15,8 @@ use App\Domain\UserManagement\UserSourceDirector;
use App\Repository\Source\SourceRepositoryInterface;
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
use App\DBAL\Types\SystemSlugType;
use App\Exception\NotSetException;
use App\Exception\SetNotPossibleException;
use App\Exception\NotCorrectInstanceException;
/**
* @author kevinfrantz
@ -61,7 +61,7 @@ class RequestedUserTest extends TestCase
$requestedSource->method('getSlug')->willReturn(SystemSlugType::IMPRINT);
$requestedSource->method('hasSlug')->willReturn(true);
$sourceRepository = $this->createMock(SourceRepositoryInterface::class);
$requestedRight = new RequestedRight($sourceRepository);
$requestedRight = new RequestedRight();
$user = $this->createMock(User::class);
$userSourceDirector = new UserSourceDirector($sourceRepository, $user);
$requestedUserRightFacade = new RequestedUser($userSourceDirector, $requestedRight);
@ -70,7 +70,7 @@ class RequestedUserTest extends TestCase
$this->assertNull($requestedUserRightFacade->setRequestedEntity($requestedSource));
$this->assertEquals($layer, $requestedRight->getLayer());
$this->assertEquals($type, $requestedRight->getCrud());
$this->expectException(NotSetException::class);
$this->expectException(NotCorrectInstanceException::class);
$this->assertNotInstanceOf(RequestedEntityInterface::class, $requestedRight->getSource());
}