Skip to content

Commit

Permalink
GitHub #89 - Add convenience method to delete rule, add related command
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmk committed Dec 27, 2014
1 parent 5a34959 commit e19d2a5
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
49 changes: 49 additions & 0 deletions library/Phue/Command/DeleteRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Transport\TransportInterface;

/**
* Delete rule command
*/
class DeleteRule implements CommandInterface
{
/**
* Rule Id
*
* @var string
*/
protected $ruleId;

/**
* Constructs a command
*
* @param mixed $rule Rule Id or Rule object
*/
public function __construct($rule)
{
$this->ruleId = (string) $rule;
}

/**
* Send command
*
* @param Client $client Phue Client
*/
public function send(Client $client)
{
$client->getTransport()->sendRequest(
"/api/{$client->getUsername()}/rules/{$this->ruleId}",
TransportInterface::METHOD_DELETE
);
}
}
12 changes: 12 additions & 0 deletions library/Phue/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Phue;

use Phue\Command\DeleteRule;

/**
* Rule object
*/
Expand Down Expand Up @@ -129,6 +131,16 @@ public function isEnabled()
return $this->attributes->status == self::STATUS_ENABLED;
}

/**
* Delete rule
*/
public function delete()
{
$this->client->sendCommand(
(new DeleteRule($this))
);
}

/**
* __toString
*
Expand Down
71 changes: 71 additions & 0 deletions tests/Phue/Test/Command/DeleteRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\DeleteRule;
use Phue\Transport\TransportInterface;

/**
* Tests for Phue\Command\DeleteRule
*/
class DeleteRuleTest extends \PHPUnit_Framework_TestCase
{
/**
* Set up
*/
public function setUp()
{
// 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));
}

/**
* Test: Send command
*
* @covers \Phue\Command\DeleteRule::__construct
* @covers \Phue\Command\DeleteRule::send
*/
public function testSend()
{
$command = new DeleteRule(5);

// Stub transport's sendRequest usage
$this->mockTransport->expects($this->once())
->method('sendRequest')
->with(
$this->equalTo("/api/{$this->mockClient->getUsername()}/rules/5"),
$this->equalTo(TransportInterface::METHOD_DELETE)
);

// Send command
$command->send($this->mockClient);
}
}
14 changes: 14 additions & 0 deletions tests/Phue/Test/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ public function testIsEnabled()
);
}

/**
* Test: Delete
*
* @covers \Phue\Rule::delete
*/
public function testDelete()
{
$this->mockClient->expects($this->once())
->method('sendCommand')
->with($this->isInstanceOf('\Phue\Command\DeleteRule'));

$this->rule->delete();
}

/**
* Test: toString
*
Expand Down

0 comments on commit e19d2a5

Please sign in to comment.