-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitHub #88 - Add commands to search and retrieve sensors
- Loading branch information
Showing
4 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* Phue: Philips Hue PHP Client | ||
* | ||
* @author Michael Squires <[email protected]> | ||
* @copyright Copyright (c) 2012 Michael K. Squires | ||
* @license http://github.com/sqmk/Phue/wiki/License | ||
*/ | ||
|
||
namespace Phue\Command; | ||
|
||
use Phue\Client; | ||
|
||
/** | ||
* Get new sensors command | ||
*/ | ||
class GetNewSensors implements CommandInterface | ||
{ | ||
/** | ||
* Last scan | ||
* | ||
* @var string | ||
*/ | ||
protected $lastScan; | ||
|
||
/** | ||
* Found sensors | ||
* | ||
* @var array | ||
*/ | ||
protected $sensors = []; | ||
|
||
/** | ||
* Send command | ||
* | ||
* @param Client $client Phue Client | ||
* | ||
* @return self This object | ||
*/ | ||
public function send(Client $client) | ||
{ | ||
// Get response | ||
$response = $client->getTransport()->sendRequest( | ||
"/api/{$client->getUsername()}/sensors/new" | ||
); | ||
|
||
$this->lastScan = $response->lastscan; | ||
|
||
// Remove scan from response | ||
unset($response->lastscan); | ||
|
||
// Iterate through left over properties as sensors | ||
foreach ($response as $sensorId => $sensor) { | ||
$this->sensors[$sensorId] = $sensor->name; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get sensors | ||
* | ||
* @return array List of new sensors | ||
*/ | ||
public function getSensors() | ||
{ | ||
return $this->sensors; | ||
} | ||
|
||
/** | ||
* Is scan currently active | ||
* | ||
* @return bool True if active, false if not | ||
*/ | ||
public function isScanActive() | ||
{ | ||
return $this->lastScan == 'active'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Phue: Philips Hue PHP Client | ||
* | ||
* @author Michael Squires <[email protected]> | ||
* @copyright Copyright (c) 2012 Michael K. Squires | ||
* @license http://github.com/sqmk/Phue/wiki/License | ||
*/ | ||
|
||
namespace Phue\Command; | ||
|
||
use Phue\Client; | ||
use Phue\Transport\TransportInterface; | ||
|
||
/** | ||
* Start Sensor Scan command | ||
*/ | ||
class StartSensorScan implements CommandInterface | ||
{ | ||
/** | ||
* Send command | ||
* | ||
* @param Client $client Phue Client | ||
* | ||
* @return mixed | ||
*/ | ||
public function send(Client $client) | ||
{ | ||
// Get response | ||
$response = $client->getTransport()->sendRequest( | ||
"/api/{$client->getUsername()}/sensors", | ||
TransportInterface::METHOD_POST | ||
); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* Phue: Philips Hue PHP Client | ||
* | ||
* @author Michael Squires <[email protected]> | ||
* @copyright Copyright (c) 2012 Michael K. Squires | ||
* @license http://github.com/sqmk/Phue/wiki/License | ||
*/ | ||
|
||
namespace Phue\Test\Command; | ||
|
||
use Phue\Client; | ||
use Phue\Command\GetNewSensors; | ||
use Phue\Transport\TransportInterface; | ||
|
||
/** | ||
* Tests for Phue\Command\GetNewSensors | ||
*/ | ||
class GetNewSensorsTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Set up | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->getNewSensors = new GetNewSensors(); | ||
|
||
// Mock client | ||
$this->mockClient = $this->getMock( | ||
'\Phue\Client', | ||
['getUsername', 'getTransport'], | ||
['127.0.0.1'] | ||
); | ||
|
||
// Mock transport | ||
$this->mockTransport = $this->getMock( | ||
'\Phue\Transport\TransportInterface', | ||
['sendRequest'] | ||
); | ||
|
||
// Stub client's getUsername method | ||
$this->mockClient->expects($this->any()) | ||
->method('getUsername') | ||
->will($this->returnValue('abcdefabcdef01234567890123456789')); | ||
|
||
// Stub client's getTransport method | ||
$this->mockClient->expects($this->any()) | ||
->method('getTransport') | ||
->will($this->returnValue($this->mockTransport)); | ||
|
||
// Mock transport results | ||
$mockTransportResults = (object) [ | ||
'1' => (object) ['name' => 'Sensor 1'], | ||
'2' => (object) ['name' => 'Sensor 2'], | ||
'lastscan' => 'active' | ||
]; | ||
|
||
// Stub transport's sendRequest usage | ||
$this->mockTransport->expects($this->once()) | ||
->method('sendRequest') | ||
->with($this->equalTo("/api/{$this->mockClient->getUsername()}/sensors/new")) | ||
->will($this->returnValue($mockTransportResults)); | ||
} | ||
|
||
/** | ||
* Test: Get new sensors | ||
* | ||
* @covers \Phue\Command\GetNewSensors::send | ||
* @covers \Phue\Command\GetNewSensors::getSensors | ||
* @covers \Phue\Command\GetNewSensors::isScanActive | ||
*/ | ||
public function testGetNewSensors() | ||
{ | ||
// Send command and get response | ||
$response = $this->getNewSensors->send($this->mockClient); | ||
|
||
// Ensure response is self object | ||
$this->assertEquals($this->getNewSensors, $response); | ||
|
||
// Ensure array of sensors | ||
$this->assertInternalType('array', $response->getSensors()); | ||
|
||
// Ensure expected number of sensors | ||
$this->assertEquals(2, count($response->getSensors())); | ||
|
||
// Ensure lastscan is active | ||
$this->assertTrue($response->isScanActive()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Phue: Philips Hue PHP Client | ||
* | ||
* @author Michael Squires <[email protected]> | ||
* @copyright Copyright (c) 2012 Michael K. Squires | ||
* @license http://github.com/sqmk/Phue/wiki/License | ||
*/ | ||
|
||
namespace Phue\Test\Command; | ||
|
||
use Phue\Client; | ||
use Phue\Command\StartSensorScan; | ||
use Phue\Transport\TransportInterface; | ||
|
||
/** | ||
* Tests for Phue\Command\StartSensorScan | ||
*/ | ||
class StartSensorScanTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Set up | ||
*/ | ||
public function setUp() | ||
{ | ||
// Mock client | ||
$this->mockClient = $this->getMock( | ||
'\Phue\Client', | ||
['getTransport'], | ||
['127.0.0.1'] | ||
); | ||
|
||
// Mock transport | ||
$this->mockTransport = $this->getMock( | ||
'\Phue\Transport\TransportInterface', | ||
['sendRequest'] | ||
); | ||
|
||
// Stub client's getUsername method | ||
$this->mockClient->expects($this->any()) | ||
->method('getUsername') | ||
->will($this->returnValue('abcdefabcdef01234567890123456789')); | ||
|
||
// Stub client's getTransport method | ||
$this->mockClient->expects($this->any()) | ||
->method('getTransport') | ||
->will($this->returnValue($this->mockTransport)); | ||
} | ||
|
||
/** | ||
* Test: Send start sensor scan command | ||
* | ||
* @covers \Phue\Command\StartSensorScan::send | ||
*/ | ||
public function testSend() | ||
{ | ||
// Stub transport's sendRequest method | ||
$this->mockTransport->expects($this->once()) | ||
->method('sendRequest') | ||
->with( | ||
$this->equalTo("/api/{$this->mockClient->getUsername()}/sensors"), | ||
$this->equalTo('POST') | ||
) | ||
->will($this->returnValue('success!')); | ||
|
||
$this->assertEquals( | ||
'success!', | ||
(new StartSensorScan)->send($this->mockClient) | ||
); | ||
} | ||
} |