Implemented Type and Layer validation for right

This commit is contained in:
Kevin Frantz 2018-12-29 16:38:31 +01:00
parent 0aac336609
commit 0c66e7a42a
3 changed files with 43 additions and 3 deletions

View File

@ -16,6 +16,7 @@ use App\Entity\Attribut\LayerAttribut;
use App\Entity\Attribut\RelationAttribut; use App\Entity\Attribut\RelationAttribut;
use App\Entity\Attribut\PriorityAttribut; use App\Entity\Attribut\PriorityAttribut;
use App\Entity\Source\SourceInterface; use App\Entity\Source\SourceInterface;
use App\Exception\NoValidChoice;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -95,4 +96,20 @@ class Right extends AbstractMeta implements RightInterface
$this->grant = true; $this->grant = true;
$this->priority = 0; $this->priority = 0;
} }
public function setType(string $type): void
{
if (!array_key_exists($type, RightType::getChoices())) {
throw new NoValidChoice();
}
$this->type = $type;
}
public function setLayer(string $layer): void
{
if (!array_key_exists($layer, LayerType::getChoices())) {
throw new NoValidChoice();
}
$this->layer = $layer;
}
} }

View File

@ -0,0 +1,7 @@
<?php
namespace App\Exception;
class NoValidChoice extends \Exception
{
}

View File

@ -7,6 +7,8 @@ use App\DBAL\Types\RightType;
use App\Entity\Meta\RightInterface; use App\Entity\Meta\RightInterface;
use App\Entity\Meta\Right; use App\Entity\Meta\Right;
use App\Entity\Meta\Law; use App\Entity\Meta\Law;
use App\DBAL\Types\LayerType;
use App\Exception\NoValidChoice;
/** /**
* @todo Implement reciever test * @todo Implement reciever test
@ -18,7 +20,7 @@ class RightTest extends TestCase
/** /**
* @var RightInterface * @var RightInterface
*/ */
protected $right; private $right;
public function setUp(): void public function setUp(): void
{ {
@ -70,7 +72,21 @@ class RightTest extends TestCase
public function testRight(): void public function testRight(): void
{ {
$this->assertNull($this->right->setType(RightType::READ)); foreach (RightType::getChoices() as $key => $value) {
$this->assertEquals(RightType::READ, $this->right->getType()); $this->assertNull($this->right->setType($key));
$this->assertEquals($key, $this->right->getType());
}
$this->expectException(NoValidChoice::class);
$this->right->setType('NoneValidType');
}
public function testLayer(): void
{
foreach (LayerType::getChoices() as $key => $value) {
$this->assertNull($this->right->setLayer($key));
$this->assertEquals($key, $this->right->getLayer());
}
$this->expectException(NoValidChoice::class);
$this->right->setLayer('NoneValidLayer');
} }
} }