Optimized tests for fixture sources

This commit is contained in:
Kevin Frantz 2019-03-31 16:59:46 +02:00
parent 0115751e98
commit 108debf6bf
9 changed files with 89 additions and 16 deletions

View File

@ -6,6 +6,8 @@ use Infinito\Entity\Meta\Right;
use Infinito\DBAL\Types\Meta\Right\LayerType; use Infinito\DBAL\Types\Meta\Right\LayerType;
use Infinito\DBAL\Types\ActionType; use Infinito\DBAL\Types\ActionType;
use Infinito\Entity\Source\SourceInterface; use Infinito\Entity\Source\SourceInterface;
use Infinito\Entity\Meta\RightInterface;
use Infinito\Entity\Meta\LawInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -13,18 +15,30 @@ use Infinito\Entity\Source\SourceInterface;
final class EntityTemplateFactory extends Right final class EntityTemplateFactory extends Right
{ {
/** /**
* @param SourceInterface $source * @param LawInterface $law
* @param RightInterface $right
*/ */
public static function createStandartPublicRight(SourceInterface $source): Right private static function addRightToLaw(LawInterface $law, RightInterface $right)
{ {
$right = new Right();
$law = $source->getLaw();
$right->setLaw($law); $right->setLaw($law);
$law->getRights()->add($right); $law->getRights()->add($right);
$right->setSource($source); }
$right->setLayer(LayerType::SOURCE);
$right->setActionType(ActionType::READ);
return $right; /**
* @param SourceInterface $source
*/
public static function createStandartPublicRights(SourceInterface $source): void
{
$law = $source->getLaw();
$readRight = new Right();
self::addRightToLaw($law, $readRight);
$readRight->setSource($source);
$readRight->setLayer(LayerType::SOURCE);
$readRight->setActionType(ActionType::READ);
$executeRight = new Right();
self::addRightToLaw($law, $executeRight);
$executeRight->setSource($source);
$executeRight->setLayer(LayerType::SOURCE);
$executeRight->setActionType(ActionType::EXECUTE);
} }
} }

View File

@ -22,7 +22,7 @@ final class GuestUserFixtureSource extends AbstractFixtureSource
{ {
$userSource = new UserSource(); $userSource = new UserSource();
$userSource->setSlug(self::getSlug()); $userSource->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRight($userSource); EntityTemplateFactory::createStandartPublicRights($userSource);
return $userSource; return $userSource;
} }

View File

@ -21,7 +21,7 @@ final class HelpFixtureSource extends AbstractFixtureSource
$helpSource = new TextSource(); $helpSource = new TextSource();
$helpSource->setText('See https://github.com/KevinFrantz/infinito/issues.'); $helpSource->setText('See https://github.com/KevinFrantz/infinito/issues.');
$helpSource->setSlug(self::getSlug()); $helpSource->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRight($helpSource); EntityTemplateFactory::createStandartPublicRights($helpSource);
return $helpSource; return $helpSource;
} }

View File

@ -21,7 +21,7 @@ final class HomepageFixtureSource extends AbstractFixtureSource
$homepage = new TextSource(); $homepage = new TextSource();
$homepage->setText('Welcome to infinito!'); $homepage->setText('Welcome to infinito!');
$homepage->setSlug(self::getSlug()); $homepage->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRight($homepage); EntityTemplateFactory::createStandartPublicRights($homepage);
return $homepage; return $homepage;
} }

View File

@ -21,7 +21,7 @@ final class ImpressumFixtureSource extends AbstractFixtureSource
$impressumSource = new TextSource(); $impressumSource = new TextSource();
$impressumSource->setText('Example Impressum'); $impressumSource->setText('Example Impressum');
$impressumSource->setSlug(self::getSlug()); $impressumSource->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRight($impressumSource); EntityTemplateFactory::createStandartPublicRights($impressumSource);
return $impressumSource; return $impressumSource;
} }

View File

