-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqmk#124 added new command GetSensorById
- Loading branch information
vagrant
committed
Oct 21, 2018
1 parent
78c7e94
commit 72dde90
Showing
2 changed files
with
128 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,55 @@ | ||
<?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\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); | ||
} | ||
} |
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,73 @@ | ||
<?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\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); | ||
} | ||
} |