Permalink
Browse files

GitHub #45 - Adding tests and test fixes.

  • Loading branch information...
1 parent 322c4e2 commit 555b08c9457f525db90a2554545e70d87afcc99c @sqmk committed Apr 2, 2013
View
@@ -25,11 +25,11 @@ class Schedule
protected $id;
/**
- * Schedule details
+ * Schedule attributes
*
* @var stdClass
*/
- protected $details;
+ protected $attributes;
/**
* Phue client
@@ -41,15 +41,15 @@ class Schedule
/**
* Construct a Phue Schedule object
*
- * @param int $id Id
- * @param stdClass $details Schedule details
- * @param Client $client Phue client
+ * @param int $id Id
+ * @param stdClass $attributes Schedule attributes
+ * @param Client $client Phue client
*/
- public function __construct($id, \stdClass $details, Client $client)
+ public function __construct($id, \stdClass $attributes, Client $client)
{
- $this->id = (int) $id;
- $this->details = $details;
- $this->client = $client;
+ $this->id = (int) $id;
+ $this->attributes = $attributes;
+ $this->client = $client;
}
/**
@@ -69,7 +69,7 @@ public function getId()
*/
public function getName()
{
- return $this->details->name;
+ return $this->attributes->name;
}
/**
@@ -83,7 +83,7 @@ public function setName($name)
(new SetScheduleAttributes($this))->name((string) $name)
);
- $this->details->name = (string) $name;
+ $this->attributes->name = (string) $name;
return $this;
}
@@ -95,7 +95,7 @@ public function setName($name)
*/
public function getDescription()
{
- return $this->details->description;
+ return $this->attributes->description;
}
/**
@@ -109,7 +109,7 @@ public function setDescription($description)
(new SetScheduleAttributes($this))->name((string) $description)
);
- $this->details->description = (string) $description;
+ $this->attributes->description = (string) $description;
return $this;
}
@@ -121,7 +121,7 @@ public function setDescription($description)
*/
public function getTime()
{
- return $this->details->time;
+ return $this->attributes->time;
}
/**
@@ -139,24 +139,27 @@ public function setTime($time)
// Build new time
$time = $command->convertTimeToUtcDate($time);
+ // Update the time, and set internal attribute
$this->client->sendCommand(
$command->time($time)
);
+ $this->attributes->time = $time;
+
return $this;
}
/**
* Get command
*
- * @return array Command details
+ * @return array Command attributes
*/
public function getCommand()
{
return [
- 'method' => $this->details->command->method,
- 'address' => $this->details->command->address,
- 'body' => $this->details->command->body
+ 'method' => $this->attributes->command->method,
+ 'address' => $this->attributes->command->address,
+ 'body' => $this->attributes->command->body
];
}
@@ -173,7 +176,7 @@ public function setCommand(SchedulableInterface $command)
(new SetScheduleAttributes($this))->command($command)
);
- $this->details->command = $command->getSchedulableParams($this->client);
+ $this->attributes->command = $command->getSchedulableParams($this->client);
return $this;
}
@@ -79,11 +79,7 @@ public function testName()
$command = (new CreateSchedule())->name('Dummy!');
// Ensure property is set properly
- $this->assertAttributeEquals(
- 'Dummy!',
- 'name',
- $command
- );
+ $this->assertAttributeContains('Dummy!', 'attributes', $command);
// Ensure self object is returned
$this->assertEquals($command, $command->name('Dummy!'));
@@ -99,11 +95,7 @@ public function testDescription()
$command = (new CreateSchedule())->description('Description!');
// Ensure property is set properly
- $this->assertAttributeEquals(
- 'Description!',
- 'description',
- $command
- );
+ $this->assertAttributeContains('Description!', 'attributes', $command);
// Ensure self object is returned
$this->assertEquals($command, $command->name('Description!'));
@@ -120,11 +112,7 @@ public function testTime()
$command = (new CreateSchedule())->time('2010-10-20T10:11:12');
// Ensure property is set properly
- $this->assertAttributeEquals(
- '2010-10-20T10:11:12',
- 'time',
- $command
- );
+ $this->assertAttributeContains('2010-10-20T10:11:12', 'attributes', $command);
// Ensure self object is returned
$this->assertEquals($command, $command->time('+10 seconds'));
@@ -160,7 +148,7 @@ public function testCommand()
);
// Ensure self object is returned
- $this->assertEquals($command, $command->time('+10 seconds'));
+ $this->assertEquals($command, $command->command($this->mockCommand));
}
/**
@@ -61,7 +61,7 @@ public function testSend()
->with("{$this->mockClient->getUsername()}/lights/10")
->will($this->returnValue(new \stdClass));
- // Get group
+ // Get light
$light = (new GetLightById(10))->send($this->mockClient);
// Ensure type is correct
@@ -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 PhueTest\Command;
+
+use Phue\Command\GetScheduleById;
+use Phue\Client;
+use Phue\Transport\TransportInterface;
+
+/**
+ * Tests for Phue\Command\GetScheduleById
+ */
+class GetScheduleByIdTest 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 getTransport usage
+ $this->mockClient->expects($this->any())
+ ->method('getTransport')
+ ->will($this->returnValue($this->mockTransport));
+ }
+
+ /**
+ * Test: Send get schedule by id command
+ *
+ * @covers \Phue\Command\GetScheduleById::__construct
+ * @covers \Phue\Command\GetScheduleById::send
+ */
+ public function testSend()
+ {
+ // Stub transport's sendRequest usage
+ $this->mockTransport->expects($this->once())
+ ->method('sendRequest')
+ ->with("{$this->mockClient->getUsername()}/schedules/9")
+ ->will($this->returnValue(new \stdClass));
+
+ // Get schedule
+ $schedule = (new GetScheduleById(9))->send($this->mockClient);
+
+ // Ensure type is correct
+ $this->assertInstanceOf(
+ '\Phue\Schedule',
+ $schedule
+ );
+ }
+}
@@ -0,0 +1,108 @@
+<?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 PhueTest\Command;
+
+use Phue\Command\SetScheduleAttributes;
+use Phue\Client;
+use Phue\Transport\TransportInterface;
+
+/**
+ * Tests for Phue\Command\SetScheduleAttributes
+ */
+class SetScheduleAttributesTest 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']
+ );
+
+ // Mock schedule
+ $this->mockSchedule = $this->getMock(
+ '\Phue\Schedule',
+ null,
+ [12, new \stdClass, $this->mockClient]
+ );
+
+ // 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 schedulable command
+ $this->mockCommand = $this->getMock(
+ '\Phue\Command\SchedulableInterface',
+ ['getSchedulableParams']
+ );
+ }
+
+ /**
+ * Test: Send command
+ *
+ * @covers \Phue\Command\SetScheduleAttributes::__construct
+ * @covers \Phue\Command\SetScheduleAttributes::send
+ */
+ public function testSend()
+ {
+ // Build command
+ $setScheduleAttributesCmd = new SetScheduleAttributes($this->mockSchedule);
+
+ // Set expected payload
+ $this->stubTransportSendRequestWithPayload(
+ (object) [
+ 'name' => 'Dummy!',
+ 'description' => 'Dummy description',
+ 'command' => NULL,
+ ]
+ );
+
+ // Change name, description
+ $setScheduleAttributesCmd->name('Dummy!')
+ ->description('Dummy description')
+ ->command($this->mockCommand)
+ ->send($this->mockClient);
+ }
+
+ /**
+ * Stub transport's sendRequest with an expected payload
+ *
+ * @param \stdClass $payload Payload
+ */
+ protected function stubTransportSendRequestWithPayload(\stdClass $payload)
+ {
+ // Stub transport's sendRequest usage
+ $this->mockTransport->expects($this->once())
+ ->method('sendRequest')
+ ->with(
+ $this->equalTo(
+ "{$this->mockClient->getUsername()}/schedules/{$this->mockSchedule->getId()}"
+ ),
+ $this->equalTo('PUT'),
+ $payload
+ );
+ }
+}
Oops, something went wrong.

0 comments on commit 555b08c

Please sign in to comment.