From e19d2a5a660e4291a5eb3030ca0e31792312dbd3 Mon Sep 17 00:00:00 2001 From: "Michael K. Squires" Date: Fri, 26 Dec 2014 16:23:41 -0800 Subject: [PATCH] GitHub #89 - Add convenience method to delete rule, add related command --- library/Phue/Command/DeleteRule.php | 49 +++++++++++++++ library/Phue/Rule.php | 12 ++++ tests/Phue/Test/Command/DeleteRuleTest.php | 71 ++++++++++++++++++++++ tests/Phue/Test/RuleTest.php | 14 +++++ 4 files changed, 146 insertions(+) create mode 100644 library/Phue/Command/DeleteRule.php create mode 100644 tests/Phue/Test/Command/DeleteRuleTest.php diff --git a/library/Phue/Command/DeleteRule.php b/library/Phue/Command/DeleteRule.php new file mode 100644 index 0000000..6517fe4 --- /dev/null +++ b/library/Phue/Command/DeleteRule.php @@ -0,0 +1,49 @@ + + * @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 + ); + } +} diff --git a/library/Phue/Rule.php b/library/Phue/Rule.php index a6b2f2c..e86d28c 100644 --- a/library/Phue/Rule.php +++ b/library/Phue/Rule.php @@ -9,6 +9,8 @@ namespace Phue; +use Phue\Command\DeleteRule; + /** * Rule object */ @@ -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 * diff --git a/tests/Phue/Test/Command/DeleteRuleTest.php b/tests/Phue/Test/Command/DeleteRuleTest.php new file mode 100644 index 0000000..92a10d9 --- /dev/null +++ b/tests/Phue/Test/Command/DeleteRuleTest.php @@ -0,0 +1,71 @@ + + * @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); + } +} diff --git a/tests/Phue/Test/RuleTest.php b/tests/Phue/Test/RuleTest.php index 1e3c597..783675c 100644 --- a/tests/Phue/Test/RuleTest.php +++ b/tests/Phue/Test/RuleTest.php @@ -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 *