Skip to content

Commit

Permalink
Widgets tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed May 29, 2020
1 parent 870c6b3 commit e34529f
Show file tree
Hide file tree
Showing 38 changed files with 1,344 additions and 25 deletions.
10 changes: 9 additions & 1 deletion src/Controllers/WidgetsV1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use FastyBird\NodeWebServer\Exceptions as NodeWebServerExceptions;
use FastyBird\NodeWebServer\Http as NodeWebServerHttp;
use FastyBird\UINode\Controllers;
use FastyBird\UINode\Exceptions\InvalidArgumentException;
use FastyBird\UINode\Hydrators;
use FastyBird\UINode\Models;
use FastyBird\UINode\Queries;
Expand Down Expand Up @@ -209,7 +210,6 @@ public function create(
);

} catch (Throwable $ex) {
Debugger::log($ex);
// Revert all changes when error occur
$this->getOrmConnection()->rollBack();

Expand Down Expand Up @@ -396,6 +396,14 @@ public function readRelationship(
if ($relationEntity === Schemas\Widgets\WidgetSchema::RELATIONSHIPS_GROUPS) {
return $response
->withEntity(NodeWebServerHttp\ScalarEntity::from($widget->getGroups()));

} elseif ($relationEntity === Schemas\Widgets\WidgetSchema::RELATIONSHIPS_DISPLAY) {
return $response
->withEntity(NodeWebServerHttp\ScalarEntity::from($widget->getDisplay()));

} elseif ($relationEntity === Schemas\Widgets\WidgetSchema::RELATIONSHIPS_DATA_SOURCES) {
return $response
->withEntity(NodeWebServerHttp\ScalarEntity::from($widget->getDataSources()));
}

$this->throwUnknownRelation($relationEntity);
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Widgets/AnalogActuator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AnalogActuator extends Actuator implements IAnalogActuator
{

/** @var string[] */
public static $allowedDisplay = [
protected $allowedDisplay = [
Entities\Widgets\Display\ISlider::class,
];

Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Widgets/AnalogSensor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AnalogSensor extends Sensor implements IAnalogSensor
{

/** @var string[] */
public static $allowedDisplay = [
protected $allowedDisplay = [
Entities\Widgets\Display\IChartGraph::class,
Entities\Widgets\Display\IGauge::class,
Entities\Widgets\Display\IAnalogValue::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Widgets/DigitalActuator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DigitalActuator extends Actuator implements IDigitalActuator
{

/** @var string[] */
public static $allowedDisplay = [
protected $allowedDisplay = [
Entities\Widgets\Display\IButton::class,
];

Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Widgets/DigitalSensor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DigitalSensor extends Sensor implements IDigitalSensor
{

/** @var string[] */
public static $allowedDisplay = [
protected $allowedDisplay = [
Entities\Widgets\Display\IDigitalValue::class,
Entities\Widgets\Display\IChartGraph::class,
];
Expand Down
9 changes: 6 additions & 3 deletions src/Entities/Widgets/Display/Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ abstract class Display extends NodeDatabaseEntities\Entity implements IDisplay

/**
* @param Entities\Widgets\IWidget $widget
* @param Uuid\UuidInterface|null $id
*
* @throws Throwable
*/
public function __construct(Entities\Widgets\IWidget $widget)
{
$this->id = Uuid\Uuid::uuid4();
public function __construct(
Entities\Widgets\IWidget $widget,
?Uuid\UuidInterface $id = null
) {
$this->id = $id ?? Uuid\Uuid::uuid4();

$this->widget = $widget;
}
Expand Down
13 changes: 10 additions & 3 deletions src/Entities/Widgets/Display/Slider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use Doctrine\ORM\Mapping as ORM;
use FastyBird\UINode\Entities;
use Ramsey\Uuid;
use Throwable;

/**
Expand All @@ -35,12 +36,18 @@ class Slider extends Display implements ISlider
* @param float $minimumValue
* @param float $maximumValue
* @param float $stepValue
* @param Uuid\UuidInterface|null $id
*
* @throws Throwable
*/
public function __construct(Entities\Widgets\IWidget $widget, float $minimumValue, float $maximumValue, float $stepValue)
{
parent::__construct($widget);
public function __construct(
Entities\Widgets\IWidget $widget,
float $minimumValue,
float $maximumValue,
float $stepValue,
?Uuid\UuidInterface $id = null
) {
parent::__construct($widget, $id);

$this->setMinimumValue($minimumValue);
$this->setMaximumValue($maximumValue);
Expand Down
18 changes: 13 additions & 5 deletions src/Entities/Widgets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
* "digital_sensor" = "FastyBird\UINode\Entities\Widgets\DigitalSensor"
* })
* @ORM\MappedSuperclass
*
* @property-read string[] $allowedDisplay
*/
abstract class Widget extends NodeDatabaseEntities\Entity implements IWidget
{
Expand Down Expand Up @@ -93,13 +95,11 @@ abstract class Widget extends NodeDatabaseEntities\Entity implements IWidget
* @var Common\Collections\Collection<int, Entities\Widgets\DataSources\IDataSource>
*
* @IPubDoctrine\Crud(is={"writable"})
* @ORM\OneToMany(targetEntity="FastyBird\UINode\Entities\Widgets\DataSources\DataSource", mappedBy="widget", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OneToMany(targetEntity="FastyBird\UINode\Entities\Widgets\DataSources\DataSource", mappedBy="widget", cascade={"persist", "remove"},
* orphanRemoval=true)
*/
protected $dataSources;

/** @var string[] */
public static $allowedDisplay = [];

/**
* @param string $name
* @param Uuid\UuidInterface|null $id
Expand Down Expand Up @@ -139,7 +139,15 @@ public function getName(): string
*/
public function setDisplay(Entities\Widgets\Display\IDisplay $display): void
{
if (!in_array(get_class($display), self::$allowedDisplay, true)) {
$isAllowed = false;

foreach ($this->allowedDisplay as $allowedClass) {
if ($display instanceof $allowedClass) {
$isAllowed = true;
}
}

if (!$isAllowed) {
throw new Exceptions\InvalidArgumentException('Provided display entity is not valid for this widget type');
}

Expand Down
23 changes: 15 additions & 8 deletions src/Hydrators/Widgets/WidgetHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function hydrateDisplayRelationship(
$relationship->getIdentifier() !== null
&& $item->getIdentifier()->getId() === $relationship->getIdentifier()->getId()
) {
$result = $this->buildDisplay($item->getType(), $item->getAttributes());
$result = $this->buildDisplay($item->getType(), $item->getAttributes(), $item->getIdentifier()->getId());

if ($result !== null) {
return $result;
Expand All @@ -117,7 +117,7 @@ protected function hydrateDisplayRelationship(
return null;
}

$result = $this->buildDisplay($relationship->getData()->getType(), $relationship);
$result = $this->buildDisplay($relationship->getData()->getType(), $relationship, $relationship->getIdentifier()->getId());

return $result;
}
Expand All @@ -140,7 +140,7 @@ protected function hydrateDataSourcesRelationship(
$this->translator->translate('messages.missingDataSource.heading'),
$this->translator->translate('messages.missingDataSource.message'),
[
'pointer' => '/data/relationships/data_sources/data/id',
'pointer' => '/data/relationships/data-sources/data/id',
]
);
}
Expand Down Expand Up @@ -170,7 +170,7 @@ protected function hydrateDataSourcesRelationship(
$this->translator->translate('messages.missingDataSource.heading'),
$this->translator->translate('messages.missingDataSource.message'),
[
'pointer' => '/data/relationships/data_sources/data/id',
'pointer' => '/data/relationships/data-sources/data/id',
]
);
}
Expand Down Expand Up @@ -209,8 +209,6 @@ protected function hydrateGroupsRelationship(
$groups[] = $group;
}

break;

} catch (Uuid\Exception\InvalidUuidStringException $ex) {
throw new NodeWebServerExceptions\JsonApiErrorException(
StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY,
Expand Down Expand Up @@ -240,13 +238,15 @@ protected function hydrateGroupsRelationship(

/**
* @param string $type
* @param JsonAPIDocument\Objects\IStandardObject<mixed> $attributes
* @param JsonAPIDocument\Objects\IStandardObject $attributes
* @param string $identifier
*
* @return mixed[]|null
*/
private function buildDisplay(
string $type,
JsonAPIDocument\Objects\IStandardObject $attributes
JsonAPIDocument\Objects\IStandardObject $attributes,
string $identifier
): ?array {
switch ($type) {
case Schemas\Widgets\Display\AnalogValueSchema::SCHEMA_TYPE:
Expand All @@ -261,6 +261,7 @@ private function buildDisplay(
);

$display['entity'] = Entities\Widgets\Display\AnalogValue::class;
$display[self::IDENTIFIER_KEY] = Uuid\Uuid::fromString($identifier);

return $display;

Expand All @@ -276,6 +277,7 @@ private function buildDisplay(
);

$display['entity'] = Entities\Widgets\Display\Button::class;
$display[self::IDENTIFIER_KEY] = Uuid\Uuid::fromString($identifier);

return $display;

Expand All @@ -291,6 +293,7 @@ private function buildDisplay(
);

$display['entity'] = Entities\Widgets\Display\ChartGraph::class;
$display[self::IDENTIFIER_KEY] = Uuid\Uuid::fromString($identifier);

return $display;

Expand All @@ -306,6 +309,7 @@ private function buildDisplay(
);

$display['entity'] = Entities\Widgets\Display\DigitalValue::class;
$display[self::IDENTIFIER_KEY] = Uuid\Uuid::fromString($identifier);

return $display;

Expand All @@ -321,6 +325,7 @@ private function buildDisplay(
);

$display['entity'] = Entities\Widgets\Display\Gauge::class;
$display[self::IDENTIFIER_KEY] = Uuid\Uuid::fromString($identifier);

return $display;

Expand All @@ -336,6 +341,7 @@ private function buildDisplay(
);

$display['entity'] = Entities\Widgets\Display\GroupedButton::class;
$display[self::IDENTIFIER_KEY] = Uuid\Uuid::fromString($identifier);

return $display;

Expand All @@ -351,6 +357,7 @@ private function buildDisplay(
);

$display['entity'] = Entities\Widgets\Display\Slider::class;
$display[self::IDENTIFIER_KEY] = Uuid\Uuid::fromString($identifier);

return $display;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schemas/Widgets/WidgetSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class WidgetSchema extends Schemas\JsonApiSchema
*/
public const RELATIONSHIPS_DISPLAY = 'display';
public const RELATIONSHIPS_GROUPS = 'groups';
public const RELATIONSHIPS_DATA_SOURCES = 'data_sources';
public const RELATIONSHIPS_DATA_SOURCES = 'data-sources';

/** @var Routing\IRouter */
protected $router;
Expand Down
15 changes: 15 additions & 0 deletions src/Translations/node.en_US.neon
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ groups:
notFound:
heading : "Group not found"
message : "Requested group was not found"

widgets:
messages:
invalidType:
heading : "Invalid type"
message : "Provided widget entity type is not valid"
notCreated:
heading : "System error"
message : "Something went wrong, widget could not be created"
notUpdated:
heading : "System error"
message : "Something went wrong, widget could not be updated"
notDeleted:
heading : "System error"
message : "Something went wrong, widget could not be deleted"
Loading

0 comments on commit e34529f

Please sign in to comment.