Visual Console Refactor: added the label model

Former-commit-id: d1c26e023fd9c75f3ae9ab0f068e301b1e71d701
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-03 15:18:29 +02:00
parent 3df4c08782
commit 5ff1a9059a
2 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Models\VisualConsole\Items;
use Models\VisualConsole\Item;
/**
* Model of a label item of the Visual Console.
*/
final class Label extends Item
{
/**
* Used to enable the fetching, validation and extraction of information
* about the linked visual console.
*
* @var boolean
*/
protected static $useLinkedVisualConsole = true;
/**
* Validate the received data structure to ensure if we can extract the
* values required to build the model.
*
* @param array $data Input data.
*
* @return void
*
* @throws \InvalidArgumentException If any input value is considered
* invalid.
*
* @overrides Item->validateData.
*/
protected function validateData(array $data): void
{
parent::validateData($data);
if (static::notEmptyStringOr($data['label'], null) === null) {
throw new \InvalidArgumentException(
'the label property is required and should be a not empty string'
);
}
}
/**
* Returns a valid representation of the model.
*
* @param array $data Input data.
*
* @return array Data structure representing the model.
*
* @overrides Item->decode.
*/
protected function decode(array $data): array
{
$return = parent::decode($data);
$return['type'] = LABEL;
return $return;
}
}

View File

@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Models\VisualConsole\Items\Label;
/**
* Test for the Visual Console Label Item model.
*/
class LabelTest extends TestCase
{
/**
* Test if the instance is created using a valid data structure.
*
* @return void
*/
public function testCanBeCreatedFromValidUserStructure(): void
{
$this->assertInstanceOf(
Label::class,
Label::fromArray(
[
'id' => 69,
'type' => LABEL,
'width' => '0',
'height' => '0',
'label' => 'Label',
]
)
);
}
/**
* Test if the instance is not created when using a invalid label.
*
* @return void
*/
public function testCannotBeCreatedWithInvalidLabel(): void
{
$this->expectException(InvalidArgumentException::class);
// Missing label.
Label::fromArray(
[
'id' => 7,
'type' => LABEL,
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
]
);
// Empty label.
Label::fromArray(
[
'id' => 7,
'type' => LABEL,
'label' => '',
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
]
);
}
/**
* Test if the model has a valid JSON representation.
*
* @return void
*/
public function testContainerIsRepresentedAsJson(): void
{
$this->assertEquals(
'{"aclGroupId":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":"Label","labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","parentId":null,"type":4,"width":0,"x":-666,"y":76}',
(string) Label::fromArray(
[
'id' => 7,
'type' => LABEL,
'label' => 'Label',
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
]
)
);
// With a linked layout.
$this->assertEquals(
'{"aclGroupId":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":"Label","labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":1,"linkedLayoutStatusType":"default","parentId":null,"type":4,"width":0,"x":-666,"y":76}',
(string) Label::fromArray(
[
'id' => 7,
'type' => LABEL,
'label' => 'Label',
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
'id_layout_linked' => 1,
]
)
);
}
}