|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright (c) 2024 Benjamin Fahl |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE.md file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @see https://github.com/WebProject-xyz/ikea-tradfri-php |
| 12 | + */ |
| 13 | + |
| 14 | +namespace IKEA\Tests\Unit\Tradfri\Device; |
| 15 | + |
| 16 | +use IKEA\Tradfri\Command\Coap\Keys; |
| 17 | +use IKEA\Tradfri\Device\RollerBlind; |
| 18 | +use IKEA\Tradfri\Exception\RuntimeException; |
| 19 | +use IKEA\Tradfri\Service\ServiceInterface as Api; |
| 20 | + |
| 21 | +final class RollerBlindTest extends DeviceTester |
| 22 | +{ |
| 23 | + public function testGetAnInstance(): void |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + // Act |
| 27 | + $lamp = $this->getModel(); |
| 28 | + // Assert |
| 29 | + $this->assertInstanceOf(RollerBlind::class, $lamp); |
| 30 | + } |
| 31 | + |
| 32 | + public function testSetType(): void |
| 33 | + { |
| 34 | + // Arrange |
| 35 | + $lamp = $this->getModel(); |
| 36 | + $lamp->setType(Keys::ATTR_DEVICE_INFO_TYPE_ROLLER_BLIND); |
| 37 | + // Act |
| 38 | + $result = $lamp->getType(); |
| 39 | + |
| 40 | + // Assert |
| 41 | + $this->assertSame(Keys::ATTR_DEVICE_INFO_TYPE_ROLLER_BLIND, $result); |
| 42 | + } |
| 43 | + |
| 44 | + public function testGetBrightnessButNotSet(): void |
| 45 | + { |
| 46 | + // Arrange |
| 47 | + $rollerBlind = $this->getModel(); |
| 48 | + |
| 49 | + // Act |
| 50 | + $result = $rollerBlind->getDarkenedState(); |
| 51 | + |
| 52 | + // Assert |
| 53 | + $this->assertSame(0, $result); |
| 54 | + } |
| 55 | + |
| 56 | + public function testGetBrightness(): void |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + $rollerBlind = $this->getModel(); |
| 60 | + $rollerBlind->setDarkenedState((int) 30); |
| 61 | + // Act |
| 62 | + $result = $rollerBlind->getDarkenedState(); |
| 63 | + |
| 64 | + // Assert |
| 65 | + $this->assertSame(30, $result); |
| 66 | + } |
| 67 | + |
| 68 | + public function testSetBrightnessToLow(): void |
| 69 | + { |
| 70 | + // Arrange |
| 71 | + $rollerBlind = $this->getModel(); |
| 72 | + $rollerBlind->setDarkenedState(-12); |
| 73 | + // Act |
| 74 | + $result = $rollerBlind->getDarkenedState(); |
| 75 | + |
| 76 | + // Assert |
| 77 | + $this->assertSame(0, $result); |
| 78 | + } |
| 79 | + |
| 80 | + public function testStates(): void |
| 81 | + { |
| 82 | + // Arrange |
| 83 | + $rollerBlind = $this->getModel(); |
| 84 | + $this->assertTrue($rollerBlind->isFullyOpened()); |
| 85 | + |
| 86 | + $service = \mock(Api::class); |
| 87 | + $service->expects()->setRollerBlindPosition($rollerBlind, 0)->once()->andReturn(true); |
| 88 | + $rollerBlind->setService($service); |
| 89 | + |
| 90 | + // Act |
| 91 | + $rollerBlind->setToPosition(0); |
| 92 | + |
| 93 | + // Assert |
| 94 | + $this->assertTrue($rollerBlind->isFullyOpened()); |
| 95 | + } |
| 96 | + |
| 97 | + public function testICanSetPositions(): void |
| 98 | + { |
| 99 | + // Arrange |
| 100 | + $rollerBlind = $this->getModel(); |
| 101 | + |
| 102 | + $service = \mock(Api::class); |
| 103 | + $service->expects('setRollerBlindPosition')->with($rollerBlind, 100)->once()->andReturnUsing(static function (): bool { |
| 104 | + $model = \func_get_arg(0); |
| 105 | + \assert($model instanceof RollerBlind); |
| 106 | + $model->setDarkenedState(\func_get_arg(1)); |
| 107 | + |
| 108 | + return true; |
| 109 | + }); |
| 110 | + $service->expects('setRollerBlindPosition')->with($rollerBlind, 75)->once()->andReturnUsing(static function (): bool { |
| 111 | + $model = \func_get_arg(0); |
| 112 | + \assert($model instanceof RollerBlind); |
| 113 | + $model->setDarkenedState(\func_get_arg(1)); |
| 114 | + |
| 115 | + return true; |
| 116 | + }); |
| 117 | + |
| 118 | + $rollerBlind->setService($service); |
| 119 | + $this->assertFalse($rollerBlind->isFullyClosed()); |
| 120 | + |
| 121 | + // Act |
| 122 | + $result = $rollerBlind->setToPosition(100); |
| 123 | + |
| 124 | + // Assert |
| 125 | + $this->assertTrue($result); |
| 126 | + $this->assertTrue($rollerBlind->isFullyClosed()); |
| 127 | + $this->assertFalse($rollerBlind->isFullyOpened()); |
| 128 | + |
| 129 | + // Act |
| 130 | + $result = $rollerBlind->setToPosition(75); |
| 131 | + // Assert |
| 132 | + $this->assertTrue($result); |
| 133 | + $this->assertFalse($rollerBlind->isFullyOpened()); |
| 134 | + $this->assertFalse($rollerBlind->isFullyClosed()); |
| 135 | + |
| 136 | + $this->assertSame(75, $rollerBlind->getDarkenedState()); |
| 137 | + } |
| 138 | + |
| 139 | + public function testICanSetPositionFails(): void |
| 140 | + { |
| 141 | + $this->expectException(RuntimeException::class); |
| 142 | + $this->expectExceptionMessage('set postion failed'); |
| 143 | + // Arrange |
| 144 | + $lamp = $this->getModel(); |
| 145 | + |
| 146 | + $service = \mock(Api::class); |
| 147 | + $service->expects('setRollerBlindPosition')->andThrow(new RuntimeException('set postion failed')); |
| 148 | + |
| 149 | + $lamp->setService($service); |
| 150 | + $this->assertTrue($lamp->isFullyOpened()); |
| 151 | + |
| 152 | + // Act |
| 153 | + $result = $lamp->setToPosition(100); |
| 154 | + |
| 155 | + // Assert |
| 156 | + } |
| 157 | + |
| 158 | + protected function getModel(): RollerBlind |
| 159 | + { |
| 160 | + return new RollerBlind($this->_id, Keys::ATTR_DEVICE_INFO_TYPE_ROLLER_BLIND); |
| 161 | + } |
| 162 | +} |
0 commit comments