Skip to content

Commit

Permalink
Merge pull request #4 from allentje/master
Browse files Browse the repository at this point in the history
Merge allentje/Phue new command
  • Loading branch information
neoteknic authored Mar 5, 2020
2 parents 0007f0c + ab13521 commit 8a90f75
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
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 <sqmk@php.net>
* @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 <sqmk@php.net>
* @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);
}
}

0 comments on commit 8a90f75

Please sign in to comment.