diff --git a/library/Phue/Command/GetSensorById.php b/library/Phue/Command/GetSensorById.php new file mode 100644 index 0000000..6465242 --- /dev/null +++ b/library/Phue/Command/GetSensorById.php @@ -0,0 +1,55 @@ + + * @copyright Copyright (c) 2012 Michael K. Squires + * @license http://github.com/sqmk/Phue/wiki/License + */ +namespace Phue\Command; + +use Phue\Client; +use Phue\Sensor; + +/** + * Get sensor by id command + */ +class GetSensorById implements CommandInterface +{ + + /** + * Sensor Id + * + * @var string + */ + protected $sensorId; + + /** + * Constructs a command + * + * @param int $sensorId + * Sensor Id + */ + public function __construct($sensorId) + { + $this->sensorId = (int) $sensorId; + } + + /** + * Send command + * + * @param Client $client + * Phue Client + * + * @return Sensor Sensor object + */ + public function send(Client $client) + { + // Get response + $attributes = $client->getTransport()->sendRequest( + "/api/{$client->getUsername()}/sensors/{$this->sensorId}" + ); + + return new Sensor($this->sensorId, $attributes, $client); + } +} diff --git a/tests/Phue/Test/Command/GetSensorByIdTest.php b/tests/Phue/Test/Command/GetSensorByIdTest.php new file mode 100644 index 0000000..13debb0 --- /dev/null +++ b/tests/Phue/Test/Command/GetSensorByIdTest.php @@ -0,0 +1,73 @@ + + * @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\GetSensorById; +use Phue\Transport\TransportInterface; + +/** + * Tests for Phue\Command\GetSensorById + */ +class GetSensorByIdTest extends \PHPUnit_Framework_TestCase +{ + + /** + * Set up + */ + public function setUp() + { + // Mock client + $this->mockClient = $this->createMock('\Phue\Client', + array( + 'getUsername', + 'getTransport' + ), array( + '127.0.0.1' + )); + + // Mock transport + $this->mockTransport = $this->createMock('\Phue\Transport\TransportInterface', + array( + 'sendRequest' + )); + + // Stub client's getUsername method + $this->mockClient->expects($this->any()) + ->method('getUsername') + ->will($this->returnValue('abcdefabcdef01234567890123456789')); + + // Stub client getTransport usage + $this->mockClient->expects($this->any()) + ->method('getTransport') + ->will($this->returnValue($this->mockTransport)); + } + + /** + * Test: Send get sensor by id command + * + * @covers \Phue\Command\GetSensorById::__construct + * @covers \Phue\Command\GetSensorById::send + */ + public function testSend() + { + // Stub transport's sendRequest usage + $this->mockTransport->expects($this->once()) + ->method('sendRequest') + ->with("/api/{$this->mockClient->getUsername()}/sensors/10") + ->will($this->returnValue(new \stdClass())); + + // Get light + $x = new GetSensorById(10); + $sensor = $x->send($this->mockClient); + + // Ensure type is correct + $this->assertInstanceOf('\Phue\Sensor', $sensor); + } +}