Implemented tests for EntityDomService and refactored code

This commit is contained in:
Kevin Frantz 2019-04-02 18:52:11 +02:00
parent 70a117c927
commit 62aaa2d686
4 changed files with 63 additions and 5 deletions

View File

@ -2,6 +2,11 @@
namespace Infinito\Attribut;
/**
* @author kevinfrantz
*
* @see TextAttributInterface
*/
trait TextAttribut
{
/**
@ -9,11 +14,17 @@ trait TextAttribut
*/
protected $text;
/**
* @return string
*/
public function getText(): string
{
return $this->text;
}
/**
* @param string $text
*/
public function setText(string $text): void
{
$this->text = $text;

View File

@ -9,6 +9,11 @@ use Infinito\Entity\UserInterface;
*/
interface UserAttributInterface
{
/**
* @var string
*/
public const USER_ATTRIBUT_NAME = 'user';
/**
* @param UserInterface $user
*/

View File

@ -120,15 +120,12 @@ final class EntityDomService implements EntityDomServiceInterface
{
foreach (LayerInterfaceMap::getAllInterfaces() as $layer => $interface) {
if ($value instanceof $interface) {
$domElement->setAttribute('layer', $layer);
$domElement->setAttribute('id', $value->getId());
$domElement->setAttribute('name', LayerType::getReadableValue($layer));
$this->setLayerDomElement($domElement, $layer, $value);
return;
}
if ($value instanceof UserInterface) {
$domElement->setAttribute('value', $value->getId());
$domElement->setAttribute('name', 'user');
$this->setUserDomElement($domElement, $value);
return;
}
@ -142,6 +139,27 @@ final class EntityDomService implements EntityDomServiceInterface
return;
}
/**
* @param \DOMElement $domElement
* @param string $layer
* @param EntityInterface $entity
*/
private function setLayerDomElement(\DOMElement $domElement, string $layer, EntityInterface $entity): void
{
$domElement->setAttribute('layer', $layer);
$domElement->setAttribute('id', $entity->getId());
$domElement->setAttribute('name', LayerType::getReadableValue($layer));
}
/**
* @param \DomElement $domElement
*/
private function setUserDomElement(\DomElement $domElement, \Infinito\Entity\UserInterface $user): void
{
$domElement->setAttribute('value', $user->getId());
$domElement->setAttribute('name', 'user');
}
/**
* @param string $method
* @param string $propertyName

View File

@ -21,6 +21,9 @@ use Infinito\Entity\EntityInterface;
use Infinito\Attribut\VersionAttribut;
use Infinito\Attribut\IdAttribut;
use Infinito\Exception\NotCorrectInstanceException;
use Infinito\Entity\Source\Complex\UserSource;
use Infinito\Entity\User;
use Infinito\Attribut\UserAttributInterface;
/**
* @author kevinfrantz
@ -66,6 +69,27 @@ class EntityDomServiceTest extends TestCase
$this->entityDomService->getDomDocument();
}
public function testUserSource(): void
{
$userSource = new UserSource();
$userId = 1234;
$user = new User();
$user->setId($userId);
$userSource->setUser($user);
$this->requestedEntityService->method('getEntity')->willReturn($userSource);
$result = $this->entityDomService->getDomDocument();
foreach ($result->childNodes as $attribut) {
$name = $attribut->getAttribute('name');
$value = $attribut->getAttribute('value');
if (UserAttributInterface::USER_ATTRIBUT_NAME === $name) {
$this->assertEquals($userId, $value);
return;
}
}
$this->assertTrue(false, 'The user attribut was not defined!');
}
public function testAbstractSource(): void
{
$slug = 'test';