Optimized for SPA

This commit is contained in:
Kevin Frantz
2019-01-05 23:52:37 +01:00
parent 9e685260e9
commit bccd6efaff
393 changed files with 253 additions and 37 deletions

View File

@@ -0,0 +1,34 @@
<?php
namespace Tests\Unit\Domain;
use PHPUnit\Framework\TestCase;
use App\Domain\FormManagement\FormMetaInterface;
use App\Domain\FormManagement\FormMeta;
use App\Entity\Source\Primitive\Name\SurnameSource;
use App\Domain\SourceManagement\SourceMeta;
use App\Domain\TemplateManagement\TemplateMetaInterface;
class FormMetaTest extends TestCase
{
/**
* @var FormMetaInterface
*/
protected $formMeta;
public function setUp(): void
{
$sourceMeta = new SourceMeta(new SurnameSource());
$this->formMeta = new FormMeta($sourceMeta);
}
public function testGetFormClass(): void
{
$this->assertEquals('App\Form\Source\Primitive\Name\SurnameType', $this->formMeta->getFormClass());
}
public function testTemplateMeta(): void
{
$this->assertInstanceOf(TemplateMetaInterface::class, $this->formMeta->getTemplateMeta());
}
}

View File

@@ -0,0 +1,206 @@
<?php
namespace Unit\Domain\LawManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\LawManagement\LawPermissionCheckerService;
use App\Domain\LawManagement\LawPermissionCheckerServiceInterface;
use App\Entity\Source\SourceInterface;
use App\Entity\Meta\Right;
use App\DBAL\Types\Meta\Right\LayerType;
use App\DBAL\Types\Meta\Right\CRUDType;
use App\Entity\Meta\Law;
use App\Entity\Meta\LawInterface;
use App\Entity\Meta\RightInterface;
use Doctrine\Common\Collections\ArrayCollection;
use App\Domain\SourceManagement\SourceMemberManager;
use App\Entity\Source\PureSource;
/**
* @author kevinfrantz
*/
class LawPermissionCheckerTest extends TestCase
{
/**
* @var LawPermissionCheckerServiceInterface The service which checks the law
*/
private $lawPermissionChecker;
/**
* @var LawInterface The law which applies to the source
*/
private $law;
/**
* @var RightInterface
*/
private $clientRight;
/**
* @var SourceInterface The client which requests a law
*/
private $clientSource;
/**
* @var SourceInterface The source to which the law applies
*/
private $source;
private function checkClientPermission(): bool
{
return $this->lawPermissionChecker->hasPermission($this->clientRight);
}
public function setUp(): void
{
$this->setSourceDummy();
$this->setLawDummy();
$this->setLawPermissionChecker();
$this->setClientSourceDummy();
$this->setClientRightDummy();
}
private function setLawPermissionChecker(): void
{
$this->lawPermissionChecker = new LawPermissionCheckerService($this->law);
}
private function setLawDummy(): void
{
$this->law = new Law();
$this->law->setSource($this->source);
}
private function setSourceDummy(): void
{
$this->source = new PureSource();
$this->source->setSlug('Requested Source');
}
private function setClientSourceDummy(): void
{
$this->clientSource = new PureSource();
$this->clientSource->setSlug('Client Source');
}
private function setClientRightDummy(): void
{
$this->clientRight = new Right();
$this->clientRight->setLayer(LayerType::SOURCE);
$this->clientRight->setType(CRUDType::READ);
$this->clientRight->setReciever($this->clientSource);
$this->clientRight->setSource($this->source);
}
private function getClonedClientRight(): RightInterface
{
return clone $this->clientRight;
}
public function testInitialValues(): void
{
$this->assertFalse($this->checkClientPermission());
$this->assertTrue($this->clientRight->getGrant());
}
public function testGeneralPermission(): void
{
$this->law->getRights()->add($this->getClonedClientRight());
$this->assertTrue($this->checkClientPermission());
$this->clientRight->setType(CRUDType::UPDATE);
$this->assertFalse($this->checkClientPermission());
}
public function testChildMemberPermission(): void
{
$parentSource = new PureSource();
$parentSource->setSlug('Parent Source');
$parentSourceMemberManager = new SourceMemberManager($parentSource);
$parentSourceMemberManager->addMember($this->clientSource);
/*
* The following asserts just check if the SourceMemberManager works like expected
*/
$this->assertEquals($parentSource, $this->clientSource->getMemberRelation()->getMemberships()->get(0)->getSource());
$this->assertEquals($this->clientSource, $parentSource->getMemberRelation()->getMembers()->get(0)->getSource());
$parentSourceRight = $this->getClonedClientRight();
$parentSourceRight->setReciever($parentSource);
$this->law->getRights()->add($parentSourceRight);
/*
* The following asserts just check if the in the tet defined values are like expected
*/
$this->assertEquals($parentSourceRight, $this->law->getRights()->get(0));
$this->assertEquals($parentSource, $parentSourceRight->getReciever());
$this->assertEquals($this->source, $parentSourceRight->getSource());
/*
* The following asserts are the important asserts for the test
*/
$this->assertTrue($this->checkClientPermission());
$this->law->setRights(new ArrayCollection());
$this->assertFalse($this->checkClientPermission());
}
public function testGetRightsByType(): void
{
$right = $this->getClonedClientRight();
$right->setType(CRUDType::UPDATE);
$this->law->getRights()->add($right);
$this->assertFalse($this->checkClientPermission());
$right->setType(CRUDType::READ);
$this->assertTrue($this->checkClientPermission());
}
public function testGetRightsByLayer(): void
{
$right = $this->getClonedClientRight();
$right->setLayer(LayerType::LAW);
$this->law->getRights()->add($right);
$this->assertFalse($this->checkClientPermission());
$right->setLayer(LayerType::SOURCE);
$this->assertTrue($this->checkClientPermission());
}
public function testSortByPriority(): void
{
$right1 = $this->getClonedClientRight();
$right1->setPriority(123);
$right1->setGrant(false);
$right1->setReciever($this->clientSource);
$right2 = $this->getClonedClientRight();
$right2->setPriority(456);
$right2->setGrant(true);
$right2->setReciever($this->clientSource);
$this->law->setRights(new ArrayCollection([
$right1,
$right2,
]));
$this->assertFalse($this->checkClientPermission());
$right2->setPriority(789);
$right1->setPriority(101112);
$this->assertTrue($this->checkClientPermission());
}
public function testMemberFilter(): void
{
$right1 = $this->getClonedClientRight();
$right1->setPriority(123);
$right1->setGrant(false);
$right1->setReciever(new PureSource());
$right1->getReciever()->setSlug('Rigth1 Reciever');
$right2 = $this->getClonedClientRight();
$right2->setPriority(456);
$right2->setGrant(true);
$right2->setReciever($this->clientSource);
$this->law->setRights(new ArrayCollection([
$right1,
$right2,
]));
$this->assertTrue($this->checkClientPermission());
}
public function testGrant(): void
{
$this->assertFalse($this->checkClientPermission());
$this->law->setGrant(true);
$this->assertTrue($this->checkClientPermission());
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Tests\Unit\Domain\MemberManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\MemberManagement\MemberManagerInterface;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
use App\Entity\Meta\Relation\Member\MemberRelation;
use App\Domain\MemberManagement\MemberManager;
class MemberManagerTest extends TestCase
{
/**
* @var MemberRelationInterface
*/
private $memberRelation;
/**
* @var MemberManagerInterface
*/
private $MemberManager;
public function setUp(): void
{
$this->memberRelation = new MemberRelation();
$this->MemberManager = new MemberManager($this->memberRelation);
}
public function testAddAndRemoveMember(): void
{
$member = new MemberRelation();
$this->assertNull($this->MemberManager->addMember($member));
$this->assertEquals($member, $this->memberRelation->getMembers()->get(0));
$this->assertEquals($this->memberRelation, $member->getMemberships()->get(0));
$this->assertNull($this->MemberManager->removeMember($member));
$this->assertEquals(0, $this->memberRelation->getMembers()->count());
$this->assertEquals(0, $member->getMemberships()->count());
}
public function testAddAndRemoveMembership(): void
{
$membership = new MemberRelation();
$this->assertNull($this->MemberManager->addMembership($membership));
$this->assertEquals($membership, $this->memberRelation->getMemberships()->get(0));
$this->assertEquals($this->memberRelation, $membership->getMembers()->get(0));
$this->assertNull($this->MemberManager->removeMembership($membership));
$this->assertEquals(0, $this->memberRelation->getMemberships()->count());
$this->assertEquals(0, $membership->getMembers()->count());
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Unit\Domain\ResponseManagement;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Meta\RightInterface;
use App\Entity\Meta\Right;
use FOS\RestBundle\View\ViewHandlerInterface;
use App\Entity\Source\PureSource;
use App\DBAL\Types\SystemSlugType;
use App\DBAL\Types\Meta\Right\LayerType;
use App\DBAL\Types\Meta\Right\CRUDType;
use App\Domain\ResponseManagement\SourceRESTResponseManager;
use App\Exception\AllreadyDefinedException;
/**
* @author kevinfrantz
*
* @todo Implement more tests!
*/
class SourceRESTReponseManagerTest extends KernelTestCase
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var RightInterface
*/
private $requestedRight;
/**
* @var ViewHandlerInterface
*/
private $viewHandler;
private function setRequestedRight(): void
{
$this->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->setType(CRUDType::READ);
$this->expectException(AllreadyDefinedException::class);
$sourceResponseManager = new SourceRESTResponseManager(null, $this->entityManager, $requestedRight, $this->viewHandler);
$sourceResponseManager->getResponse();
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace Tests\Unit\Domain\RightManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Meta\RightInterface;
use App\Entity\Meta\Right;
use App\Entity\Source\SourceInterface;
use App\DBAL\Types\Meta\Right\LayerType;
use App\Domain\RightManagement\RightCheckerInterface;
use App\Domain\RightManagement\RightChecker;
use App\DBAL\Types\Meta\Right\CRUDType;
use App\Entity\Source\PureSource;
class RightCheckerTest extends TestCase
{
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $layer;
/**
* @var SourceInterface
*/
private $source;
/**
* @var RightInterface
*/
private $right;
/**
* @var RightCheckerInterface
*/
private $rightManager;
public function setUp(): void
{
$this->layer = LayerType::MEMBER;
$this->type = CRUDType::READ;
$this->source = new PureSource();
$this->right = new Right();
$this->right->setReciever($this->source);
$this->right->setType($this->type);
$this->right->setLayer($this->layer);
$this->rightManager = new RightChecker($this->right);
}
public function testFirstDimension(): void
{
$granted = $this->rightManager->isGranted($this->layer, $this->type, $this->source);
$this->assertTrue($granted);
$notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $this->type, $this->source);
$this->assertFalse($notGranted);
$notGranted2 = $this->rightManager->isGranted($this->layer, CRUDType::UPDATE, $this->source);
$this->assertFalse($notGranted2);
$this->right->setGrant(false);
$notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $this->source);
$this->assertFalse($notGranted3);
$notGranted4 = $this->rightManager->isGranted($this->layer, $this->type, new PureSource());
$this->assertFalse($notGranted4);
}
public function testSecondDimension(): void
{
$secondSource = new PureSource();
$this->source->getMemberRelation()->getMembers()->add($secondSource->getMemberRelation());
$granted = $this->rightManager->isGranted($this->layer, $this->type, $secondSource);
$this->assertTrue($granted);
$notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $this->type, $secondSource);
$this->assertFalse($notGranted);
$notGranted2 = $this->rightManager->isGranted($this->layer, CRUDType::UPDATE, $secondSource);
$this->assertFalse($notGranted2);
$this->right->setGrant(false);
$notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $secondSource);
$this->assertFalse($notGranted3);
}
public function testThirdDimension(): void
{
$thirdSource = new PureSource();
$secondSource = new PureSource();
$secondSource->getMemberRelation()->getMembers()->add($thirdSource->getMemberRelation());
$this->source->getMemberRelation()->getMembers()->add($secondSource->getMemberRelation());
$granted = $this->rightManager->isGranted($this->layer, $this->type, $thirdSource);
$this->assertTrue($granted);
$notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $this->type, $thirdSource);
$this->assertFalse($notGranted);
$notGranted2 = $this->rightManager->isGranted($this->layer, CRUDType::UPDATE, $thirdSource);
$this->assertFalse($notGranted2);
$this->right->setGrant(false);
$notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $thirdSource);
$this->assertFalse($notGranted3);
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Tests\Unit\Domain\SecureLoadManagement;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\Common\Persistence\ObjectRepository;
use App\Entity\Source\AbstractSource;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use App\Domain\SecureLoadManagement\SecureSourceLoader;
use App\Entity\Source\Primitive\Text\TextSource;
use App\DBAL\Types\SystemSlugType;
use App\Entity\Meta\Right;
use App\DBAL\Types\Meta\Right\LayerType;
use App\DBAL\Types\Meta\Right\CRUDType;
use App\Entity\Source\Complex\UserSource;
use App\Entity\Source\Primitive\Text\TextSourceInterface;
use Doctrine\ORM\EntityManagerInterface;
/**
* @author kevinfrantz
*
* @todo Implement more tests
*/
class SecureSourceLoaderTest extends KernelTestCase
{
/**
* @var ObjectRepository
*/
private $sourceRepository;
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function setUp(): void
{
self::bootKernel();
$this->entityManager = self::$container->get('doctrine.orm.default_entity_manager');
$this->setSourceRepository();
}
private function setSourceRepository(): void
{
$this->sourceRepository = $this->entityManager->getRepository(AbstractSource::class);
}
public function testAccessDeniedException(): void
{
$requestedSource = new TextSource();
$requestedSource->setSlug(SystemSlugType::IMPRINT);
$requestedRight = new Right();
$requestedRight->setSource($requestedSource);
$requestedRight->setLayer(LayerType::SOURCE);
$requestedRight->setType(CRUDType::READ);
$requestedRight->setReciever(new UserSource());
$secureSourceLoader = new SecureSourceLoader($this->entityManager, $requestedRight);
$this->expectException(AccessDeniedHttpException::class);
$secureSourceLoader->getSource();
}
public function testGranted(): void
{
$requestedSource = new TextSource();
$requestedSource->setSlug(SystemSlugType::IMPRINT);
$requestedRight = new Right();
$requestedRight->setSource($requestedSource);
$requestedRight->setLayer(LayerType::SOURCE);
$requestedRight->setType(CRUDType::READ);
$requestedRight->setReciever($this->sourceRepository->findOneBySlug(SystemSlugType::GUEST_USER));
$secureSourceLoader = new SecureSourceLoader($this->entityManager, $requestedRight);
$this->assertInstanceOf(TextSourceInterface::class, $secureSourceLoader->getSource());
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace Tests\Unit\Domain\SecureSourceManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Source\SourceInterface;
use App\Domain\SecureManagement\SecureSourceCheckerInterface;
use App\Entity\Source\AbstractSource;
use App\Domain\SecureManagement\SecureSourceChecker;
use App\Entity\Meta\Right;
use App\DBAL\Types\Meta\Right\LayerType;
use App\DBAL\Types\Meta\Right\CRUDType;
use App\Entity\Attribut\SourceAttribut;
use App\Entity\Attribut\SourceAttributInterface;
use App\Exception\SourceAccessDenied;
/**
* @author kevinfrantz
*/
class SecureSourceCheckerTest extends TestCase
{
/**
* @var SourceInterface|SourceAttributInterface
*/
private $source;
/**
* @var SourceInterface
*/
private $recieverSource;
/**
* @var SecureSourceCheckerInterface
*/
private $securerSourceChecker;
private function createSourceMock(): SourceInterface
{
return new class() extends AbstractSource implements SourceAttributInterface {
use SourceAttribut;
};
}
public function setUp(): void
{
$this->source = $this->createSourceMock();
$this->recieverSource = $this->createSourceMock();
$this->securerSourceChecker = new SecureSourceChecker($this->source);
}
public function testFirstLevel(): void
{
$right = new Right();
$right->setLayer(LayerType::SOURCE);
$right->setType(CRUDType::UPDATE);
$right->setReciever($this->recieverSource);
$right->setSource($this->source);
$this->source->getLaw()->getRights()->add($right);
$requestedRight = clone $right;
$this->assertTrue($this->securerSourceChecker->hasPermission($requestedRight));
$requestedRight->setType(CRUDType::READ);
$this->assertFalse($this->securerSourceChecker->hasPermission($requestedRight));
}
public function testSecondLevel(): void
{
$right = new Right();
$right->setLayer(LayerType::SOURCE);
$right->setType(CRUDType::UPDATE);
$right->setReciever($this->recieverSource);
$right->setSource($this->source);
$this->source->getLaw()->getRights()->add($right);
$attributSource = $this->createSourceMock();
$childRight = clone $right;
$attributSource->getLaw()->getRights()->add($childRight);
$this->source->setSource($attributSource);
$requestedRight = clone $right;
$this->assertTrue($this->securerSourceChecker->hasPermission($requestedRight));
$childRight->setType(CRUDType::READ);
$this->expectException(SourceAccessDenied::class);
$this->securerSourceChecker->hasPermission($requestedRight);
}
public function testThirdLevel(): void
{
$right = new Right();
$right->setLayer(LayerType::SOURCE);
$right->setType(CRUDType::UPDATE);
$right->setReciever($this->recieverSource);
$right->setSource($this->source);
$this->source->getLaw()->getRights()->add($right);
$attribut1Source = $this->createSourceMock();
$attribut1Source->getLaw()->getRights()->add($right);
$this->source->setSource($attribut1Source);
$childRight = clone $right;
$attribut2Source = $this->createSourceMock();
$attribut2Source->getLaw()->getRights()->add($childRight);
$attribut1Source->setSource($attribut2Source);
$requestedRight = clone $right;
$this->assertTrue($this->securerSourceChecker->hasPermission($requestedRight));
$childRight->setType(CRUDType::READ);
$this->expectException(SourceAccessDenied::class);
$this->securerSourceChecker->hasPermission($requestedRight);
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Tests\Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Source\SourceInterface;
use App\Entity\Source\Complex\UserSource;
use App\Entity\Source\Primitive\Text\TextSource;
use App\Entity\Source\Primitive\Name\FirstNameSource;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Source\Complex\FullPersonNameSource;
use App\Domain\SourceManagement\SourceMemberInformation;
use App\Domain\SourceManagement\SourceMemberInformationInterface;
use App\Entity\Source\PureSource;
class SourceMemberInformationTest extends TestCase
{
/**
* @var SourceInterface
*/
private $source;
/**
* @var SourceMemberInformationInterface
*/
private $sourceMemberInformation;
public function setUp(): void
{
$this->source = new UserSource();
$this->sourceMemberInformation = new SourceMemberInformation($this->source);
}
public function testOneDimension(): void
{
$this->source->getMemberRelation()->getMembers()->add((new TextSource())->getMemberRelation());
$allSourceMembers = $this->sourceMemberInformation->getAllMembers();
$this->assertEquals(1, $allSourceMembers->count());
$this->assertInstanceOf(SourceInterface::class, $allSourceMembers[0]);
}
public function testThreeDimension(): void
{
$source1 = new TextSource();
$source2 = new FirstNameSource();
$source2->getMemberRelation()->setMembers(new ArrayCollection([$source1->getMemberRelation()]));
$source3 = new FullPersonNameSource();
$source3->getMemberRelation()->getMembers()->add($source2->getMemberRelation());
$this->source->getMemberRelation()->getMembers()->add($source3->getMemberRelation());
$allSourceMembers = $this->sourceMemberInformation->getAllMembers();
$this->assertEquals(3, $allSourceMembers->count());
foreach ($allSourceMembers as $sourceMember) {
$this->assertInstanceOf(SourceInterface::class, $sourceMember);
}
}
public function testRecursion(): void
{
$recursiveSource = new UserSource();
$recursiveSource->getMemberRelation()->getMembers()->add($this->source->getMemberRelation());
$this->source->getMemberRelation()->getMembers()->add($recursiveSource->getMemberRelation());
$allSourceMembers = $this->sourceMemberInformation->getAllMembers();
$this->assertEquals(2, $allSourceMembers->count());
foreach ($allSourceMembers as $sourceMember) {
$this->assertInstanceOf(SourceInterface::class, $sourceMember);
}
}
public function testError(): void
{
$this->expectException(\Error::class);
$this->source->getMemberRelation()->getMembers()->add(new PureSource());
$this->sourceMemberInformation->getAllMembers();
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Tests\Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Source\SourceInterface;
use App\Domain\SourceManagement\SourceMemberManagerInterface;
use App\Domain\SourceManagement\SourceMemberManager;
use App\Entity\Source\PureSource;
class SourceMemberManagerTest extends TestCase
{
/**
* @var SourceInterface
*/
private $source;
/**
* @var SourceMemberManagerInterface
*/
private $sourceMemberManager;
public function setUp(): void
{
$this->source = new PureSource();
$this->sourceMemberManager = new SourceMemberManager($this->source);
}
public function testAddAndRemoveMember(): void
{
$member = new PureSource();
$this->assertNull($this->sourceMemberManager->addMember($member));
$this->assertEquals($member, $this->source->getMemberRelation()->getMembers()->get(0)->getSource());
$this->assertEquals($this->source, $member->getMemberRelation()->getMemberships()->get(0)->getSource());
$this->assertNull($this->sourceMemberManager->removeMember($member));
$this->assertEquals(0, $this->source->getMemberRelation()->getMembers()->count());
$this->assertEquals(0, $member->getMemberRelation()->getMemberships()->count());
}
public function testAddAndRemoveMembership(): void
{
$membership = new PureSource();
$this->assertNull($this->sourceMemberManager->addMembership($membership));
$this->assertEquals($membership, $this->source->getMemberRelation()->getMemberships()->get(0)->getSource());
$this->assertEquals($this->source, $membership->getMemberRelation()->getMembers()->get(0)->getSource());
$this->assertNull($this->sourceMemberManager->removeMembership($membership));
$this->assertEquals(0, $this->source->getMemberRelation()->getMemberships()->count());
$this->assertEquals(0, $membership->getMemberRelation()->getMembers()->count());
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace Tests\Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\SourceManagement\SourceMembershipInformationInterface;
use App\Domain\SourceManagement\SourceMembershipInformation;
use App\Entity\Source\SourceInterface;
use App\Entity\Source\Complex\UserSource;
use App\Entity\Source\Primitive\Text\TextSource;
use App\Entity\Source\Primitive\Name\FirstNameSource;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Source\Complex\FullPersonNameSource;
class SourceMembershipInformationTest extends TestCase
{
/**
* @var SourceInterface
*/
protected $source;
/**
* @var SourceMembershipInformationInterface
*/
protected $sourceMembershipInformation;
public function setUp(): void
{
$this->source = new UserSource();
$this->sourceMembershipInformation = new SourceMembershipInformation($this->source);
}
public function testOneDimension(): void
{
$this->source->getMemberRelation()->getMemberships()->add((new TextSource())->getMemberRelation());
$this->assertEquals(1, $this->sourceMembershipInformation->getAllMemberships()->count());
}
public function testThreeDimension(): void
{
$source1 = new TextSource();
$source2 = new FirstNameSource();
$source2->getMemberRelation()->setMemberships(new ArrayCollection([$source1->getMemberRelation()]));
$source3 = new FullPersonNameSource();
$source3->getMemberRelation()->getMemberships()->add($source2->getMemberRelation());
$this->source->getMemberRelation()->getMemberships()->add($source3->getMemberRelation());
$this->assertEquals(3, $this->sourceMembershipInformation->getAllMemberships()->count());
}
public function testRecursion(): void
{
$recursiveSource = new UserSource();
$recursiveSource->getMemberRelation()->getMemberships()->add($this->source->getMemberRelation());
$this->source->getMemberRelation()->getMemberships()->add($recursiveSource->getMemberRelation());
$this->assertEquals(2, $this->sourceMembershipInformation->getAllMemberships()->count());
}
public function testError(): void
{
$this->expectException(\Error::class);
$this->source->getMemberRelation()->getMemberships()->add($this->createSourceMock());
$this->sourceMembershipInformation->getAllMemberships();
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Tests\Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\SourceManagement\SourceMetaInterface;
use App\Entity\Source\Complex\UserSource;
use App\Domain\SourceManagement\SourceMeta;
use App\Entity\Source\Complex\UserSourceInterface;
use App\Domain\TemplateManagement\TemplateMetaInterface;
use App\Entity\Source\SourceInterface;
use App\Domain\FormManagement\FormMetaInterface;
class SourceMetaTest extends TestCase
{
/**
* @var SourceMetaInterface
*/
protected $sourceMeta;
/**
* @var SourceInterface
*/
protected $source;
public function setUp(): void
{
$this->source = new UserSource();
$this->sourceMeta = new SourceMeta($this->source);
}
public function testBasicName(): void
{
$this->assertEquals('user', $this->sourceMeta->getBasicName());
$this->assertNotEquals('user2', $this->sourceMeta->getBasicName());
}
public function testBasicPath(): void
{
$subset = ['source', 'complex'];
$amount = count($subset);
$basicPathArray = $this->sourceMeta->getBasicPathArray();
for ($index = 0; $index < $amount; ++$index) {
$this->assertEquals($subset[$index], $basicPathArray[$index]);
}
$this->assertArraySubset($subset, $basicPathArray);
$this->assertEquals($amount, count($basicPathArray));
}
public function testInterfaceReflection(): void
{
/**
* @var \ReflectionClass
*/
$interfaceReflection = $this->sourceMeta->getInterfaceReflection();
$this->assertEquals(UserSourceInterface::class, $interfaceReflection->getName());
}
public function testSourceReflection(): void
{
/**
* @var \ReflectionClass
*/
$sourceReflection = $this->sourceMeta->getSourceReflection();
$this->assertEquals(UserSource::class, $sourceReflection->getName());
}
public function testTemplateMeta(): void
{
$this->assertInstanceOf(TemplateMetaInterface::class, $this->sourceMeta->getTemplateMeta());
}
public function testSource(): void
{
$this->assertEquals($this->source, $this->sourceMeta->getSource());
}
public function testFormMeta(): void
{
$this->assertInstanceOf(FormMetaInterface::class, $this->sourceMeta->getFormMeta());
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Source\SourceInterface;
use App\Domain\SourceManagement\SourceRightManagerInterface;
use App\Domain\SourceManagement\SourceRightManager;
use App\Entity\Meta\RightInterface;
use App\Entity\Meta\Right;
use App\Entity\Meta\Law;
use App\Exception\AllreadySetException;
use App\Exception\NotSetException;
use App\Exception\AllreadyDefinedException;
use App\Entity\Source\PureSource;
class SourceRightManagerTest extends TestCase
{
/**
* @var SourceInterface
*/
private $source;
/**
* @var SourceRightManagerInterface
*/
private $sourceRightManager;
/**
* @var RightInterface
*/
private $right;
public function setUp(): void
{
$this->source = new PureSource();
$this->sourceRightManager = new SourceRightManager($this->source);
$this->right = new Right();
}
public function testLawException(): void
{
$this->right->setLaw(new Law());
$this->expectException(AllreadyDefinedException::class);
$this->sourceRightManager->addRight($this->right);
}
public function testSourceException(): void
{
$this->right->setSource(new PureSource());
$this->expectException(AllreadyDefinedException::class);
$this->sourceRightManager->addRight($this->right);
}
public function testNotSetException(): void
{
$this->expectException(NotSetException::class);
$this->sourceRightManager->removeRight($this->right);
}
public function testAllreadSetException(): void
{
$this->sourceRightManager->addRight($this->right);
$this->expectException(AllreadySetException::class);
$this->sourceRightManager->addRight($this->right);
}
public function testRightAdd(): void
{
$this->assertNull($this->sourceRightManager->addRight($this->right));
$this->assertEquals($this->source, $this->right->getSource());
$this->assertEquals($this->right, $this->source->getLaw()->getRights()->get(0));
$this->assertEquals($this->right->getLaw(), $this->source->getLaw());
$this->assertNull($this->sourceRightManager->removeRight($this->right));
$this->assertNotEquals($this->source, $this->right->getSource());
$this->assertNotEquals($this->right->getLaw(), $this->source->getLaw());
$this->assertEquals(0, $this->source->getLaw()->getRights()->count());
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Tests\Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Source\Complex\Collection\TreeCollectionSource;
use App\Entity\Source\SourceInterface;
use Doctrine\Common\Collections\ArrayCollection;
use App\Domain\SourceManagement\TreeSourceServiceInterface;
use App\Domain\SourceManagement\TreeSourceService;
class TreeSourceServiceTest extends TestCase
{
/**
* @var TreeSourceServiceInterface
*/
protected $treeService;
public function setUp(): void
{
$tree1 = new TreeCollectionSource();
$tree2 = new TreeCollectionSource();
$tree3 = new TreeCollectionSource();
$tree4 = new TreeCollectionSource();
$tree5 = new TreeCollectionSource(new ArrayCollection([$tree2]));
$leave1 = $this->createMock(SourceInterface::class);
$leave2 = $this->createMock(SourceInterface::class);
$leave3 = $this->createMock(SourceInterface::class);
$leave4 = $this->createMock(SourceInterface::class);
$leave5 = $this->createMock(SourceInterface::class);
$tree2->setCollection(new ArrayCollection([$leave3, $leave4, $tree5, $leave5]));
$collection = new ArrayCollection([$tree2, $tree3, $leave1, $leave2, $tree4, $tree1]);
$tree1->setCollection($collection);
$this->treeService = new TreeSourceService($tree1);
}
public function testGetLeaves(): void
{
$this->assertEquals(2, $this->treeService->getLeaves()->count());
}
public function testGetBranches(): void
{
$this->assertEquals(4, $this->treeService->getBranches()->count());
}
public function testGetAllBranches(): void
{
$this->assertEquals(5, $this->treeService->getAllBranches()->count());
}
public function testGetAllLeaves(): void
{
$this->assertEquals(5, $this->treeService->getAllLeaves()->count());
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Tests\Unit\Domain\TemplateManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\TemplateManagement\TemplateMetaInterface;
use App\Entity\Source\Primitive\Name\FirstNameSource;
use App\Entity\Source\SourceInterface;
use App\Domain\TemplateManagement\TemplateMeta;
use App\Domain\SourceManagement\SourceMeta;
class TemplateMetaTest extends TestCase
{
/**
* @var TemplateMetaInterface
*/
protected $templateMeta;
/**
* @var SourceInterface
*/
protected $source;
private function getExpectedPath(string $type, string $context): string
{
return $context.'/entity/source/primitive/name/firstname.'.$type.'.twig';
}
public function setUp(): void
{
$this->source = new FirstNameSource();
$sourceMeta = new SourceMeta($this->source);
$this->templateMeta = new TemplateMeta($sourceMeta->getBasicPathArray(), $sourceMeta->getBasicName(), 'entity');
}
public function testFrameTemplatePath(): void
{
$this->assertEquals($this->getExpectedPath('html', 'frame'), $this->templateMeta->getFrameTemplatePath());
}
public function testContentTemplatePath(): void
{
$this->assertEquals($this->getExpectedPath('html', 'content'), $this->templateMeta->getContentTemplatePath());
}
public function testSetType(): void
{
$this->templateMeta->setTemplateType('json');
$this->assertEquals($this->getExpectedPath('json', 'content'), $this->templateMeta->getContentTemplatePath());
$this->assertEquals($this->getExpectedPath('json', 'frame'), $this->templateMeta->getFrameTemplatePath());
$this->assertEquals('json', $this->templateMeta->getTemplateType());
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Tests\Unit\Domain\UserManagement;
use App\Domain\UserManagement\UserIdentityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use App\DBAL\Types\SystemSlugType;
use App\Entity\User;
class UserIdentityManagerTest extends KernelTestCase
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function setUp(): void
{
self::bootKernel();
$this->entityManager = self::$container->get('doctrine.orm.default_entity_manager');
}
public function testGuestUser(): void
{
$origineUser = null;
$userIdentityManager = new UserIdentityManager($this->entityManager, $origineUser);
$expectedUser = $userIdentityManager->getUser();
$this->assertEquals(SystemSlugType::GUEST_USER, $expectedUser->getSource()->getSlug());
}
public function testUser(): void
{
$origineUser = new User();
$userIdentityManager = new UserIdentityManager($this->entityManager, $origineUser);
$expectedUser = $userIdentityManager->getUser();
$this->assertEquals($origineUser, $expectedUser);
}
}