Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the GetBySensorId Command #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions library/Phue/Command/GetSensorById.php
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);
}
}
73 changes: 73 additions & 0 deletions tests/Phue/Test/Command/GetSensorByIdTest.php
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 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);
}
}