From 72dde9016b10fa30ab8cf7804045a44c050e3bc5 Mon Sep 17 00:00:00 2001 From: vagrant Date: Sun, 21 Oct 2018 12:43:25 +0000 Subject: [PATCH 1/2] #124 added new command GetSensorById --- library/Phue/Command/GetSensorById.php | 55 ++++++++++++++ tests/Phue/Test/Command/GetSensorByIdTest.php | 73 +++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 library/Phue/Command/GetSensorById.php create mode 100644 tests/Phue/Test/Command/GetSensorByIdTest.php 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..3924e37 --- /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 light 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); + } +} From ab13521db9b37cbf7da8a2d93c7e001f8a999154 Mon Sep 17 00:00:00 2001 From: vagrant Date: Sun, 21 Oct 2018 12:53:42 +0000 Subject: [PATCH 2/2] #124 fixed typo --- tests/Phue/Test/Command/GetSensorByIdTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Phue/Test/Command/GetSensorByIdTest.php b/tests/Phue/Test/Command/GetSensorByIdTest.php index 3924e37..13debb0 100644 --- a/tests/Phue/Test/Command/GetSensorByIdTest.php +++ b/tests/Phue/Test/Command/GetSensorByIdTest.php @@ -50,7 +50,7 @@ public function setUp() } /** - * Test: Send get light by id command + * Test: Send get sensor by id command * * @covers \Phue\Command\GetSensorById::__construct * @covers \Phue\Command\GetSensorById::send