diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/Label.php b/pandora_console/include/rest-api/models/VisualConsole/Items/Label.php new file mode 100644 index 0000000000..04a124e36c --- /dev/null +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/Label.php @@ -0,0 +1,64 @@ +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; + } + + +} diff --git a/pandora_console/tests/Functional/Models/VisualConsole/Items/LabelTest.php b/pandora_console/tests/Functional/Models/VisualConsole/Items/LabelTest.php new file mode 100644 index 0000000000..2c1fd963cd --- /dev/null +++ b/pandora_console/tests/Functional/Models/VisualConsole/Items/LabelTest.php @@ -0,0 +1,129 @@ +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, + ] + ) + ); + + } + + +}