Implemented ClassAttribut

This commit is contained in:
Kevin Frantz 2019-02-03 12:34:29 +01:00
parent f821293062
commit d00d70f440
3 changed files with 102 additions and 0 deletions

View File

@ -0,0 +1,39 @@
<?php
namespace App\Attribut;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @see ClassAttributInterface
*
* @author kevinfrantz
*/
trait ClassAttribut
{
/**
* @var string
*/
private $class;
/**
* @param string $class
*/
public function setClass(string $class): void
{
if (class_exists($class)) {
$this->class = $class;
return;
}
throw new NotFoundHttpException('Class '.$class.' couldn\'t be found!');
}
/**
* @return string
*/
public function getClass(): string
{
return $this->class;
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Attribut;
/**
* @author kevinfrantz
*/
interface ClassAttributInterface
{
/**
* @param string $class
*/
public function setClass(string $class): void;
/**
* @return string
*/
public function getClass(): string;
}

View File

@ -0,0 +1,44 @@
<?php
namespace Attribut;
use PHPUnit\Framework\TestCase;
use App\Attribut\ClassAttributInterface;
use App\Attribut\ClassAttribut;
use App\Entity\Source\AbstractSource;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ClassAttributTest extends TestCase
{
/**
* @var ClassAttributInterface
*/
protected $classAttribut;
public function setUp(): void
{
$this->classAttribut = new class() implements ClassAttributInterface {
use ClassAttribut;
};
}
public function testConstructor(): void
{
$this->expectException(\TypeError::class);
$this->classAttribut->getClass();
}
public function testAccessors(): void
{
$class = AbstractSource::class;
$this->assertNull($this->classAttribut->setClass($class));
$this->assertEquals($class, $this->classAttribut->getClass());
}
public function testException(): void
{
$class = 'NOTEXISTINGCLASS';
$this->expectException(NotFoundHttpException::class);
$this->classAttribut->setClass($class);
}
}