Skip to content

Commit

Permalink
GitHub #88 - Add commands to search and retrieve sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmk committed Dec 27, 2014
1 parent 02a2348 commit 03f83fe
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 0 deletions.
79 changes: 79 additions & 0 deletions library/Phue/Command/GetNewSensors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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;

/**
* Get new sensors command
*/
class GetNewSensors implements CommandInterface
{
/**
* Last scan
*
* @var string
*/
protected $lastScan;

/**
* Found sensors
*
* @var array
*/
protected $sensors = [];

/**
* Send command
*
* @param Client $client Phue Client
*
* @return self This object
*/
public function send(Client $client)
{
// Get response
$response = $client->getTransport()->sendRequest(
"/api/{$client->getUsername()}/sensors/new"
);

$this->lastScan = $response->lastscan;

// Remove scan from response
unset($response->lastscan);

// Iterate through left over properties as sensors
foreach ($response as $sensorId => $sensor) {
$this->sensors[$sensorId] = $sensor->name;
}

return $this;
}

/**
* Get sensors
*
* @return array List of new sensors
*/
public function getSensors()
{
return $this->sensors;
}

/**
* Is scan currently active
*
* @return bool True if active, false if not
*/
public function isScanActive()
{
return $this->lastScan == 'active';
}
}
37 changes: 37 additions & 0 deletions library/Phue/Command/StartSensorScan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\Transport\TransportInterface;

/**
* Start Sensor Scan command
*/
class StartSensorScan implements CommandInterface
{
/**
* Send command
*
* @param Client $client Phue Client
*
* @return mixed
*/
public function send(Client $client)
{
// Get response
$response = $client->getTransport()->sendRequest(
"/api/{$client->getUsername()}/sensors",
TransportInterface::METHOD_POST
);

return $response;
}
}
89 changes: 89 additions & 0 deletions tests/Phue/Test/Command/GetNewSensorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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\GetNewSensors;
use Phue\Transport\TransportInterface;

/**
* Tests for Phue\Command\GetNewSensors
*/
class GetNewSensorsTest extends \PHPUnit_Framework_TestCase
{
/**
* Set up
*/
public function setUp()
{
$this->getNewSensors = new GetNewSensors();

// Mock client
$this->mockClient = $this->getMock(
'\Phue\Client',
['getUsername', 'getTransport'],
['127.0.0.1']
);

// Mock transport
$this->mockTransport = $this->getMock(
'\Phue\Transport\TransportInterface',
['sendRequest']
);

// Stub client's getUsername method
$this->mockClient->expects($this->any())
->method('getUsername')
->will($this->returnValue('abcdefabcdef01234567890123456789'));

// Stub client's getTransport method
$this->mockClient->expects($this->any())
->method('getTransport')
->will($this->returnValue($this->mockTransport));

// Mock transport results
$mockTransportResults = (object) [
'1' => (object) ['name' => 'Sensor 1'],
'2' => (object) ['name' => 'Sensor 2'],
'lastscan' => 'active'
];

// Stub transport's sendRequest usage
$this->mockTransport->expects($this->once())
->method('sendRequest')
->with($this->equalTo("/api/{$this->mockClient->getUsername()}/sensors/new"))
->will($this->returnValue($mockTransportResults));
}

/**
* Test: Get new sensors
*
* @covers \Phue\Command\GetNewSensors::send
* @covers \Phue\Command\GetNewSensors::getSensors
* @covers \Phue\Command\GetNewSensors::isScanActive
*/
public function testGetNewSensors()
{
// Send command and get response
$response = $this->getNewSensors->send($this->mockClient);

// Ensure response is self object
$this->assertEquals($this->getNewSensors, $response);

// Ensure array of sensors
$this->assertInternalType('array', $response->getSensors());

// Ensure expected number of sensors
$this->assertEquals(2, count($response->getSensors()));

// Ensure lastscan is active
$this->assertTrue($response->isScanActive());
}
}
71 changes: 71 additions & 0 deletions tests/Phue/Test/Command/StartSensorScanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\StartSensorScan;
use Phue\Transport\TransportInterface;

/**
* Tests for Phue\Command\StartSensorScan
*/
class StartSensorScanTest extends \PHPUnit_Framework_TestCase
{
/**
* Set up
*/
public function setUp()
{
// Mock client
$this->mockClient = $this->getMock(
'\Phue\Client',
['getTransport'],
['127.0.0.1']
);

// Mock transport
$this->mockTransport = $this->getMock(
'\Phue\Transport\TransportInterface',
['sendRequest']
);

// Stub client's getUsername method
$this->mockClient->expects($this->any())
->method('getUsername')
->will($this->returnValue('abcdefabcdef01234567890123456789'));

// Stub client's getTransport method
$this->mockClient->expects($this->any())
->method('getTransport')
->will($this->returnValue($this->mockTransport));
}

/**
* Test: Send start sensor scan command
*
* @covers \Phue\Command\StartSensorScan::send
*/
public function testSend()
{
// Stub transport's sendRequest method
$this->mockTransport->expects($this->once())
->method('sendRequest')
->with(
$this->equalTo("/api/{$this->mockClient->getUsername()}/sensors"),
$this->equalTo('POST')
)
->will($this->returnValue('success!'));

$this->assertEquals(
'success!',
(new StartSensorScan)->send($this->mockClient)
);
}
}

0 comments on commit 03f83fe

Please sign in to comment.