Browse files

GitHub #46, #47 - Rename Authenticate command to CreateUser. Username…

… pattern has been corrected.
  • Loading branch information...
1 parent e3488e5 commit 8169c74b5cbee171aab1e068cb99613657975056 @sqmk committed Mar 30, 2013
View
27 bin/phue-authenticate → bin/phue-create-user
@@ -9,7 +9,7 @@ if (is_file(__DIR__ . '/../vendor/autoload.php')) {
// Show usage if host and username not passed
if (!isset($argv[1], $argv[2])) {
- echo "Philips Hue Authenticator", "\n\n",
+ echo "Philips Hue User Creator", "\n\n",
"Usage:", "\n",
" <host> <username>", "\n\n";
exit(1);
@@ -18,12 +18,6 @@ if (!isset($argv[1], $argv[2])) {
// Initialize client
$client = new \Phue\Client($argv[1], $argv[2]);
-// Hash username if doesn't appear to be hash
-if ($client->getUsername() != $argv[2]) {
- echo "! - Username {$argv[2]} doesn't appear to be 32 character hash (A-F, 0-9)", "\n",
- "! - Using this for username instead: {$client->getUsername()}", "\n\n";
-}
-
echo "Testing connection to bridge at {$client->getHost()}", "\n";
try {
@@ -36,30 +30,31 @@ try {
exit(1);
}
-echo "Attempting to authenticate ({$client->getUsername()}):", "\n",
+echo "Attempting to create user:", "\n",
"Press the Bridge's button!", "\n",
"Waiting.";
-// Try X times to authenticate
+// Try X times to create user
$maxTries = 30;
for ($i = 1; $i <= $maxTries; ++$i) {
try {
$response = $client->sendCommand(
- new \Phue\Command\Authenticate
+ new \Phue\Command\CreateUser($argv[2])
);
- if ($response->username == $client->getUsername()) {
- echo "\n", "Authentication for user {$client->getUsername()} was successful!", "\n\n";
+ echo "\n", "Successfully created new user: {$response->username}", "\n\n";
- exit(0);
- }
+ break;
} catch (\Phue\Transport\Exception\LinkButtonException $e) {
echo ".";
+ } catch (Exception $e) {
+ echo "\n", "Failure to create user. Please try again!",
+ "\n", "Reason: {$e->getMessage()}", "\n\n";
+
+ break;
}
sleep(1);
}
-echo "\n", "Failure to authenticate. Please try again!", "\n\n";
-
exit(1);
View
2 composer.json
@@ -33,7 +33,7 @@
}
},
"bin": [
- "bin/phue-authenticate",
+ "bin/phue-create-user",
"bin/phue-bridge-finder",
"bin/phue-light-finder",
"bin/phue-list-lights"
View
7 library/Phue/Client.php
@@ -25,7 +25,7 @@ class Client
/**
* Client name
*/
- const CLIENT_NAME = 'Phue';
+ const DEFAULT_DEVICE_TYPE = 'Phue';
/**
* Host address
@@ -95,11 +95,6 @@ public function getUsername()
*/
public function setUsername($username)
{
- // Hash username if not already in hash format
- if (!preg_match('/[a-f0-9]{32}/i', $username)) {
- $username = md5($username);
- }
-
$this->username = (string) $username;
}
View
53 library/Phue/Command/Authenticate.php
@@ -1,53 +0,0 @@
-<?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;
-
-/**
- * Authenticate command
- */
-class Authenticate implements CommandInterface
-{
- /**
- * Send command
- *
- * @param Client $client Phue Client
- *
- * @return stdClass Authentication response
- */
- public function send(Client $client)
- {
- // Get response
- $response = $client->getTransport()->sendRequest(
- '',
- TransportInterface::METHOD_POST,
- $this->buildRequestData($client)
- );
-
- return $response;
- }
-
- /**
- * Build request data
- *
- * @param Client $client Phue client
- *
- * @return stdClass Request data object
- */
- protected function buildRequestData(Client $client)
- {
- return (object) [
- 'username' => $client->getUsername(),
- 'devicetype' => $client::CLIENT_NAME
- ];
- }
-}
View
124 library/Phue/Command/CreateUser.php
@@ -0,0 +1,124 @@
+<?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;
+
+/**
+ * Create user command
+ */
+class CreateUser implements CommandInterface
+{
+ /**
+ * Username to create
+ *
+ * @var string
+ */
+ protected $username;
+
+ /**
+ * Device type
+ *
+ * @var string
+ */
+ protected $deviceType;
+
+ /**
+ * Instantiates a create user command
+ *
+ * @param string $username Username
+ * @param string $deviceType Device type
+ */
+ public function __construct($username = null, $deviceType = Client::DEFAULT_DEVICE_TYPE)
+ {
+ $this->setUsername($username);
+ $this->setDeviceType($deviceType);
+ }
+
+ /**
+ * Set username
+ *
+ * @param string $username Username
+ *
+ * @return CreateUser Self object
+ */
+ public function setUsername($username)
+ {
+ // Allow for null username
+ if ($username === null) {
+ return;
+ }
+
+ // Match username format
+ if (!preg_match('/^[a-z0-9]{10,40}$/i', $username)) {
+ throw new \InvalidArgumentException(
+ "Username must contain alphanumeric characters, and be between 10 and 40 characters"
+ );
+ }
+
+ $this->username = $username;
+ }
+
+ /**
+ * Set device type
+ *
+ * @param string $deviceType Device type
+ *
+ * @return CreateUser Self object
+ */
+ public function setDeviceType($deviceType)
+ {
+ $this->deviceType = (string) $deviceType;
+
+ return $this;
+ }
+
+ /**
+ * Send command
+ *
+ * @param Client $client Phue Client
+ *
+ * @return stdClass Authentication response
+ */
+ public function send(Client $client)
+ {
+ // Get response
+ $response = $client->getTransport()->sendRequest(
+ '',
+ TransportInterface::METHOD_POST,
+ $this->buildRequestData($client)
+ );
+
+ return $response;
+ }
+
+ /**
+ * Build request data
+ *
+ * @param Client $client Phue client
+ *
+ * @return stdClass Request data object
+ */
+ protected function buildRequestData(Client $client)
+ {
+ // Initialize data to send
+ $request = [
+ 'devicetype' => $this->deviceType
+ ];
+
+ // Leave username blank if one not provided
+ if ($this->username !== null) {
+ $request['username'] = (string) $this->username;
+ }
+
+ return (object) $request;
+ }
+}
View
20 tests/PhueTest/ClientTest.php
@@ -47,29 +47,13 @@ public function testGetHost()
* @covers \Phue\Client::getUsername
* @covers \Phue\Client::setUsername
*/
- public function testNonHashedUsername()
+ public function testGetSetUsername()
{
$this->client->setUsername('dummy');
$this->assertEquals(
$this->client->getUsername(),
- '275876e34cf609db118f3d84b799a790'
- );
- }
-
- /**
- * Test: Setting hashed username
- *
- * @covers \Phue\Client::getUsername
- * @covers \Phue\Client::setUsername
- */
- public function testHashedUsername()
- {
- $this->client->setUsername('275876e34cf609db118f3d84b799a790');
-
- $this->assertEquals(
- $this->client->getUsername(),
- '275876e34cf609db118f3d84b799a790'
+ 'dummy'
);
}
View
43 tests/PhueTest/Command/AuthenticateTest.php → tests/PhueTest/Command/CreateUserTest.php
@@ -9,14 +9,14 @@
namespace PhueTest\Command;
-use Phue\Command\Authenticate;
+use Phue\Command\CreateUser;
use Phue\Client;
use Phue\Transport\TransportInterface;
/**
- * Tests for Phue\Command\Authenticate
+ * Tests for Phue\Command\CreateUser
*/
-class AuthenticateTest extends \PHPUnit_Framework_TestCase
+class CreateUserTest extends \PHPUnit_Framework_TestCase
{
/**
* Set up
@@ -48,13 +48,42 @@ public function setUp()
}
/**
- * Test: Send authentication command
+ * Test: Instantiating CreateUser command
*
- * @covers \Phue\Command\Authenticate::send
- * @covers \Phue\Command\Authenticate::buildRequestData
+ * @covers \Phue\Command\CreateUser::__construct
+ * @covers \Phue\Command\CreateUser::setUsername
+ * @covers \Phue\Command\CreateUser::setDeviceType
+ */
+ public function testInstantiation()
+ {
+ $command = new CreateUser('testuser0123', 'phpunit');
+ }
+
+ /**
+ * Test: Setting invalid username
+ *
+ * @covers \Phue\Command\CreateUser::setUsername
+ *
+ * @expectedException \InvalidArgumentException
+ */
+ public function testExceptionOnInvalidUsername()
+ {
+ $command = new CreateUser;
+ $command->setUsername('test');
+ }
+
+ /**
+ * Test: Send create user command
+ *
+ * @covers \Phue\Command\CreateUser::send
+ * @covers \Phue\Command\CreateUser::buildRequestData
*/
public function testSend()
{
+ // Set up username and device type to pass to create user command
+ $username = 'testuser0123';
+ $deviceType = 'phpunit';
+
// Stub transport's sendRequest method
$this->mockTransport->expects($this->once())
->method('sendRequest')
@@ -66,7 +95,7 @@ public function testSend()
->will($this->returnValue('success!'));
$this->assertEquals(
- (new Authenticate)->send($this->mockClient),
+ (new CreateUser('testuser0123', 'phpunit'))->send($this->mockClient),
'success!'
);
}

0 comments on commit 8169c74

Please sign in to comment.