@ -21,7 +21,7 @@ final class InformationFixtureSource extends AbstractFixtureSource
$informationSource = new TextSource(); $informationSource = new TextSource();
$informationSource->setText('See https://github.com/KevinFrantz/infinito/issues.'); $informationSource->setText('See https://github.com/KevinFrantz/infinito/issues.');
$informationSource->setSlug(self::getSlug()); $informationSource->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRight($informationSource); EntityTemplateFactory::createStandartPublicRights($informationSource);
return $informationSource; return $informationSource;
} }

View File

@ -0,0 +1,44 @@
<?php
namespace tests\Integration\Domain\FixtureManagement;
use PHPUnit\Framework\TestCase;
use Infinito\Entity\Source\AbstractSource;
use Infinito\Domain\FixtureManagement\EntityTemplateFactory;
use Infinito\Entity\Source\Complex\UserSource;
use Infinito\Entity\Meta\Right;
use Infinito\DBAL\Types\Meta\Right\LayerType;
use Infinito\DBAL\Types\ActionType;
use Infinito\Domain\LawManagement\LawPermissionChecker;
/**
* @author kevinfrantz
*/
class EntityTemplateFactoryIntegrationTest extends TestCase
{
public function testStandartPublicRights(): void
{
$allowedActions = [ActionType::READ, ActionType::EXECUTE];
$allowedLayers = [LayerType::SOURCE];
$source = new class() extends AbstractSource {
};
$law = $source->getLaw();
$anonymUserSource = new UserSource();
EntityTemplateFactory::createStandartPublicRights($source);
$requestedRight = new Right();
$requestedRight->setReciever($anonymUserSource);
$lawPermissionChecker = new LawPermissionChecker($law);
foreach (LayerType::getValues() as $layerType) {
foreach (ActionType::getValues() as $actionType) {
$requestedRight->setActionType($actionType);
$requestedRight->setLayer($layerType);
$checkResult = $lawPermissionChecker->hasPermission($requestedRight);
if (in_array($actionType, $allowedActions) && in_array($layerType, $allowedLayers)) {
$this->assertTrue($checkResult);
} else {
$this->assertFalse($checkResult);
}
}
}
}
}

View File

@ -10,7 +10,7 @@ use Infinito\Entity\Source\SourceInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
class FixtureSourceFactoryTest extends TestCase class FixtureSourceFactoryIntegrationTest extends TestCase
{ {
/** /**
* @var array|FixtureSourceInterface[] * @var array|FixtureSourceInterface[]
@ -39,12 +39,27 @@ class FixtureSourceFactoryTest extends TestCase
} }
} }
public function testFixtureSourcesIcons(): void
{
$icons = [];
foreach ($this->fixtureSources as $fixtureSource) {
$this->assertInstanceOf(FixtureSourceInterface::class, $fixtureSource);
$icon = $fixtureSource->getIcon();
$this->assertIsString($icon);
$this->assertFalse(in_array($icon, $icons), 'An icon has to be unique');
$icons[] = $icon;
}
}
/**
* The following test is redundant.
*/
public function testFixtureSourcesObjects(): void public function testFixtureSourcesObjects(): void
{ {
$objects = []; $objects = [];
foreach ($this->fixtureSources as $fixtureSource) { foreach ($this->fixtureSources as $fixtureSource) {
$this->assertInstanceOf(SourceInterface::class, $fixtureSource->getORMReadyObject()); $this->assertInstanceOf(SourceInterface::class, $fixtureSource->getORMReadyObject());
$this->assertFalse(in_array($fixtureSource, $objects), 'A slug has to be unique'); $this->assertFalse(in_array($fixtureSource, $objects), 'A object has to be unique');
$objects[] = $fixtureSource; $objects[] = $fixtureSource;
} }
} }

View File

@ -9,7 +9,7 @@ use Infinito\Domain\FixtureManagement\FixtureSource\ImpressumFixtureSource;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
class ImprintFixtureSourceTest extends KernelTestCase class ImprintFixtureSourceIntegrationTest extends KernelTestCase
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}