Skip to content

Commit

Permalink
Merge pull request #92 from WebProject-xyz/develop
Browse files Browse the repository at this point in the history
Next Release
  • Loading branch information
Fahl-Design authored Oct 8, 2021
2 parents eafe5c6 + c34c285 commit c0df985
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 62 deletions.
67 changes: 24 additions & 43 deletions src/IKEA/Tradfri/Device/Helper/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
use IKEA\Tradfri\Device\Remote;
use IKEA\Tradfri\Device\Repeater;
use IKEA\Tradfri\Device\RollerBlind;
use IKEA\Tradfri\Device\Unknown;
use IKEA\Tradfri\Exception\RuntimeException;
use function class_exists;
use function get_class_methods;
use function str_replace;
use function strpos;

/**
* Class Type.
Expand Down Expand Up @@ -116,7 +119,7 @@ public function isRollerBlind(string $typeAttribute): bool
/**
* Check if given type attribute can be processed.
*/
public function isKnownDeviceType(string $typeAttribute): bool
public function isUnknownDeviceType(string $typeAttribute): bool
{
foreach (get_class_methods($this) as $method) {
if (__FUNCTION__ === $method) {
Expand All @@ -126,54 +129,32 @@ public function isKnownDeviceType(string $typeAttribute): bool
continue;
}
if ($this->$method($typeAttribute)) {
return true;
return false;
}
}

return false;
return true;
}

public function buildFrom(string $typeAttribute, int $deviceId): Device
public function buildFrom(string $typeAttribute, int $deviceId, bool $buildUnknownDevice = true): Device
{
$model = null;

if ($this->isLightBulb($typeAttribute)) {
$model = new LightBulb($deviceId, $typeAttribute);
}
if ($this->isMotionSensor($typeAttribute)) {
$model = new MotionSensor($deviceId);
}

if ($this->isRemote($typeAttribute)) {
$model = new Remote($deviceId);
}

if ($this->isDimmer($typeAttribute)) {
$model = new Dimmer($deviceId);
}

if ($this->isFloalt($typeAttribute)) {
$model = new Floalt($deviceId, $typeAttribute);
}

if ($this->isOpenCloseRemote($typeAttribute)) {
$model = new OpenCloseRemote($deviceId);
}

if ($this->isRepeater($typeAttribute)) {
$model = new Repeater($deviceId, $typeAttribute);
}

if ($this->isRollerBlind($typeAttribute)) {
$model = new RollerBlind($deviceId, $typeAttribute);
}

if (false === $this->isKnownDeviceType($typeAttribute)) {
$model = new Unknown($deviceId, $typeAttribute);
}
foreach (get_class_methods($this) as $method) {
if (0 !== strncmp($method, 'is', 2)) {
continue;
}

if ($model) {
return $model;
if ($this->$method($typeAttribute)) {
if ('isUnknownDeviceType' === $method && $buildUnknownDevice) {
$modelClass = 'Unknown';
} else {
$modelClass = str_replace('is', '', $method);
}

$fqdnClassName = '\\IKEA\\Tradfri\\Device\\' . $modelClass;
if (class_exists($fqdnClassName)) {
return new $fqdnClassName($deviceId, $typeAttribute);
}
}
}

throw new RuntimeException('Unable to detect device type: ' . $typeAttribute);
Expand Down
2 changes: 1 addition & 1 deletion src/IKEA/Tradfri/Traits/ProvidesType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public function setType(string $type): self
*/
public function isValidType(string $type): bool
{
return (new Type())->isKnownDeviceType($type);
return false === (new Type())->isUnknownDeviceType($type);
}
}
44 changes: 33 additions & 11 deletions tests/Unit/Tradfri/Device/Helper/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function isMotionSensorData(): array
}

