Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Commit

Permalink
tests: fix disconnect check test
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkEzz committed Apr 3, 2021
1 parent 93e32ff commit fd042f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions src/YeePHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class YeePHP implements YeePHPInterface
*/
protected $socket;

/**
* Current status of the socket
*
* @var string
*/
protected $is_connected = false;

/**
* Light constructor.
* @param string $ip The light IP
Expand Down Expand Up @@ -353,11 +360,12 @@ public function commit(): bool
*/
public function disconnect(): bool
{
try {
if($this->is_connected)
{
$this->is_connected = false;
return fclose($this->socket);
} catch (Exception $e) {
return false;
}
else return false;
}


Expand All @@ -374,6 +382,7 @@ protected function connect(): bool

stream_set_blocking($sock, false);
$this->socket = $sock;
$this->is_connected = true;

return true;
}
Expand All @@ -394,7 +403,7 @@ protected function getProp(string $prop): ?array

$res = $this->makeRequest($job);

if ($res[0] == "ok")
if ($res[0] !== "ok")
throw new Exception('Problem in result'); // TODO

return $res;
Expand Down Expand Up @@ -430,7 +439,11 @@ protected function getProps(array $props): ?array
protected function checkIsOnline(): bool
{
if (socket_get_status($this->socket) === [])
throw new Exception('Device is offline!');
{
$this->is_connected = false;
throw new Exception('Device disconnected!');
}


return true;
}
Expand Down Expand Up @@ -466,7 +479,7 @@ protected function makeRequest(array $job): ?array
if (!array_key_exists('error', $res) && array_key_exists('result', $res))
$result = $res['result'];
} else {
throw new Exception('No response received from device !');
return null;
}

return $result;
Expand All @@ -484,9 +497,6 @@ protected function createJob(string $method, array $params): void
$this->jobs[] = $this->createJobArray($method, $params);
}




/**
* Convert an method and his params into a job array
*
Expand Down
2 changes: 1 addition & 1 deletion tests/LightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LightTest extends TestCase
*
* @var string
*/
private string $ip = '192.168.1.21';
private string $ip = '192.168.0.104';

private YeePHP $light;

Expand Down

0 comments on commit fd042f2

Please sign in to comment.