mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-11-04 03:07:58 +00:00 
			
		
		
		
	Optimized CrudAttribut logic
This commit is contained in:
		@@ -2,6 +2,9 @@
 | 
			
		||||
 | 
			
		||||
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.
 | 
			
		||||
 *
 | 
			
		||||
@@ -10,6 +13,8 @@ namespace App\Entity\Attribut;
 | 
			
		||||
trait CrudAttribut
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @see CRUDType
 | 
			
		||||
     *
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    protected $crud;
 | 
			
		||||
@@ -19,6 +24,9 @@ trait CrudAttribut
 | 
			
		||||
     */
 | 
			
		||||
    public function setCrud(string $crud): void
 | 
			
		||||
    {
 | 
			
		||||
        if (!array_key_exists($crud, CRUDType::getChoices())) {
 | 
			
		||||
            throw new NoValidChoiceException();
 | 
			
		||||
        }
 | 
			
		||||
        $this->crud = $crud;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -99,19 +99,6 @@ class Right extends AbstractMeta implements RightInterface
 | 
			
		||||
        $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}
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,8 @@ namespace Tests\Unit\Entity\Attribut;
 | 
			
		||||
use PHPUnit\Framework\TestCase;
 | 
			
		||||
use App\Entity\Attribut\CrudAttributInterface;
 | 
			
		||||
use App\Entity\Attribut\CrudAttribut;
 | 
			
		||||
use App\DBAL\Types\Meta\Right\CRUDType;
 | 
			
		||||
use App\Exception\NoValidChoiceException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
@@ -14,11 +16,11 @@ class CrudAttributTest extends TestCase
 | 
			
		||||
    /**
 | 
			
		||||
     * @var CrudAttributInterface
 | 
			
		||||
     */
 | 
			
		||||
    protected $typeAttribut;
 | 
			
		||||
    protected $crudAttribut;
 | 
			
		||||
 | 
			
		||||
    public function setUp(): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->typeAttribut = new class() implements CrudAttributInterface {
 | 
			
		||||
        $this->crudAttribut = new class() implements CrudAttributInterface {
 | 
			
		||||
            use CrudAttribut;
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
@@ -26,13 +28,16 @@ class CrudAttributTest extends TestCase
 | 
			
		||||
    public function testConstructor(): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->expectException(\TypeError::class);
 | 
			
		||||
        $this->typeAttribut->getCrud();
 | 
			
		||||
        $this->crudAttribut->getCrud();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testAccessors(): void
 | 
			
		||||
    {
 | 
			
		||||
        $type = 'Hello World!';
 | 
			
		||||
        $this->assertNull($this->typeAttribut->setCrud($type));
 | 
			
		||||
        $this->assertEquals($type, $this->typeAttribut->getCrud());
 | 
			
		||||
        foreach (CRUDType::getChoices() as $enum) {
 | 
			
		||||
            $this->assertNull($this->crudAttribut->setCrud($enum));
 | 
			
		||||
            $this->assertEquals($enum, $this->crudAttribut->getCrud());
 | 
			
		||||
        }
 | 
			
		||||
        $this->expectException(NoValidChoiceException::class);
 | 
			
		||||
        $this->crudAttribut->setCrud('NoneValidType');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -73,9 +73,9 @@ class RightTest extends TestCase
 | 
			
		||||
 | 
			
		||||
    public function testRight(): void
 | 
			
		||||
    {
 | 
			
		||||
        foreach (CRUDType::getChoices() as $key => $value) {
 | 
			
		||||
            $this->assertNull($this->right->setCrud($key));
 | 
			
		||||
            $this->assertEquals($key, $this->right->getCrud());
 | 
			
		||||
        foreach (CRUDType::getChoices() as $enum) {
 | 
			
		||||
            $this->assertNull($this->right->setCrud($enum));
 | 
			
		||||
            $this->assertEquals($enum, $this->right->getCrud());
 | 
			
		||||
        }
 | 
			
		||||
        $this->expectException(NoValidChoiceException::class);
 | 
			
		||||
        $this->right->setCrud('NoneValidType');
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user