From c99577826421304a871148f7a6041778e2afa04b Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sat, 26 Jan 2019 20:45:55 +0100 Subject: [PATCH] Optimized RequestManagement and pushed code coverage for it to 100% --- .../Domain/LayerManagement/LayerClassMap.php | 4 ++ .../Right/AbstractRequestedRightFacade.php | 2 +- .../Right/RequestedRight.php | 10 ----- .../Entity/RequestedEntityTest.php | 43 +++++++++++++++++++ .../AbstractRequestedRightFacadeTest.php | 2 + .../Right/RequestedRightTest.php | 21 ++++++++- 6 files changed, 70 insertions(+), 12 deletions(-) diff --git a/application/symfony/src/Domain/LayerManagement/LayerClassMap.php b/application/symfony/src/Domain/LayerManagement/LayerClassMap.php index b8dac3f..29104b6 100644 --- a/application/symfony/src/Domain/LayerManagement/LayerClassMap.php +++ b/application/symfony/src/Domain/LayerManagement/LayerClassMap.php @@ -5,6 +5,8 @@ namespace App\Domain\LayerManagement; use App\DBAL\Types\Meta\Right\LayerType; use App\Entity\Source\AbstractSource; use App\Exception\NotSetException; +use App\Entity\Meta\Law; +use App\Entity\Meta\Right; /** * @author kevinfrantz @@ -13,6 +15,8 @@ final class LayerClassMap implements LayerClassMapInterface { const LAYER_CLASS_MAP = [ LayerType::SOURCE => AbstractSource::class, + LayerType::LAW => Law::class, + LayerType::RIGHT => Right::class, ]; /** diff --git a/application/symfony/src/Domain/RequestManagement/Right/AbstractRequestedRightFacade.php b/application/symfony/src/Domain/RequestManagement/Right/AbstractRequestedRightFacade.php index 4a6fae5..f6e1058 100644 --- a/application/symfony/src/Domain/RequestManagement/Right/AbstractRequestedRightFacade.php +++ b/application/symfony/src/Domain/RequestManagement/Right/AbstractRequestedRightFacade.php @@ -122,6 +122,6 @@ abstract class AbstractRequestedRightFacade implements RequestedRightInterface */ public function hasRequestedEntity(): bool { - $this->requestedRight->hasRequestedEntity(); + return $this->requestedRight->hasRequestedEntity(); } } diff --git a/application/symfony/src/Domain/RequestManagement/Right/RequestedRight.php b/application/symfony/src/Domain/RequestManagement/Right/RequestedRight.php index d0c53b3..6168b21 100644 --- a/application/symfony/src/Domain/RequestManagement/Right/RequestedRight.php +++ b/application/symfony/src/Domain/RequestManagement/Right/RequestedRight.php @@ -7,7 +7,6 @@ use App\Attribut\CrudAttribut; use App\Attribut\LayerAttribut; use App\Attribut\RecieverAttribut; use App\Exception\PreconditionFailedException; -use App\Exception\NotSetException; use App\Domain\RequestManagement\Entity\RequestedEntityInterface; use App\Attribut\RequestedEntityAttribut; use App\Entity\Meta\MetaInterface; @@ -68,19 +67,10 @@ class RequestedRight implements RequestedRightInterface { $this->validateRequestedEntity(); $this->loadSource(); - $this->validateLoad(); return $this->source; } - private function validateLoad(): void - { - if ($this->source) { - return; - } - throw new NotSetException('The Requested Source couldn\'t be found!'); - } - /** * Overriding is neccessary to declare the correct relation. * diff --git a/application/symfony/tests/Unit/Domain/RequestManagement/Entity/RequestedEntityTest.php b/application/symfony/tests/Unit/Domain/RequestManagement/Entity/RequestedEntityTest.php index c91c985..6c3bebc 100644 --- a/application/symfony/tests/Unit/Domain/RequestManagement/Entity/RequestedEntityTest.php +++ b/application/symfony/tests/Unit/Domain/RequestManagement/Entity/RequestedEntityTest.php @@ -5,6 +5,12 @@ namespace tests\Unit\Domain\RequestManagement\Entity; use PHPUnit\Framework\TestCase; use App\Domain\RequestManagement\Entity\RequestedEntity; use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface; +use App\Exception\NotSetException; +use App\Exception\NotCorrectInstanceException; +use App\Domain\RequestManagement\Right\RequestedRightInterface; +use App\DBAL\Types\Meta\Right\LayerType; +use App\Repository\RepositoryInterface; +use App\Entity\EntityInterface; /** * @author kevinfrantz @@ -24,4 +30,41 @@ class RequestedEntityTest extends TestCase $this->assertEquals($id, $requestedEntity->getId()); $this->assertFalse($requestedEntity->hasSlug()); } + + public function testNotSetExeptionIdSlug(): void + { + $layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class); + $requestedEntity = new RequestedEntity($layerRepositoryFactoryService); + $this->expectException(NotSetException::class); + $requestedEntity->getEntity(); + } + + public function testNotCorrectInstanceException(): void + { + $layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class); + $layerRepositoryFactoryService->method('getRepository')->willReturn($this->createMock(RepositoryInterface::class)); + $requestedRight = $this->createMock(RequestedRightInterface::class); + $requestedRight->method('getLayer')->willReturn(LayerType::LAW); + $requestedEntity = new RequestedEntity($layerRepositoryFactoryService); + $requestedEntity->setSlug('abcd'); + $requestedEntity->setRequestedRight($requestedRight); + $this->expectException(NotCorrectInstanceException::class); + $requestedEntity->getEntity(); + } + + public function testLoadById(): void + { + $entityMock = $this->createMock(EntityInterface::class); + $repository = $this->createMock(RepositoryInterface::class); + $repository->method('find')->willReturn($entityMock); + $layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class); + $layerRepositoryFactoryService->method('getRepository')->willReturn($repository); + $requestedRight = $this->createMock(RequestedRightInterface::class); + $requestedRight->method('getLayer')->willReturn(LayerType::LAW); + $requestedEntity = new RequestedEntity($layerRepositoryFactoryService); + $requestedEntity->setId(123); + $requestedEntity->setRequestedRight($requestedRight); + $entityResult = $requestedEntity->getEntity(); + $this->assertEquals($entityMock, $entityResult); + } } diff --git a/application/symfony/tests/Unit/Domain/RequestManagement/Right/AbstractRequestedRightFacadeTest.php b/application/symfony/tests/Unit/Domain/RequestManagement/Right/AbstractRequestedRightFacadeTest.php index 1c965e9..480bcb2 100644 --- a/application/symfony/tests/Unit/Domain/RequestManagement/Right/AbstractRequestedRightFacadeTest.php +++ b/application/symfony/tests/Unit/Domain/RequestManagement/Right/AbstractRequestedRightFacadeTest.php @@ -48,12 +48,14 @@ class AbstractRequestedRightFacadeTest extends TestCase $requestedRight->method('getSource')->willReturn($source); $requestedRight->method('getReciever')->willReturn($reciever); $requestedRight->method('getRequestedEntity')->willReturn($requestedEntity); + $requestedRight->method('hasRequestedEntity')->willReturn(true); $requestedRightFacade = $this->getRequestedRightFacade($requestedRight); $this->assertEquals($layer, $requestedRightFacade->getLayer()); $this->assertEquals($type, $requestedRightFacade->getCrud()); $this->assertEquals($source, $requestedRightFacade->getSource()); $this->assertEquals($reciever, $requestedRightFacade->getReciever()); $this->assertEquals($requestedEntity, $requestedRightFacade->getRequestedEntity()); + $this->assertTrue($requestedRightFacade->hasRequestedEntity()); } public function testSetters(): void diff --git a/application/symfony/tests/Unit/Domain/RequestManagement/Right/RequestedRightTest.php b/application/symfony/tests/Unit/Domain/RequestManagement/Right/RequestedRightTest.php index 54654a1..a96e564 100644 --- a/application/symfony/tests/Unit/Domain/RequestManagement/Right/RequestedRightTest.php +++ b/application/symfony/tests/Unit/Domain/RequestManagement/Right/RequestedRightTest.php @@ -14,6 +14,8 @@ use App\Entity\Source\PureSource; use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface; use App\Domain\RepositoryManagement\LayerRepositoryFactoryService; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use App\Entity\Meta\Law; +use App\Entity\Source\SourceInterface; /** * @author kevinfrantz @@ -48,7 +50,7 @@ class RequestedRightTest extends KernelTestCase public function testLayerException(): void { $this->expectException(\TypeError::class); - var_dump($this->requestedRight->getLayer()); + $this->requestedRight->getLayer(); } public function testRequestedEntityWithoutAttributes(): void @@ -86,4 +88,21 @@ class RequestedRightTest extends KernelTestCase $responseSource2 = $this->requestedRight->getSource(); $this->assertEquals($responseSource1, $responseSource2); } + + public function testMetaEntity(): void + { + $slug = 123; + $source = $this->createMock(SourceInterface::class); + $entity = new Law(); + $entity->setSource($source); + $requestedEntity = $this->createMock(RequestedEntityInterface::class); + $requestedEntity->method('getSlug')->willReturn($slug); + $requestedEntity->method('hasSlug')->willReturn(true); + $requestedEntity->method('getEntity')->willReturn($entity); + $this->assertEquals($slug, $requestedEntity->getSlug()); + $this->requestedRight->setRequestedEntity($requestedEntity); + $this->requestedRight->setLayer(LayerType::LAW); + $responseSource1 = $this->requestedRight->getSource(); + $this->assertEquals($responseSource1, $source); + } }