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

Commit

Permalink
YeePHP: refactor checkIsOnline() method
Browse files Browse the repository at this point in the history
With feof, it used to return false negative, switched to stream_get_meta_data() function instead
  • Loading branch information
SharkEzz committed Jan 1, 2021
1 parent 68b72d9 commit 29d3d36
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/YeePHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function commit(): bool

protected function connect(): bool
{
$sock = fsockopen($this->lightIP, $this->lightPort, $errCode, $errStr, 5);
$sock = fsockopen($this->lightIP, $this->lightPort, $errCode, $errStr, 3);
if(!$sock) return false;

stream_set_blocking($sock, false);
Expand Down Expand Up @@ -271,7 +271,7 @@ protected function getProp(string $prop): string
*/
protected function checkIsOnline(): bool
{
if(feof($this->socket))
if(stream_get_meta_data($this->socket) === [])
throw new Exception('Device is offline!');

return true;
Expand All @@ -286,8 +286,6 @@ protected function checkIsOnline(): bool
*/
protected function makeRequest(array $job): ?string
{
$success = false;

$this->checkIsOnline();

$requestStr = json_encode($job) . "\r\n";
Expand Down
14 changes: 10 additions & 4 deletions tests/LightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ protected function setUp(): void
$this->light = new YeePHP($this->ip);
}

public function testConstructorThrowExceptionIfTheIpAdressIsInvalid()
{
$this->expectException(\Exception::class);
new YeePHP('192.1555.0.0');
}

public function testCanSeeIfLightIsOnline(): void
{
$this->assertTrue($this->light->isConnected());
Expand All @@ -42,12 +48,12 @@ public function testLightCanChangeBrightness(): void

public function testLightCanChangeColorAndBrightness(): void
{
$this->assertTrue(
$this->light
$res = $this->light
->setColor(0xFF8888)
->setBrightness(100)
->commit()
);
->commit();

$this->assertTrue($res);
}

public function testCanGetLightProps()
Expand Down

0 comments on commit 29d3d36

Please sign in to comment.