From c52045e46cf98e9c59d15f795335525aed31e012 Mon Sep 17 00:00:00 2001 From: Malachi Soord Date: Mon, 29 Jun 2020 21:23:52 +0200 Subject: [PATCH] Improvements (#9) * Fix for no devices * Improve error * Align --- src/ApiClient.php | 16 +++++--------- tests/Functional/ApiClientTest.php | 35 ++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/ApiClient.php b/src/ApiClient.php index ae9f65b..cd787af 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -128,9 +128,7 @@ private function getAccessToken(): Token $this->validate($response, 'An error occurred while fetching access token'); - $token = Token::fromArray($response); - - return $token; + return Token::fromArray($response); } private function refreshAccessToken(): Token @@ -151,9 +149,7 @@ private function refreshAccessToken(): Token $this->validate($response, 'Failed to refresh access token'); - $token = Token::fromArray($response); - - return $token; + return Token::fromArray($response); } private function getBaseUrl(Session $session): UriInterface @@ -162,15 +158,15 @@ private function getBaseUrl(Session $session): UriInterface } /** - * @param array $response - * @param string|null $message + * @param array $response + * @param string $message * * @throws TuyaClientException */ - private function validate(array $response, string $message = null) + private function validate(array $response, string $message) { if (isset($response['responseStatus']) && $response['responseStatus'] === 'error') { - $message = $message ?? $response['responseMsg']; + $message = sprintf('%s - %s', $message, $response['errorMsg']); throw new TuyaClientException($message); } diff --git a/tests/Functional/ApiClientTest.php b/tests/Functional/ApiClientTest.php index 53ed516..7345146 100644 --- a/tests/Functional/ApiClientTest.php +++ b/tests/Functional/ApiClientTest.php @@ -3,6 +3,7 @@ namespace Tests\Functional\Inverse\TuyaClient; use Inverse\TuyaClient\ApiClient; +use Inverse\TuyaClient\Device\AbstractDevice; use Inverse\TuyaClient\Device\SwitchDevice; class ApiClientTest extends BaseTestCase @@ -11,25 +12,37 @@ public function testDiscoverDevices() { $apiClient = $this->getApiClient(); - $switch = $this->getDevice($apiClient); + $device = $this->getDevice($apiClient); - if (!$switch->isOn()) { - $apiClient->sendEvent($switch->getOnEvent()); - - $switch = $this->getDevice($apiClient); - $this->assertTrue($switch->isOn()); + if ($device === null) { + return $this->doesNotPerformAssertions(); } - $apiClient->sendEvent($switch->getOffEvent()); - $switch = $this->getDevice($apiClient); + if ($device instanceof SwitchDevice) { + if (!$device->isOn()) { + $apiClient->sendEvent($device->getOnEvent()); + + $device = $this->getDevice($apiClient); + $this->assertTrue($device->isOn()); + } - $this->assertFalse($switch->isOn()); + $apiClient->sendEvent($device->getOffEvent()); + $device = $this->getDevice($apiClient); + + $this->assertFalse($device->isOn()); + } } - private function getDevice(ApiClient $apiClient): SwitchDevice + private function getDevice(ApiClient $apiClient): ?AbstractDevice { $devices = $apiClient->discoverDevices(); - return $devices[1]; + $device = null; + + if (!empty($devices)) { + $device = $devices[0]; + } + + return $device; } }