From c0d19ba6e6aabaded82d34d7b3e2b97a8ddb8fa0 Mon Sep 17 00:00:00 2001 From: "Michael K. Squires" Date: Fri, 6 Nov 2015 20:35:51 -0600 Subject: [PATCH] GitHub #95 - Selecting usernames will be deprecated in the future --- README.md | 14 ++++---- bin/phue-create-user | 10 +++--- library/Phue/Command/CreateUser.php | 42 +--------------------- tests/Phue/Test/Command/CreateUserTest.php | 21 ++--------- 4 files changed, 16 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 932ff51..712f347 100644 --- a/README.md +++ b/README.md @@ -102,11 +102,11 @@ If the username provided is not created, you can use the convenience script to a ```php // Push the bridge's link button prior to running this try { - $client->sendCommand( - new \Phue\Command\CreateUser($client->getUsername()) + $response = $client->sendCommand( + new \Phue\Command\CreateUser ); - echo 'You are now authenticated'; + echo 'New user created: ' . $response->username; } catch (\Phue\Transport\Exception\LinkButtonException $e) { echo 'The link button was not pressed!'; } @@ -491,12 +491,12 @@ If the script provided doesn't find your bridge, or if you don't have internet c To test connectivity and authenticate with the bridge, you can use ```bin/phue-create-user```. The script uses the Phue library to make requests and receive responses from the Philips Hue bridge. -At this point, you should be ready to authenticate with the bridge. The bridge expects a username between 10 and 40 characters with alphanumeric characters to authenticate with. +At this point, you should be ready to authenticate with the bridge. The bridge will generate a username for you. Here's how to run the script for authenticating/creating a user: ``` -$ ./bin/phue-create-user 10.0.1.31 yourusername +$ ./bin/phue-create-user 10.0.1.31 ``` If the connection is ok, you will get a response similar to this: @@ -516,10 +516,10 @@ Attempting to create user: Press the Bridge's button! Waiting.......... -Successfully created new user: yourusername +Successfully created new user: abcdef0123456 ``` -From then on, you should be able to use the username you just created for interacting with the Philips Hue bridge! +From then on, you should be able to use the username generated for interacting with the Philips Hue bridge! ### Scanning / registering new lights diff --git a/bin/phue-create-user b/bin/phue-create-user index 0d83bc8..71c559f 100755 --- a/bin/phue-create-user +++ b/bin/phue-create-user @@ -7,16 +7,16 @@ if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../../../autoload.php'; } -// Show usage if host and username not passed -if (!isset($argv[1], $argv[2])) { +// Show usage if host is not passed +if (!isset($argv[1])) { echo "Philips Hue User Creator", "\n\n", "Usage:", "\n", - " ", "\n\n"; + " ", "\n\n"; exit(1); } // Initialize client -$client = new \Phue\Client($argv[1], $argv[2]); +$client = new \Phue\Client($argv[1]); echo "Testing connection to bridge at {$client->getHost()}", "\n"; @@ -39,7 +39,7 @@ $maxTries = 30; for ($i = 1; $i <= $maxTries; ++$i) { try { $response = $client->sendCommand( - new \Phue\Command\CreateUser($argv[2]) + new \Phue\Command\CreateUser ); echo "\n\n", "Successfully created new user: {$response->username}", "\n\n"; diff --git a/library/Phue/Command/CreateUser.php b/library/Phue/Command/CreateUser.php index 97178fa..c8317f9 100644 --- a/library/Phue/Command/CreateUser.php +++ b/library/Phue/Command/CreateUser.php @@ -22,13 +22,6 @@ class CreateUser implements CommandInterface */ const DEFAULT_DEVICE_TYPE = 'Phue'; - /** - * Username to create - * - * @var string - */ - protected $username; - /** * Device type * @@ -39,41 +32,13 @@ class CreateUser implements CommandInterface /** * Instantiates a create user command * - * @param string $username Username * @param string $deviceType Device type */ - public function __construct($username = null, $deviceType = self::DEFAULT_DEVICE_TYPE) + public function __construct($deviceType = self::DEFAULT_DEVICE_TYPE) { - $this->setUsername($username); $this->setDeviceType($deviceType); } - /** - * Set username - * - * @param string $username Username - * - * @throws \InvalidArgumentException - * - * @return self This 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 * @@ -129,11 +94,6 @@ protected function buildRequestData(Client $client) 'devicetype' => $this->deviceType ]; - // Leave username blank if one not provided - if ($this->username !== null) { - $request['username'] = (string) $this->username; - } - return (object) $request; } } diff --git a/tests/Phue/Test/Command/CreateUserTest.php b/tests/Phue/Test/Command/CreateUserTest.php index b1fa039..7082eee 100644 --- a/tests/Phue/Test/Command/CreateUserTest.php +++ b/tests/Phue/Test/Command/CreateUserTest.php @@ -51,25 +51,11 @@ public function setUp() * Test: Instantiating CreateUser command * * @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'); + $command = new CreateUser('phpunit'); } /** @@ -93,8 +79,7 @@ public function testExceptionOnInvalidDeviceType() */ public function testSend() { - // Set up username and device type to pass to create user command - $username = 'testuser0123'; + // Set up device type to pass to create user command $deviceType = 'phpunit'; // Stub transport's sendRequest method @@ -109,7 +94,7 @@ public function testSend() $this->assertEquals( 'success!', - (new CreateUser('testuser0123', 'phpunit'))->send($this->mockClient) + (new CreateUser('phpunit'))->send($this->mockClient) ); } }