From 03f83fea3b81ee1842dd71647e4fc3d891be8d53 Mon Sep 17 00:00:00 2001 From: "Michael K. Squires" Date: Fri, 26 Dec 2014 16:48:10 -0800 Subject: [PATCH] GitHub #88 - Add commands to search and retrieve sensors --- library/Phue/Command/GetNewSensors.php | 79 ++++++++++++++++ library/Phue/Command/StartSensorScan.php | 37 ++++++++ tests/Phue/Test/Command/GetNewSensorsTest.php | 89 +++++++++++++++++++ .../Phue/Test/Command/StartSensorScanTest.php | 71 +++++++++++++++ 4 files changed, 276 insertions(+) create mode 100644 library/Phue/Command/GetNewSensors.php create mode 100644 library/Phue/Command/StartSensorScan.php create mode 100644 tests/Phue/Test/Command/GetNewSensorsTest.php create mode 100644 tests/Phue/Test/Command/StartSensorScanTest.php diff --git a/library/Phue/Command/GetNewSensors.php b/library/Phue/Command/GetNewSensors.php new file mode 100644 index 0000000..cdc12b5 --- /dev/null +++ b/library/Phue/Command/GetNewSensors.php @@ -0,0 +1,79 @@ + + * @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'; + } +} diff --git a/library/Phue/Command/StartSensorScan.php b/library/Phue/Command/StartSensorScan.php new file mode 100644 index 0000000..a9bfd05 --- /dev/null +++ b/library/Phue/Command/StartSensorScan.php @@ -0,0 +1,37 @@ + + * @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; + } +} diff --git a/tests/Phue/Test/Command/GetNewSensorsTest.php b/tests/Phue/Test/Command/GetNewSensorsTest.php new file mode 100644 index 0000000..0765122 --- /dev/null +++ b/tests/Phue/Test/Command/GetNewSensorsTest.php @@ -0,0 +1,89 @@ + + * @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()); + } +} diff --git a/tests/Phue/Test/Command/StartSensorScanTest.php b/tests/Phue/Test/Command/StartSensorScanTest.php new file mode 100644 index 0000000..8109b18 --- /dev/null +++ b/tests/Phue/Test/Command/StartSensorScanTest.php @@ -0,0 +1,71 @@ + + * @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) + ); + } +}