From 707df1b951c81b0747df87b426b94cbe8efb7c6b Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sun, 27 Jan 2019 15:28:25 +0100 Subject: [PATCH] Implemented draft for ActionManagement --- .../API/Source/SourceApiController.php | 47 ----------- .../ActionManagement/AbstractAction.php | 47 +++++++++++ .../AbstractActionConstructor.php | 25 ++++++ .../ActionManagement/ActionFactoryService.php | 74 +++++++++++++++++ .../ActionFactoryServiceInterface.php | 16 ++++ .../ActionManagement/ActionInterface.php | 15 ++++ .../Domain/ActionManagement/ActionService.php | 74 ++++++++++++++++- .../ActionServiceInterface.php | 27 ++++++ .../Create/AbstractCreateAction.php | 12 +++ .../Create/CreateSourceAction.php | 21 +++++ .../ActionManagement/Delete/DeleteAction.php | 45 ++++++++++ .../ActionManagement/Read/ReadAction.php | 42 ++++++++++ .../Read/ReadActionInterface.php | 14 ++++ .../Update/UpdateSourceAction.php | 7 ++ .../SourceRESTResponseManagerService.php | 82 ------------------- ...rceRESTResponseManagerServiceInterface.php | 14 ---- .../AbstractSecureCRUDService.php | 40 --------- .../CRUD/AbstractSecureCRUDService.php | 12 --- .../Create/AbstractSecureCreateService.php | 12 --- .../Create/SecureCreateServiceInterface.php | 19 ----- .../Create/SecureHeredityCreateService.php | 20 ----- .../CRUD/Create/SecureMemberCreateService.php | 16 ---- .../CRUD/Create/SecureRightCreateService.php | 20 ----- .../CRUD/Create/SecureSourceCreateService.php | 25 ------ .../CRUD/Read/AbstractSecureReadService.php | 12 --- .../CRUD/Read/SecureLawReadService.php | 21 ----- .../CRUD/Read/SecureMemberReadService.php | 21 ----- .../CRUD/Read/SecureReadServiceInterface.php | 20 ----- .../CRUD/Read/SecureRightReadService.php | 21 ----- .../CRUD/Read/SecureSourceReadService.php | 70 ---------------- .../Read/SecureSourceReadServiceInterface.php | 10 --- .../CRUD/SecureCRUDServiceInterface.php | 12 --- .../Factory/SecureCRUDFactoryService.php | 58 ------------- .../SecureCRUDFactoryServiceInterface.php | 17 ---- .../ViewManagement/AbstractViewBuilder.php | 43 ---------- .../src/Domain/ViewManagement/ViewBuilder.php | 60 ++++++++++++++ .../src/Exception/NotSecureException.php | 10 +++ .../src/Exception/NotValidByFormException.php | 10 +++ application/symfony/src/Exception/README.md | 6 ++ ...erRepositoryFactoryServiceAttributTest.php | 2 +- .../ActionFactoryServiceTest.php | 55 +++++++++++++ .../ActionManagement/ActionServiceTest.php | 63 ++++++++++++-- .../Read/ReadSourceActionTest.php | 72 ++++++++++++++++ .../SourceRESTReponseManagerTest.php | 75 ----------------- .../CRUD/Read/SecureSourceReadServiceTest.php | 75 ----------------- .../Factory/SecureCRUDFactoryServiceTest.php | 58 ------------- 46 files changed, 686 insertions(+), 831 deletions(-) create mode 100644 application/symfony/src/Domain/ActionManagement/AbstractAction.php create mode 100644 application/symfony/src/Domain/ActionManagement/AbstractActionConstructor.php create mode 100644 application/symfony/src/Domain/ActionManagement/ActionFactoryService.php create mode 100644 application/symfony/src/Domain/ActionManagement/ActionFactoryServiceInterface.php create mode 100644 application/symfony/src/Domain/ActionManagement/ActionInterface.php create mode 100644 application/symfony/src/Domain/ActionManagement/Create/AbstractCreateAction.php create mode 100644 application/symfony/src/Domain/ActionManagement/Create/CreateSourceAction.php create mode 100644 application/symfony/src/Domain/ActionManagement/Delete/DeleteAction.php create mode 100644 application/symfony/src/Domain/ActionManagement/Read/ReadAction.php create mode 100644 application/symfony/src/Domain/ActionManagement/Read/ReadActionInterface.php create mode 100644 application/symfony/src/Domain/ActionManagement/Update/UpdateSourceAction.php delete mode 100644 application/symfony/src/Domain/ResponseManagement/SourceRESTResponseManagerService.php delete mode 100644 application/symfony/src/Domain/ResponseManagement/SourceRESTResponseManagerServiceInterface.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/AbstractSecureCRUDService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/AbstractSecureCRUDService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Create/AbstractSecureCreateService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Create/SecureCreateServiceInterface.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Create/SecureHeredityCreateService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Create/SecureMemberCreateService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Create/SecureRightCreateService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Create/SecureSourceCreateService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/AbstractSecureReadService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/SecureLawReadService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/SecureMemberReadService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/SecureReadServiceInterface.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/SecureRightReadService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/SecureSourceReadService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/SecureSourceReadServiceInterface.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/CRUD/SecureCRUDServiceInterface.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/Factory/SecureCRUDFactoryService.php delete mode 100644 application/symfony/src/Domain/SecureCRUDManagement/Factory/SecureCRUDFactoryServiceInterface.php delete mode 100644 application/symfony/src/Domain/ViewManagement/AbstractViewBuilder.php create mode 100644 application/symfony/src/Domain/ViewManagement/ViewBuilder.php create mode 100644 application/symfony/src/Exception/NotSecureException.php create mode 100644 application/symfony/src/Exception/NotValidByFormException.php create mode 100644 application/symfony/src/Exception/README.md create mode 100644 application/symfony/tests/Unit/Domain/ActionManagement/ActionFactoryServiceTest.php create mode 100644 application/symfony/tests/Unit/Domain/ActionManagement/Read/ReadSourceActionTest.php delete mode 100644 application/symfony/tests/Unit/Domain/ResponseManagement/SourceRESTReponseManagerTest.php delete mode 100644 application/symfony/tests/Unit/Domain/SecureCRUDManagement/CRUD/Read/SecureSourceReadServiceTest.php delete mode 100644 application/symfony/tests/Unit/Domain/SecureCRUDManagement/Factory/SecureCRUDFactoryServiceTest.php diff --git a/application/symfony/src/Controller/API/Source/SourceApiController.php b/application/symfony/src/Controller/API/Source/SourceApiController.php index 2f573be..39b8d05 100644 --- a/application/symfony/src/Controller/API/Source/SourceApiController.php +++ b/application/symfony/src/Controller/API/Source/SourceApiController.php @@ -6,7 +6,6 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Request; use App\Controller\API\AbstractAPIController; -use App\Entity\Source\PureSource; /** * @author kevinfrantz @@ -28,52 +27,6 @@ class SourceApiController extends AbstractAPIController { } -// /** -// * @Route("/{_locale}/api/source.{_format}", -// * defaults={"_format"="json"} , -// * methods={"POST","GET"} -// * ) -// * {@inheritdoc} -// * -// * @see \App\Controller\API\AbstractAPIController::create() -// */ -// public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response -// { -// $response = new Response(); -// if (!$this->getUser()) { -// //throw $this->createAccessDeniedException('The user must be logged in!'); -// } - -// if (Request::METHOD_POST === $request->getMethod()) { -// $response = new Response(); -// $response->setContent('Post Request!'); - -// return $response; -// } -// /** -// * @var SecureSourceCreatorInterface -// */ -// $sourceCreator = $crudFactory->create(); -// $response->setContent($sourceCreator->create()->getText()); - -// return $response; - - // // $response = new Response(); - // // $response->setContent('GET Request!'); - - // // return $response; - - // // $requestedSource = new PureSource(); - // // $requestedSource->setSlug(SystemSlugType::IMPRINT); - // // $requestedRight = new Right(); - // // $requestedRight->setSource($requestedSource); - // // $requestedRight->setLayer(LayerType::SOURCE); - // // $requestedRight->setCrud(CRUDType::READ); - // // $sourceResponseManager = new SourceRESTResponseManager($this->getUser(), $entityManager, $requestedRight, $this->getViewHandler()); - - // // return $sourceResponseManager->getResponse(); -// } - /** * @Route("/{_locale}/api/source/{identifier}.{_format}", * defaults={"_format"="json"} , diff --git a/application/symfony/src/Domain/ActionManagement/AbstractAction.php b/application/symfony/src/Domain/ActionManagement/AbstractAction.php new file mode 100644 index 0000000..76770c3 --- /dev/null +++ b/application/symfony/src/Domain/ActionManagement/AbstractAction.php @@ -0,0 +1,47 @@ +isSecure()) { + if ($this->isValidByForm()) { + return $this->proccess(); + } + throw new NotValidByFormException('The requested Entity is not valid!'); + } + throw new NotSecureException("You don't have the permission to execute this action!"); + } +} diff --git a/application/symfony/src/Domain/ActionManagement/AbstractActionConstructor.php b/application/symfony/src/Domain/ActionManagement/AbstractActionConstructor.php new file mode 100644 index 0000000..674834d --- /dev/null +++ b/application/symfony/src/Domain/ActionManagement/AbstractActionConstructor.php @@ -0,0 +1,25 @@ +actionService = $actionService; + } +} diff --git a/application/symfony/src/Domain/ActionManagement/ActionFactoryService.php b/application/symfony/src/Domain/ActionManagement/ActionFactoryService.php new file mode 100644 index 0000000..a14e266 --- /dev/null +++ b/application/symfony/src/Domain/ActionManagement/ActionFactoryService.php @@ -0,0 +1,74 @@ +ucfirst($action).$this->ucfirst($layer).self::CLASS_SUFFIX; + } + + /** + * @param string $layer + * @param string $action + * + * @return string + */ + private function getActionNamespace(string $action, string $layer = ''): string + { + return self::BASE_NAMESPACE.$this->ucfirst($action).'\\'.$this->getClassName($action, $layer); + } + + /** + * @return string + */ + private function generateFullClassName(): string + { + $requestedAction = $this->actionService->getRequestedAction(); + $action = $requestedAction->getActionType(); + $layer = $requestedAction->getLayer(); + $class = $this->getActionNamespace($action, $layer); + if (class_exists($class)) { + return $class; + } + $defaultClass = $this->getActionNamespace($action); + + return $defaultClass; + } + + /** + * {@inheritdoc} + * + * @see \App\Domain\ActionManagement\ActionFactoryServiceInterface::create() + */ + public function create(): ActionInterface + { + $class = $this->generateFullClassName(); + + return new $class($this->actionService); + } +} diff --git a/application/symfony/src/Domain/ActionManagement/ActionFactoryServiceInterface.php b/application/symfony/src/Domain/ActionManagement/ActionFactoryServiceInterface.php new file mode 100644 index 0000000..55acbcc --- /dev/null +++ b/application/symfony/src/Domain/ActionManagement/ActionFactoryServiceInterface.php @@ -0,0 +1,16 @@ +requestedAction = $requestedAction; $this->secureRequestedRightChecker = $secureRequestedRightChecker; + $this->requestStack = $requestStack; + $this->layerRepositoryFactoryService = $layerRepositoryFactoryService; + $this->entityFormBuilderService = $entityFormBuilderService; + $this->entityManager = $entityManager; } /** @@ -48,4 +80,44 @@ final class ActionService implements ActionServiceInterface { return $this->secureRequestedRightChecker->check($this->requestedAction); } + + /** + * @return FormBuilderInterface + */ + public function getForm(EntityInterface $entity): FormBuilderInterface + { + $this->entityFormBuilderService->create($entity); + } + + /** + * {@inheritdoc} + * + * @see \App\Domain\ActionManagement\ActionServiceInterface::getRequest() + */ + public function getRequest(): Request + { + return $this->requestStack->getCurrentRequest(); + } + + /** + * {@use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;inheritDoc}. + * + * @see \App\Domain\ActionManagement\ActionServiceInterface::getRepository() + */ + public function getRepository(): RepositoryInterface + { + $layer = $this->requestedAction->getLayer(); + + return $this->layerRepositoryFactoryService->getRepository($layer); + } + + /** + * {@inheritdoc} + * + * @see \App\Domain\ActionManagement\ActionServiceInterface::getEntityManager() + */ + public function getEntityManager(): EntityManagerInterface + { + return $this->entityManager; + } } diff --git a/application/symfony/src/Domain/ActionManagement/ActionServiceInterface.php b/application/symfony/src/Domain/ActionManagement/ActionServiceInterface.php index 7250d5f..2171e7e 100644 --- a/application/symfony/src/Domain/ActionManagement/ActionServiceInterface.php +++ b/application/symfony/src/Domain/ActionManagement/ActionServiceInterface.php @@ -3,6 +3,11 @@ namespace App\Domain\ActionManagement; use App\Domain\RequestManagement\Action\RequestedActionInterface; +use Symfony\Component\HttpFoundation\Request; +use App\Repository\RepositoryInterface; +use Symfony\Component\Form\FormBuilderInterface; +use App\Entity\EntityInterface; +use Doctrine\ORM\EntityManagerInterface; /** * This interface offers all classes for managing an Action. @@ -20,4 +25,26 @@ interface ActionServiceInterface * @return bool true if the action permissions are right */ public function isRequestedActionSecure(): bool; + + /** + * @return Request + */ + public function getRequest(): Request; + + /** + * @return RepositoryInterface + */ + public function getRepository(): RepositoryInterface; + + /** + * @param EntityInterface $entity + * + * @return FormBuilderInterface + */ + public function getForm(EntityInterface $entity): FormBuilderInterface; + + /** + * @return EntityManagerInterface + */ + public function getEntityManager(): EntityManagerInterface; } diff --git a/application/symfony/src/Domain/ActionManagement/Create/AbstractCreateAction.php b/application/symfony/src/Domain/ActionManagement/Create/AbstractCreateAction.php new file mode 100644 index 0000000..a17e627 --- /dev/null +++ b/application/symfony/src/Domain/ActionManagement/Create/AbstractCreateAction.php @@ -0,0 +1,12 @@ +isSecure(); + } + + /** + * @todo Implement! + * {@inheritdoc} + * + * @see \App\Domain\ActionManagement\AbstractAction::isValidByForm() + */ + protected function isValidByForm(): bool + { + return true; + } + + /** + * {@inheritdoc} + * + * @see \App\Domain\ActionManagement\AbstractAction::proccess() + */ + protected function proccess() + { + $entityManager = $this->actionService->getEntityManager(); + $entity = $this->actionService->getRequestedAction()->getRequestedEntity()->getEntity(); + $entityManager->remove($entity); + } +} diff --git a/application/symfony/src/Domain/ActionManagement/Read/ReadAction.php b/application/symfony/src/Domain/ActionManagement/Read/ReadAction.php new file mode 100644 index 0000000..0c0a3a4 --- /dev/null +++ b/application/symfony/src/Domain/ActionManagement/Read/ReadAction.php @@ -0,0 +1,42 @@ +actionService->isRequestedActionSecure(); + } + + /** + * @todo Implement! + * {@inheritdoc} + * + * @see \App\Domain\ActionManagement\AbstractAction::isValidByForm() + */ + protected function isValidByForm(): bool + { + return true; + } + + /** + * {@inheritdoc} + * + * @see \App\Domain\ActionManagement\AbstractAction::proccess() + */ + protected function proccess() + { + return $this->actionService->getRequestedAction()->getRequestedEntity()->getEntity(); + } +} diff --git a/application/symfony/src/Domain/ActionManagement/Read/ReadActionInterface.php b/application/symfony/src/Domain/ActionManagement/Read/ReadActionInterface.php new file mode 100644 index 0000000..afa717e --- /dev/null +++ b/application/symfony/src/Domain/ActionManagement/Read/ReadActionInterface.php @@ -0,0 +1,14 @@ +entityManager = $entityManager; - $this->user = $user; - $this->setRequestedRight($requestedRight); - $this->setLoadedSource(); - $this->setView(); - } - - private function setView(): void - { - $this->view = new View($this->loadedSource, 200); - } - - /** - * @param RightInterface $requestedRight - * - * @throws AllreadyDefinedException - */ - private function setRequestedRight(RightInterface $requestedRight): void - { - try { - $requestedRight->getReciever(); - throw new AllreadyDefinedException('The reciever is allready defined.'); - } catch (\TypeError $error) { - $requestedRight->setReciever($this->user->getSource()); - $this->requestedRight = $requestedRight; - } - } - - /** - * {@inheritdoc} - * - * @see \App\Domain\ResponseManagement\SourceRESTResponseManagerServiceInterface::getResponse() - */ - public function getResponse(ViewHandlerInterface $viewHandler): Response - { - return $viewHandler->handle($this->view); - } -} diff --git a/application/symfony/src/Domain/ResponseManagement/SourceRESTResponseManagerServiceInterface.php b/application/symfony/src/Domain/ResponseManagement/SourceRESTResponseManagerServiceInterface.php deleted file mode 100644 index 18ffed3..0000000 --- a/application/symfony/src/Domain/ResponseManagement/SourceRESTResponseManagerServiceInterface.php +++ /dev/null @@ -1,14 +0,0 @@ -requestStack = $requestStack; - $this->security = $security; - $this->entityManager = $entityManager; - } -} diff --git a/application/symfony/src/Domain/SecureCRUDManagement/CRUD/AbstractSecureCRUDService.php b/application/symfony/src/Domain/SecureCRUDManagement/CRUD/AbstractSecureCRUDService.php deleted file mode 100644 index 99bfc24..0000000 --- a/application/symfony/src/Domain/SecureCRUDManagement/CRUD/AbstractSecureCRUDService.php +++ /dev/null @@ -1,12 +0,0 @@ -setText('Hello World!'); - - return $source; - } -} diff --git a/application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/AbstractSecureReadService.php b/application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/AbstractSecureReadService.php deleted file mode 100644 index 4d591ca..0000000 --- a/application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/AbstractSecureReadService.php +++ /dev/null @@ -1,12 +0,0 @@ -setSource($source); - - return $requestedRight; - } - - /** - * {@inheritdoc} - * - * @see \App\Domain\SecureCRUDManagement\AbstractSecureCRUDService::__construct() - */ - public function __construct(RequestStack $requestStack, Security $security, EntityManagerInterface $entityManager) - { - $this->sourceRepository = $entityManager->getRepository(AbstractSource::class); - parent::__construct($requestStack, $security, $entityManager); - } - - /** - * @todo This will not work! Change interface to requested right! - * - * @param RightInterface $requestedRight - * - * @return EntityInterface - */ - public function read(RightInterface $requestedRight): EntityInterface - { - $source = $requestedRight->getSource(); - $requestedRight = $this->getClonedRightWithModifiedSource($source, $requestedRight); - $secureSourceChecker = new SecureSourceChecker($source); - if ($secureSourceChecker->hasPermission($requestedRight)) { - return $source; - } - throw new SourceAccessDenied(); - } -} diff --git a/application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/SecureSourceReadServiceInterface.php b/application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/SecureSourceReadServiceInterface.php deleted file mode 100644 index 6e62b64..0000000 --- a/application/symfony/src/Domain/SecureCRUDManagement/CRUD/Read/SecureSourceReadServiceInterface.php +++ /dev/null @@ -1,10 +0,0 @@ -getCrud($crud); - } - - /** - * @param string $layer - * - * @return string - */ - private function getCRUDNamespace(string $layer, string $crud): string - { - return 'App\\Domain\\SecureCRUDManagement\\CRUD\\'.$this->getCrud($crud).'\\'.$this->getClassName($layer, $crud).'Service'; - } - - /** - * {@inheritdoc} - * - * @see \App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface::create() - */ - public function create(RightInterface $requestedRight): SecureCRUDServiceInterface - { - $namespace = $this->getCRUDNamespace($requestedRight->getLayer(), $requestedRight->getCrud()); - - return new $namespace($this->requestStack, $this->security, $this->entityManager); - } -} diff --git a/application/symfony/src/Domain/SecureCRUDManagement/Factory/SecureCRUDFactoryServiceInterface.php b/application/symfony/src/Domain/SecureCRUDManagement/Factory/SecureCRUDFactoryServiceInterface.php deleted file mode 100644 index af57ed5..0000000 --- a/application/symfony/src/Domain/SecureCRUDManagement/Factory/SecureCRUDFactoryServiceInterface.php +++ /dev/null @@ -1,17 +0,0 @@ -view = new View(); - $this->requestedUserRight = $requestedUserRight; - $this->secureCrudFactoryService = $secureCrudFactoryService; - } - - /** - * @return View - */ - public function getView(): View - { - $this->secureCrudFactoryService->create($requestedRight); - } -} diff --git a/application/symfony/src/Domain/ViewManagement/ViewBuilder.php b/application/symfony/src/Domain/ViewManagement/ViewBuilder.php new file mode 100644 index 0000000..63b352c --- /dev/null +++ b/application/symfony/src/Domain/ViewManagement/ViewBuilder.php @@ -0,0 +1,60 @@ +view = new View(); + $this->requestedUser = $requestedUserRight; + $this->secureCrudFactoryService = $secureCrudFactoryService; + $this->requestedEntity = $requestedEntity; + } + + private function process() + { + $secureCrudService = $this->secureCrudFactoryService->create($this->requestedUser); + $entity = $secureCrudService->process($this->requestedEntity); + } + + /** + * @return View + */ + public function getView(): View + { + } +} diff --git a/application/symfony/src/Exception/NotSecureException.php b/application/symfony/src/Exception/NotSecureException.php new file mode 100644 index 0000000..24f059a --- /dev/null +++ b/application/symfony/src/Exception/NotSecureException.php @@ -0,0 +1,10 @@ +requestedEntity = $this->createMock(RequestedEntityInterface::class); + $this->requestedAction = $this->createMock(RequestedActionInterface::class); + $this->actionService = $this->createMock(ActionServiceInterface::class); + $this->actionService->method('getRequestedAction')->willReturn($this->requestedAction); + $this->actionFactoryService = new ActionFactoryService($this->actionService); + } + + public function testCreate(): void + { + foreach (ActionType::getChoices() as $action) { + foreach (LayerType::getChoices() as $layer) { + $this->requestedAction->method('getLayer')->willReturn($layer); + $this->requestedAction->method('getActionType')->willReturn($action); + $action = $this->actionFactoryService->create(); + $this->assertInstanceOf(ActionInterface::class, $action); + } + } + } +} diff --git a/application/symfony/tests/Unit/Domain/ActionManagement/ActionServiceTest.php b/application/symfony/tests/Unit/Domain/ActionManagement/ActionServiceTest.php index 9d5ad68..e1506ef 100644 --- a/application/symfony/tests/Unit/Domain/ActionManagement/ActionServiceTest.php +++ b/application/symfony/tests/Unit/Domain/ActionManagement/ActionServiceTest.php @@ -6,26 +6,71 @@ use PHPUnit\Framework\TestCase; use App\Domain\ActionManagement\ActionService; use App\Domain\RequestManagement\Action\RequestedActionInterface; use App\Domain\SecureManagement\SecureRequestedRightCheckerInterface; +use Doctrine\ORM\EntityManagerInterface; +use App\Domain\FormManagement\EntityFormBuilderServiceInterface; +use Symfony\Component\HttpFoundation\RequestStack; +use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface; +use App\Domain\ActionManagement\ActionServiceInterface; /** * @author kevinfrantz */ class ActionServiceTest extends TestCase { + /** + * @var RequestedActionInterface + */ + private $requestedAction; + + /** + * @var SecureRequestedRightCheckerInterface + */ + private $secureRequestedRightChecker; + + /** + * @var EntityFormBuilderServiceInterface + */ + private $entityFormBuilderService; + + /** + * @var RequestStack + */ + private $requestStack; + + /** + * @var LayerRepositoryFactoryServiceInterface + */ + private $layerRepositoryFactoryService; + + /** + * @var ActionServiceInterface + */ + private $actionService; + + /** + * @var EntityManagerInterface + */ + private $entityManager; + + public function setUp(): void + { + $this->requestedAction = $this->createMock(RequestedActionInterface::class); + $this->secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class); + $this->entityFormBuilderService = $this->createMock(EntityFormBuilderServiceInterface::class); + $this->requestStack = $this->createMock(RequestStack::class); + $this->layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class); + $this->entityManager = $this->createMock(EntityManagerInterface::class); + $this->actionService = new ActionService($this->requestedAction, $this->secureRequestedRightChecker, $this->requestStack, $this->layerRepositoryFactoryService, $this->entityFormBuilderService, $this->entityManager); + } + public function testIsRequestedActionSecure() { - $requestedAction = $this->createMock(RequestedActionInterface::class); - $secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class); - $secureRequestedRightChecker->method('check')->willReturn(true); - $actionService = new ActionService($requestedAction, $secureRequestedRightChecker); - $this->assertTrue($actionService->isRequestedActionSecure()); + $this->secureRequestedRightChecker->method('check')->willReturn(true); + $this->assertTrue($this->actionService->isRequestedActionSecure()); } public function testRequestedActionGetter() { - $requestedAction = $this->createMock(RequestedActionInterface::class); - $secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class); - $actionService = new ActionService($requestedAction, $secureRequestedRightChecker); - $this->assertInstanceOf(RequestedActionInterface::class, $actionService->getRequestedAction()); + $this->assertInstanceOf(RequestedActionInterface::class, $this->actionService->getRequestedAction()); } } diff --git a/application/symfony/tests/Unit/Domain/ActionManagement/Read/ReadSourceActionTest.php b/application/symfony/tests/Unit/Domain/ActionManagement/Read/ReadSourceActionTest.php new file mode 100644 index 0000000..0f20b42 --- /dev/null +++ b/application/symfony/tests/Unit/Domain/ActionManagement/Read/ReadSourceActionTest.php @@ -0,0 +1,72 @@ +entity = $this->createMock(SourceInterface::class); + $this->requestedEntity = $this->createMock(RequestedEntityInterface::class); + $this->requestedEntity->method('getEntity')->willReturn($this->entity); + $this->entityManager = $this->createMock(EntityManagerInterface::class); + $this->requestedAction = $this->createMock(RequestedActionInterface::class); + $this->requestedAction->method('getRequestedEntity')->willReturn($this->requestedEntity); + $this->actionService = $this->createMock(ActionServiceInterface::class); + $this->actionService->method('getEntityManager')->willReturn($this->entityManager); + $this->actionService->method('getRequestedAction')->willReturn($this->requestedAction); + $this->sourceReadAction = new ReadAction($this->actionService); + } + + public function testNotSecureException(): void + { + $this->actionService->method('isRequestedActionSecure')->willReturn(false); + $this->expectException(NotSecureException::class); + $this->sourceReadAction->execute(); + } + + public function testGranted(): void + { + $this->actionService->method('isRequestedActionSecure')->willReturn(true); + $result = $this->sourceReadAction->execute(); + $this->assertEquals($this->entity, $result); + } +} diff --git a/application/symfony/tests/Unit/Domain/ResponseManagement/SourceRESTReponseManagerTest.php b/application/symfony/tests/Unit/Domain/ResponseManagement/SourceRESTReponseManagerTest.php deleted file mode 100644 index b95982e..0000000 --- a/application/symfony/tests/Unit/Domain/ResponseManagement/SourceRESTReponseManagerTest.php +++ /dev/null @@ -1,75 +0,0 @@ -requestedRight = new Right(); - } - - private function setEntityManager(): void - { - $this->entityManager = self::$container->get('doctrine.orm.default_entity_manager'); - } - - private function setViewHandler(): void - { - $this->viewHandler = $this->createMock(ViewHandlerInterface::class); - } - - public function setUp(): void - { - self::bootKernel(); - $this->setEntityManager(); - $this->setRequestedRight(); - $this->setViewHandler(); - } - - public function testAllreadyDefinedException(): void - { - $requestedSource = new PureSource(); - $requestedSource->setSlug(SystemSlugType::IMPRINT); - $requestedRight = new Right(); - $requestedRight->setSource($requestedSource); - $requestedRight->setReciever(new PureSource()); - $requestedRight->setLayer(LayerType::SOURCE); - $requestedRight->setCrud(CRUDType::READ); - $this->expectException(AllreadyDefinedException::class); - $sourceResponseManager = new SourceRESTResponseManager(null, $this->entityManager, $requestedRight, $this->viewHandler); - $sourceResponseManager->getResponse(); - } -} diff --git a/application/symfony/tests/Unit/Domain/SecureCRUDManagement/CRUD/Read/SecureSourceReadServiceTest.php b/application/symfony/tests/Unit/Domain/SecureCRUDManagement/CRUD/Read/SecureSourceReadServiceTest.php deleted file mode 100644 index 8cd5992..0000000 --- a/application/symfony/tests/Unit/Domain/SecureCRUDManagement/CRUD/Read/SecureSourceReadServiceTest.php +++ /dev/null @@ -1,75 +0,0 @@ -get('request_stack'); - $security = new Security(self::$kernel->getContainer()); - $entityManager = self::$container->get('doctrine.orm.default_entity_manager'); - $this->secureSourceReadService = new SecureSourceReadService($requestStack, $security, $entityManager); - } - - public function testAccessDeniedException(): void - { - $requestedSource = new TextSource(); - $requestedSource->setSlug(SystemSlugType::IMPRINT); - $requestedRight = new Right(); - $requestedRight->setSource($requestedSource); - $requestedRight->setLayer(LayerType::SOURCE); - $requestedRight->setCrud(CRUDType::READ); - $requestedRight->setReciever(new UserSource()); - $this->expectException(AccessDeniedHttpException::class); - $this->secureSourceReadService->read($requestedRight); - } - - public function testGranted(): void - { - $requestedSource = new TextSource(); - $requestedSource->setSlug(SystemSlugType::IMPRINT); - $requestedRight = new Right(); - $requestedRight->setSource($requestedSource); - $requestedRight->setLayer(LayerType::SOURCE); - $requestedRight->setCrud(CRUDType::READ); - $requestedRight->setReciever($this->sourceRepository->findOneBySlug(SystemSlugType::GUEST_USER)); - $textSourceResponse = $this->secureSourceReadService->read($requestedRight); - $this->assertInstanceOf(TextSourceInterface::class, $textSourceResponse); - } -} diff --git a/application/symfony/tests/Unit/Domain/SecureCRUDManagement/Factory/SecureCRUDFactoryServiceTest.php b/application/symfony/tests/Unit/Domain/SecureCRUDManagement/Factory/SecureCRUDFactoryServiceTest.php deleted file mode 100644 index bd2fa6e..0000000 --- a/application/symfony/tests/Unit/Domain/SecureCRUDManagement/Factory/SecureCRUDFactoryServiceTest.php +++ /dev/null @@ -1,58 +0,0 @@ - [ - LayerType::LAW, - ], - CRUDType::DELETE => [ - LayerType::LAW, - ], - CRUDType::READ => [], - CRUDType::UPDATE => [], - ]; - - /** - * @var SecureCRUDFactoryServiceInterface - */ - private $secureCRUDFactoryService; - - public function setUp(): void - { - self::bootKernel(); - $requestStack = self::$container->get('request_stack'); - $security = new Security(self::$kernel->getContainer()); - $entityManager = self::$container->get('doctrine.orm.default_entity_manager'); - $this->secureCRUDFactoryService = new SecureCRUDFactoryService($requestStack, $security, $entityManager); - } - - public function testCreate(): void - { - foreach (CRUDType::getChoices() as $crud) { - foreach (LayerType::getChoices() as $layer) { - if (!in_array($layer, self::EXCLUDED_TYPES[$crud])) { - $requestedRight = new Right(); - $requestedRight->setLayer($layer); - $requestedRight->setCrud($crud); - $secureCreator = $this->secureCRUDFactoryService->create($requestedRight); - $this->assertInstanceOf(SecureCRUDServiceInterface::class, $secureCreator); - } - } - } - } -}