/**
* @dataProvider isKnownDeviceTypeData
* @dataProvider isUnknownDeviceTypeData
*/
public function testKnownDeviceType(
string $typeAttribute,
Expand All @@ -183,7 +183,7 @@ public function testKnownDeviceType(
$helper = new Type();

// Act
$condition = $helper->isKnownDeviceType($typeAttribute);
$condition = $helper->isUnknownDeviceType($typeAttribute);
// Assert
if ($assertTrue) {
$this->assertTrue(
Expand All @@ -198,17 +198,39 @@ public function testKnownDeviceType(
}
}

public function isKnownDeviceTypeData(): array
public function isUnknownDeviceTypeData(): array
{
return [
['invalidStringValue', false],
[Keys::ATTR_DEVICE_INFO_TYPE_BLUB_E27_W, true],
[Keys::ATTR_DEVICE_INFO_TYPE_BLUB_E27_WS, true],
[Keys::ATTR_DEVICE_INFO_TYPE_BLUB_GU10_WS, true],
[Keys::ATTR_DEVICE_INFO_TYPE_BLUB_GU10_W, true],
[Keys::ATTR_DEVICE_INFO_TYPE_MOTION_SENSOR, true],
[Keys::ATTR_DEVICE_INFO_TYPE_REMOTE_CONTROL, true],
[Keys::ATTR_DEVICE_INFO_TYPE_DIMMER, true],
['invalidStringValue', true],
[Keys::ATTR_DEVICE_INFO_TYPE_BLUB_E27_W, false],
[Keys::ATTR_DEVICE_INFO_TYPE_BLUB_E27_WS, false],
[Keys::ATTR_DEVICE_INFO_TYPE_BLUB_GU10_WS, false],
[Keys::ATTR_DEVICE_INFO_TYPE_BLUB_GU10_W, false],
[Keys::ATTR_DEVICE_INFO_TYPE_MOTION_SENSOR, false],
[Keys::ATTR_DEVICE_INFO_TYPE_REMOTE_CONTROL, false],
[Keys::ATTR_DEVICE_INFO_TYPE_DIMMER, false],
];
}

public function testBuildFrom(): void
{
// Arrange
$helper = new Type();

// Act
$model = $helper->buildFrom(Keys::ATTR_DEVICE_INFO_TYPE_BLUB_E27_WS, 1);

// Assert
$this->assertNotNull($model);
}

public function testBuildFromNoUnknownClassAndSeeError(): void
{
$this->expectExceptionMessage('Unable to detect device type: blubb');
// Arrange
$helper = new Type();

// Act
$helper->buildFrom('blubb', 1, false);
}
}
24 changes: 17 additions & 7 deletions tests/Unit/Tradfri/Mapper/DeviceDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use IKEA\Tradfri\Device\LightBulb;
use IKEA\Tradfri\Device\MotionSensor;
use IKEA\Tradfri\Device\Remote;
use IKEA\Tradfri\Device\RollerBlind;
use IKEA\Tradfri\Exception\RuntimeException;
use IKEA\Tradfri\Mapper\DeviceData;
use IKEA\Tradfri\Service\ServiceInterface;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function testICanMapDataToCollectionWithNoError(): void
// Assert
$this->assertInstanceOf(Devices::class, $result);
$this->assertFalse($result->isEmpty());
$this->assertSame(6, $result->count());
$this->assertSame(7, $result->count());

$device1 = $result->get(1000);
$this->assertInstanceOf(LightBulb::class, $device1);
Expand Down Expand Up @@ -97,16 +98,25 @@ public function testICanMapDataToCollectionWithNoError(): void
$this->assertSame('UnitTestFactory', $device4->getManufacturer());
$this->assertSame('v1.33.7', $device4->getVersion());

$device5 = $result->get(5000);
$this->assertInstanceOf(MotionSensor::class, $device5);
$device5 = $result->get(6000);
$this->assertInstanceOf(RollerBlind::class, $device5);
$this->assertFalse($device5->isLightBulb());
$this->assertSame(5000, $device5->getId());
$this->assertSame(Keys::ATTR_DEVICE_INFO_TYPE_MOTION_SENSOR, $device5->getName());
$this->assertSame(Keys::ATTR_DEVICE_INFO_TYPE_MOTION_SENSOR, $device5->getType());
$this->assertSame(6000, $device5->getId());
$this->assertSame(Keys::ATTR_DEVICE_INFO_TYPE_ROLLER_BLIND, $device5->getName());
$this->assertSame(Keys::ATTR_DEVICE_INFO_TYPE_ROLLER_BLIND, $device5->getType());
$this->assertSame('UnitTestFactory', $device5->getManufacturer());
$this->assertSame('v1.33.7', $device5->getVersion());

$this->assertCount(6, $result->getDevices());
$device6 = $result->get(5000);
$this->assertInstanceOf(MotionSensor::class, $device6);
$this->assertFalse($device6->isLightBulb());
$this->assertSame(5000, $device6->getId());
$this->assertSame(Keys::ATTR_DEVICE_INFO_TYPE_MOTION_SENSOR, $device6->getName());
$this->assertSame(Keys::ATTR_DEVICE_INFO_TYPE_MOTION_SENSOR, $device6->getType());
$this->assertSame('UnitTestFactory', $device6->getManufacturer());
$this->assertSame('v1.33.7', $device6->getVersion());

$this->assertCount(7, $result->getDevices());
$this->assertCount(2, $result->getLightBulbs());
}
}
14 changes: 14 additions & 0 deletions tests/_support/Helper/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ public function getDevices(): array
Keys::ATTR_DEVICE_INFO_TYPE => Keys::ATTR_DEVICE_INFO_TYPE_MOTION_SENSOR,
],
])),
json_decode(json_encode([
Keys::ATTR_ID => 6000,
Keys::ATTR_NAME => Keys::ATTR_DEVICE_INFO_TYPE_ROLLER_BLIND,
Keys::ATTR_DEVICE_INFO => [
Keys::ATTR_DEVICE_INFO_MANUFACTURER => 'UnitTestFactory',
Keys::ATTR_DEVICE_VERSION => 'v1.33.7',
Keys::ATTR_DEVICE_INFO_TYPE => Keys::ATTR_DEVICE_INFO_TYPE_ROLLER_BLIND,
],
Keys::ATTR_FYRTUR_CONTROL => [
[
Keys::ATTR_FYRTUR_STATE => 100,
],
],
])),
];
}

Expand Down

0 comments on commit c0df985

Please sign in to comment.