Optimized CrudAttribut logic

This commit is contained in:
Kevin Frantz 2019-01-16 21:36:05 +01:00
parent 93bf246915
commit 6897b44aec
4 changed files with 22 additions and 22 deletions

View File

@ -2,6 +2,9 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use App\Exception\NoValidChoiceException;
use App\DBAL\Types\Meta\Right\CRUDType;
/** /**
* @todo Implement a trait for crud which substitute this one. * @todo Implement a trait for crud which substitute this one.
* *
@ -10,6 +13,8 @@ namespace App\Entity\Attribut;
trait CrudAttribut trait CrudAttribut
{ {
/** /**
* @see CRUDType
*
* @var string * @var string
*/ */
protected $crud; protected $crud;
@ -19,6 +24,9 @@ trait CrudAttribut
*/ */
public function setCrud(string $crud): void public function setCrud(string $crud): void
{ {
if (!array_key_exists($crud, CRUDType::getChoices())) {
throw new NoValidChoiceException();
}
$this->crud = $crud; $this->crud = $crud;
} }

View File

@ -99,19 +99,6 @@ class Right extends AbstractMeta implements RightInterface
$this->priority = 0; $this->priority = 0;
} }
/**
* {@inheritdoc}
*
* @see \App\Entity\Attribut\CrudAttributInterface::setCrud()
*/
public function setCrud(string $crud): void
{
if (!array_key_exists($crud, CRUDType::getChoices())) {
throw new NoValidChoiceException();
}
$this->crud = $crud;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
* *

View File

@ -5,6 +5,8 @@ namespace Tests\Unit\Entity\Attribut;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use App\Entity\Attribut\CrudAttributInterface; use App\Entity\Attribut\CrudAttributInterface;
use App\Entity\Attribut\CrudAttribut; use App\Entity\Attribut\CrudAttribut;
use App\DBAL\Types\Meta\Right\CRUDType;
use App\Exception\NoValidChoiceException;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -14,11 +16,11 @@ class CrudAttributTest extends TestCase
/** /**
* @var CrudAttributInterface * @var CrudAttributInterface
*/ */
protected $typeAttribut; protected $crudAttribut;
public function setUp(): void public function setUp(): void
{ {
$this->typeAttribut = new class() implements CrudAttributInterface { $this->crudAttribut = new class() implements CrudAttributInterface {
use CrudAttribut; use CrudAttribut;
}; };
} }
@ -26,13 +28,16 @@ class CrudAttributTest extends TestCase
public function testConstructor(): void public function testConstructor(): void
{ {
$this->expectException(\TypeError::class); $this->expectException(\TypeError::class);
$this->typeAttribut->getCrud(); $this->crudAttribut->getCrud();
} }
public function testAccessors(): void public function testAccessors(): void
{ {
$type = 'Hello World!'; foreach (CRUDType::getChoices() as $enum) {
$this->assertNull($this->typeAttribut->setCrud($type)); $this->assertNull($this->crudAttribut->setCrud($enum));
$this->assertEquals($type, $this->typeAttribut->getCrud()); $this->assertEquals($enum, $this->crudAttribut->getCrud());
}
$this->expectException(NoValidChoiceException::class);
$this->crudAttribut->setCrud('NoneValidType');
} }
} }

View File

@ -73,9 +73,9 @@ class RightTest extends TestCase
public function testRight(): void public function testRight(): void
{ {
foreach (CRUDType::getChoices() as $key => $value) { foreach (CRUDType::getChoices() as $enum) {
$this->assertNull($this->right->setCrud($key)); $this->assertNull($this->right->setCrud($enum));
$this->assertEquals($key, $this->right->getCrud()); $this->assertEquals($enum, $this->right->getCrud());
} }
$this->expectException(NoValidChoiceException::class); $this->expectException(NoValidChoiceException::class);
$this->right->setCrud('NoneValidType'); $this->right->setCrud('NoneValidType');