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

Commit

Permalink
YeePHP: added disconnect method and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkEzz committed Jan 1, 2021
1 parent 29d3d36 commit 9f68382
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Interfaces/YeePHPInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,11 @@ public function setPower(string $power): self;
* @return bool
*/
public function commit(): bool;

/**
* Close the opened socket. Return true if the socket has been closed
*
* @return bool
*/
public function disconnect(): bool;
}
19 changes: 17 additions & 2 deletions src/YeePHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function __construct(string $ip, int $port = 55443)
*/
public function __destruct()
{
fclose($this->socket);
$this->disconnect();
}

/**
Expand Down Expand Up @@ -159,6 +159,8 @@ public function toggle(): self
*/
public function setColor(int $hexColor): self
{
// TODO: check if the color is a valid hexadecimal number

$this->createJob('set_rgb', [
$hexColor,
$this->fadeEffect,
Expand Down Expand Up @@ -232,6 +234,19 @@ public function commit(): bool
return $success;
}

public function disconnect(): bool
{
try
{
return fclose($this->socket);
}
catch (\TypeError $e)
{
return false;
}

}

protected function connect(): bool
{
$sock = fsockopen($this->lightIP, $this->lightPort, $errCode, $errStr, 3);
Expand Down Expand Up @@ -271,7 +286,7 @@ protected function getProp(string $prop): string
*/
protected function checkIsOnline(): bool
{
if(stream_get_meta_data($this->socket) === [])
if(socket_get_status($this->socket) === [])
throw new Exception('Device is offline!');

return true;
Expand Down
11 changes: 11 additions & 0 deletions tests/LightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,15 @@ public function testCanGetLightProps()
$this->assertNotEmpty($this->light->getBrightness());
$this->assertNotEmpty($this->light->getColor());
}

public function testIfDisconnectMethodReturnTrueIfTheSocketHasBeenClosed()
{
$this->assertTrue($this->light->disconnect());
}

public function testIfDisconnectMethodReturnFalseIfTheSocketHasBeenAlreadyClosed()
{
$this->light->disconnect();
$this->assertFalse($this->light->disconnect());
}
}

0 comments on commit 9f68382

Please sign in to comment.