From 205c95bcb89d4402ce1911b18719e1fd47f12be2 Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Tue, 26 Mar 2019 08:46:29 +0100 Subject: [PATCH] Added Group, Icon and line Former-commit-id: d13c4cdfd03f299812c588aa41685296d35675f9 --- .../include/rest-api/models/Model.php | 4 +- .../models/VisualConsole/Container.php | 11 +- .../models/VisualConsole/items/Group.php | 86 +++++++ .../models/VisualConsole/items/Icon.php | 69 ++++++ .../models/VisualConsole/items/Line.php | 215 ++++++++++++++++++ .../Models/VisualConsole/items/GroupTest.php | 147 ++++++++++++ .../Models/VisualConsole/items/IconTest.php | 95 ++++++++ .../Models/VisualConsole/items/LineTest.php | 136 +++++++++++ 8 files changed, 760 insertions(+), 3 deletions(-) create mode 100644 pandora_console/include/rest-api/models/VisualConsole/items/Group.php create mode 100644 pandora_console/include/rest-api/models/VisualConsole/items/Icon.php create mode 100644 pandora_console/include/rest-api/models/VisualConsole/items/Line.php create mode 100644 pandora_console/tests/Functional/Models/VisualConsole/items/GroupTest.php create mode 100644 pandora_console/tests/Functional/Models/VisualConsole/items/IconTest.php create mode 100644 pandora_console/tests/Functional/Models/VisualConsole/items/LineTest.php diff --git a/pandora_console/include/rest-api/models/Model.php b/pandora_console/include/rest-api/models/Model.php index f28369e7f9..3c0a4e299a 100644 --- a/pandora_console/include/rest-api/models/Model.php +++ b/pandora_console/include/rest-api/models/Model.php @@ -67,7 +67,7 @@ abstract class Model * * @return self Instance of the model. */ - public static function fromArray(array $data): self + public static function fromArray(array $data) { // The reserved word static refers to the invoked class at runtime. return new static($data); @@ -84,7 +84,7 @@ abstract class Model * * @abstract */ - abstract protected static function fetchDataFromDB(array $filter): array; + abstract protected static function fetchDataFromDB(array $filter); /** diff --git a/pandora_console/include/rest-api/models/VisualConsole/Container.php b/pandora_console/include/rest-api/models/VisualConsole/Container.php index e5c110a4bf..39fb7e040f 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Container.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Container.php @@ -19,6 +19,15 @@ final class Container extends Model * @param array $data Input data. * * @return void + */ + public static function fromArray(array $data) + { + return new self($data); + } + + + /** + * Validate the input data * * @throws \InvalidArgumentException If any input value is considered * invalid. @@ -179,7 +188,7 @@ final class Container extends Model * * @override Model::fetchDataFromDB. */ - public static function fetchDataFromDB(array $filter): self + public static function fetchDataFromDB(array $filter) { // Due to this DB call, this function cannot be unit tested without // a proper mock. diff --git a/pandora_console/include/rest-api/models/VisualConsole/items/Group.php b/pandora_console/include/rest-api/models/VisualConsole/items/Group.php new file mode 100644 index 0000000000..461f4fc706 --- /dev/null +++ b/pandora_console/include/rest-api/models/VisualConsole/items/Group.php @@ -0,0 +1,86 @@ +extractImageSrc($data); + $return['groupId'] = $this->extractGroupId($data); + return $return; + } + + + /** + * extractBackgroundUrl + * + * @param mixed $data + * + * @return void + */ + private function extractImageSrc(array $data) + { + $imageSrc = Model::notEmptyStringOr( + Model::issetInArray($data, ['imageSrc', 'image']), + null + ); + if ($imageSrc === null) { + throw new \InvalidArgumentException( + 'the imageSrc property is required and should be string' + ); + } else { + return $imageSrc; + } + } + + + private function extractGroupId(array $data): int + { + $groupId = Model::parseIntOr( + Model::issetInArray($data, ['groupId', 'id_group']), + 0 + ); + if ($groupId === null) { + throw new \InvalidArgumentException( + 'the group Id property is required and should be integer' + ); + } else { + return $groupId; + } + } + + +} diff --git a/pandora_console/include/rest-api/models/VisualConsole/items/Icon.php b/pandora_console/include/rest-api/models/VisualConsole/items/Icon.php new file mode 100644 index 0000000000..3b2aada8b4 --- /dev/null +++ b/pandora_console/include/rest-api/models/VisualConsole/items/Icon.php @@ -0,0 +1,69 @@ +extractImageSrc($data); + return $return; + } + + + /** + * extractBackgroundUrl + * + * @param mixed $data + * + * @return void + */ + private function extractImageSrc(array $data) + { + $imageSrc = Model::notEmptyStringOr( + Model::issetInArray($data, ['imageSrc', 'image']), + null + ); + if ($imageSrc === null) { + throw new \InvalidArgumentException( + 'the imageSrc property is required and should be string' + ); + } else { + return $imageSrc; + } + } + + +} diff --git a/pandora_console/include/rest-api/models/VisualConsole/items/Line.php b/pandora_console/include/rest-api/models/VisualConsole/items/Line.php new file mode 100644 index 0000000000..10050bf914 --- /dev/null +++ b/pandora_console/include/rest-api/models/VisualConsole/items/Line.php @@ -0,0 +1,215 @@ + (int) $data['id'], + 'type' => LINE_ITEM, + 'startX' => $this->extractStartX($data), + 'startY' => $this->extractStartY($data), + 'endX' => $this->extractEndX($data), + 'endY' => $this->extractEndY($data), + 'isOnTop' => $this->extractIsOnTope($data), + 'borderWidth' => $this->extractBorderWidth($data), + 'borderColor' => $this->extractBorderColor($data), + ]; + } + + + /** + * Extract the value of startX and + * return a integer. + * + * @param mixed $data + * + * @return integer + */ + private function extractStartX(array $data): int + { + $startX = Model::parseIntOr( + Model::issetInArray($data, ['startX', 'pos_x']), + 0 + ); + return $startX; + } + + + /** + * Extract the value of startY and + * return a integer. + * + * @param mixed $data + * + * @return integer + */ + private function extractStartY(array $data): int + { + $startY = Model::parseIntOr( + Model::issetInArray($data, ['startY', 'pos_y']), + 0 + ); + return $startY; + } + + + /** + * Extract the value of endX and + * return a integer. + * + * @param mixed $data + * + * @return integer + */ + private function extractEndX(array $data): int + { + $endX = Model::parseIntOr( + Model::issetInArray($data, ['endX', 'width']), + 0 + ); + return $endX; + } + + + /** + * Extract the value of endY and + * return a integer. + * + * @param mixed $data + * + * @return integer + */ + private function extractEndY(array $data): int + { + $endY = Model::parseIntOr( + Model::issetInArray($data, ['endY', 'height']), + 0 + ); + return $endY; + } + + + /** + * Extract the value of isOnTop and + * return a bool. + * + * @param mixed $data + * + * @return boolean + */ + private function extractIsOnTope(array $data): bool + { + $isOnTop = Model::parseBool( + Model::issetInArray($data, ['isOnTop', 'show_on_top']) + ); + return $isOnTop; + } + + + /** + * Extract the value of borderWidth and + * return a integer. + * + * @param mixed $data + * + * @return integer + */ + private function extractBorderWidth(array $data): int + { + $borderWidth = Model::parseIntOr( + Model::issetInArray($data, ['borderWidth', 'border_width']), + 0 + ); + if ($borderWidth >= 0) { + return $borderWidth; + } else { + return 0; + } + } + + + /** + * Extract the value of borderColor and + * return to not empty string or null. + * + * @param mixed $data + * + * @return void + */ + private function extractBorderColor(array $data) + { + $borderColor = Model::notEmptyStringOr( + Model::issetInArray($data, ['borderColor', 'border_color']), + null + ); + return $borderColor; + } + + + /** + * Obtain a vc item data structure from the database using a filter. + * + * @param array $filter Filter of the Visual Console Item. + * + * @return array The Visual Console line data structure stored into the DB. + * @throws \Exception When the data cannot be retrieved from the DB. + * + * @override Model::fetchDataFromDB. + */ + protected static function fetchDataFromDB(array $filter): array + { + // Due to this DB call, this function cannot be unit tested without + // a proper mock. + $row = \db_get_row_filter('tlayout_data', $filter); + + if ($row === false) { + throw new \Exception('error fetching the data from the DB'); + } + + return $row; + } + + +} diff --git a/pandora_console/tests/Functional/Models/VisualConsole/items/GroupTest.php b/pandora_console/tests/Functional/Models/VisualConsole/items/GroupTest.php new file mode 100644 index 0000000000..166c5b7dfe --- /dev/null +++ b/pandora_console/tests/Functional/Models/VisualConsole/items/GroupTest.php @@ -0,0 +1,147 @@ +assertInstanceOf( + Group::class, + Group::fromArray( + [ + 'id' => 13, + 'type' => GROUP_ITEM, + 'width' => '600', + 'height' => '500', + 'imageSrc' => 'image.jpg', + 'groupId' => 12, + ] + ) + ); + + $this->assertInstanceOf( + Group::class, + Group::fromArray( + [ + 'id' => 1004, + 'type' => GROUP_ITEM, + 'width' => '600', + 'height' => '500', + 'image' => 'test_image.png', + 'id_group' => 0, + ] + ) + ); + } + + + public function testCannotBeCreatedWithInvalidImageSrc(): void + { + $this->expectException(InvalidArgumentException::class); + // Invalid imageSrc. + Group::fromArray( + [ + 'id' => 7, + 'type' => GROUP_ITEM, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'imageSrc' => '', + 'id_group' => 0, + ] + ); + // Missing imageSrc. + Group::fromArray( + [ + 'id' => 7, + 'type' => GROUP_ITEM, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'id_group' => 0, + ] + ); + } + + + public function testCannotBeCreatedWithInvalidGroupId(): void + { + $this->expectException(InvalidArgumentException::class); + // Invalid groupId. + Group::fromArray( + [ + 'id' => 7, + 'type' => GROUP_ITEM, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'imageSrc' => 'test.jpg', + 'groupId' => 'bar', + ] + ); + // Missing groupId. + Group::fromArray( + [ + 'id' => 7, + 'type' => GROUP_ITEM, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'imageSrc' => 'test.jpg', + ] + ); + } + + + public function testContainerIsRepresentedAsJson(): void + { + $this->assertEquals( + '{"id":1,"type"11,"label":null,"labelPosition":"down","isLinkEnabled":false,"isOnTop":false,"parentId":null,"aclGroupId":null,"width":0,"height":0,"x":0,"y":0,"imageSrc":"image.jpg","groupId":0}', + Group::fromArray( + [ + 'id' => 1, + 'type' => GROUP_ITEM, + 'width' => '0', + 'height' => '0', + 'imageSrc' => 'image.jpg', + 'groupId' => 0, + ] + ) + ); + } + + +} diff --git a/pandora_console/tests/Functional/Models/VisualConsole/items/IconTest.php b/pandora_console/tests/Functional/Models/VisualConsole/items/IconTest.php new file mode 100644 index 0000000000..9ccdb946b1 --- /dev/null +++ b/pandora_console/tests/Functional/Models/VisualConsole/items/IconTest.php @@ -0,0 +1,95 @@ +assertInstanceOf( + Icon::class, + Icon::fromArray( + [ + 'id' => 69, + 'type' => ICON, + 'width' => '0', + 'height' => '0', + 'imageSrc' => 'image.jpg', + ] + ) + ); + } + + + public function testCannotBeCreatedWithInvalidImageSrc(): void + { + $this->expectException(InvalidArgumentException::class); + // Invalid imageSrc. + Icon::fromArray( + [ + 'id' => 7, + 'type' => ICON, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'imageSrc' => '', + ] + ); + // Missing imageSrc. + Icon::fromArray( + [ + 'id' => 7, + 'type' => ICON, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + ] + ); + } + + + public function testContainerIsRepresentedAsJson(): void + { + $this->assertEquals( + '{"id":7,"type":5,"label":null,"labelPosition":"up","isLinkEnabled":true,"isOnTop":false,"parentId":null,"aclGroupId":null,"width":0,"height":0,"x":-666,"y":76,"imageSrc":"image.jpg"}', + Icon::fromArray( + [ + 'id' => 7, + 'type' => ICON, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'imageSrc' => 'image.jpg', + ] + ) + ); + } + + +} diff --git a/pandora_console/tests/Functional/Models/VisualConsole/items/LineTest.php b/pandora_console/tests/Functional/Models/VisualConsole/items/LineTest.php new file mode 100644 index 0000000000..97d9e9dbf5 --- /dev/null +++ b/pandora_console/tests/Functional/Models/VisualConsole/items/LineTest.php @@ -0,0 +1,136 @@ +assertInstanceOf( + Line::class, + Line::fromArray( + [ + 'id' => 10, + 'type' => LINE_ITEM, + 'startX' => 50, + 'startY' => 100, + 'endX' => 0, + 'endY' => 10, + 'isOnTop' => false, + 'borderWidth' => 0, + 'borderColor' => 'white', + ] + ) + ); + + $this->assertInstanceOf( + Line::class, + Line::fromArray( + [ + 'id' => 10, + 'type' => LINE_ITEM, + 'startX' => 50, + 'endY' => 10, + 'borderColor' => 'black', + ] + ) + ); + } + + + public function testCannotBeCreatedWithInvalidId(): void + { + $this->expectException(InvalidArgumentException::class); + // Invalid id. + Line::fromArray( + [ + 'id' => 'foo', + 'type' => LINE_ITEM, + 'startX' => 50, + 'startY' => 100, + 'endX' => 0, + 'endY' => 10, + 'isOnTop' => false, + 'borderWidth' => 0, + 'borderColor' => 'white', + ] + ); + // Missing id. + Line::fromArray( + [ + 'type' => LINE_ITEM, + 'startX' => 50, + 'startY' => 100, + 'endX' => 0, + 'endY' => 10, + 'isOnTop' => false, + 'borderWidth' => 0, + 'borderColor' => 'white', + ] + ); + } + + + public function testCannotBeCreatedWithInvalidtype(): void + { + $this->expectException(InvalidArgumentException::class); + // Invalid type. + Line::fromArray( + [ + 'id' => 13, + 'type' => 'test', + 'startX' => 50, + 'startY' => 100, + 'endX' => 0, + 'endY' => 10, + 'isOnTop' => false, + 'borderWidth' => 0, + 'borderColor' => 'white', + ] + ); + // Missing type. + Line::fromArray( + [ + 'id' => 13, + 'startX' => 50, + 'startY' => 100, + 'endX' => 0, + 'endY' => 10, + 'isOnTop' => true, + 'borderWidth' => 0, + 'borderColor' => 'white', + ] + ); + } + + + public function testContainerIsRepresentedAsJson(): void + { + $this->assertEquals( + '{"id":1,"type":13,"startX":50,"startY":100,"endX":0,"endY":10,"isOnTop":false,"borderWidth":0,"borderColor":"white"}', + Line::fromArray( + [ + 'id' => 1, + 'type' => LINE_ITEM, + 'startX' => 50, + 'startY' => 100, + 'endX' => 0, + 'endY' => 10, + 'isOnTop' => false, + 'borderWidth' => 0, + 'borderColor' => 'white', + ] + ) + ); + } + + +}