Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jkniest committed Aug 5, 2023
1 parent ab4467a commit 0833c3e
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/Clients/HueClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@

interface HueClient
{
public function isAuthenticated(): bool;

public function authenticate(): void;

public function get(string $endpoint): array;
}
21 changes: 21 additions & 0 deletions src/Clients/LocalHueClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace jkniest\HueIt\Clients;

class LocalHueClient implements HueClient
{
public function get(string $endpoint): array
{
// TODO: Implement get() method.

Check failure on line 9 in src/Clients/LocalHueClient.php

View workflow job for this annotation

GitHub Actions / PHPstan (8.1, lowest)

Method jkniest\HueIt\Clients\LocalHueClient::get() should return array but return statement is missing.

Check failure on line 9 in src/Clients/LocalHueClient.php

View workflow job for this annotation

GitHub Actions / PHPstan (8.2, stable)

Method jkniest\HueIt\Clients\LocalHueClient::get() should return array but return statement is missing.
}

public function isAuthenticated(): bool
{
// TODO: Implement isAuthenticated() method.

Check failure on line 14 in src/Clients/LocalHueClient.php

View workflow job for this annotation

GitHub Actions / PHPstan (8.1, lowest)

Method jkniest\HueIt\Clients\LocalHueClient::isAuthenticated() should return bool but return statement is missing.

Check failure on line 14 in src/Clients/LocalHueClient.php

View workflow job for this annotation

GitHub Actions / PHPstan (8.2, stable)

Method jkniest\HueIt\Clients\LocalHueClient::isAuthenticated() should return bool but return statement is missing.
}

public function authenticate(): void
{
// TODO: Implement authenticate() method.
}
}
10 changes: 10 additions & 0 deletions src/Exceptions/HueItException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace jkniest\HueIt\Exceptions;

use Exception;

class HueItException extends Exception
{

}
13 changes: 13 additions & 0 deletions src/Exceptions/NotAuthenticatedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace jkniest\HueIt\Exceptions;

use Throwable;

class NotAuthenticatedException extends HueItException
{
public function __construct(int $code = 0, ?Throwable $previous = null)
{
parent::__construct('Not authenticated', $code, $previous);
}
}
23 changes: 23 additions & 0 deletions src/Fake/FakeHueClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
namespace jkniest\HueIt\Fake;

use jkniest\HueIt\Clients\HueClient;
use jkniest\HueIt\Exceptions\NotAuthenticatedException;
use jkniest\HueIt\Fake\Model\FakeLight;

class FakeHueClient implements HueClient
{
public const VALID_HOST = 'http://valid.hue';
public const VALID_TOKEN = 'valid-fake-token';

private bool $authenticated = false;

/** @var FakeLight[] */
private array $fakeLights = [];

Expand All @@ -20,8 +26,15 @@ public function setFakeLights(array $lights): void
$this->fakeLights = $lights;
}

/**
* @throws NotAuthenticatedException
*/
public function get(string $endpoint): array
{
if(!$this->authenticated) {
throw new NotAuthenticatedException();
}

switch ($endpoint) {
case '/resource/light':
return [
Expand All @@ -32,4 +45,14 @@ public function get(string $endpoint): array
return [];
}
}

public function isAuthenticated(): bool
{
return $this->authenticated;
}

public function authenticate(): void
{
$this->authenticated = true;
}
}
16 changes: 14 additions & 2 deletions src/PhillipsHue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

namespace jkniest\HueIt;

use jkniest\HueIt\Clients\LocalHueClient;
use jkniest\HueIt\Models\Light;
use Illuminate\Support\Collection;
use jkniest\HueIt\Clients\HueClient;

class PhillipsHue
{
private HueClient $client;
public function __construct(
private HueClient $client = new LocalHueClient()
)
{
}

public function setClient(HueClient $client): void
{
Expand All @@ -22,6 +27,13 @@ public function getClient(): HueClient
return $this->client;
}

public function authenticate(string $host, string $token): self
{
$this->client->authenticate();

return $this;
}

/**
* @return Collection<string, Light>
*/
Expand All @@ -30,6 +42,6 @@ public function getLights(): Collection
$lights = $this->client->get('/resource/light');

return (new Collection($lights['data']))
->map(static fn (array $light) => new Light($light));
->map(static fn(array $light) => new Light($light));
}
}
17 changes: 16 additions & 1 deletion tests/Feature/GetLocalLightsTest.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
<?php

use Illuminate\Support\Collection;
use jkniest\HueIt\Exceptions\NotAuthenticatedException;
use jkniest\HueIt\Fake\FakeHueClient;
use jkniest\HueIt\Fake\Model\FakeLight;
use jkniest\HueIt\Models\Light;
use jkniest\HueIt\PhillipsHue;

it('can return a collection of lights', function () {
$client = new FakeHueClient();

$client->setFakeLights([
FakeLight::create()->name('Example Light 1'),
FakeLight::create(),
FakeLight::create(),
]);

$hue = new PhillipsHue('123.456.789.1', 'my-token-123');
expect($client->isAuthenticated())->toBeFalse();

$hue = new PhillipsHue();
$hue->setClient($client);

$authResult = $hue->authenticate(FakeHueClient::VALID_HOST, FakeHueClient::VALID_TOKEN);
expect($authResult)->toBe($hue)
->and($client->isAuthenticated())->toBeTrue();

$lights = $hue->getLights();

expect($lights)->toBeInstanceOf(Collection::class)
->and($lights)->toHaveCount(3)
->and($lights->first())->toBeInstanceOf(Light::class)
->and($lights->first()->name)->toBe('Example Light 1');
});

it('throws an exception if not authenticated', function () {
$hue = new PhillipsHue();
$hue->setClient(new FakeHueClient());

$hue->getLights();
})->throws(NotAuthenticatedException::class);
25 changes: 25 additions & 0 deletions tests/Unit/Clients/LocalHueClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use jkniest\HueIt\Clients\LocalHueClient;

todo('can authenticate', function (): void {
$client = new LocalHueClient();

expect($client->isAuthenticated())->toBeFalse();

$client->authenticate('127.0.0.1', 'my-token-123');

expect($client->isAuthenticated())->toBeTrue()
->and($client->getHost())->toBe('127.0.0.1')
->and($client->getToken())->toBe('my-token-123');
});

todo('can fetch resources');

todo('it throws an exception if not authenticated');

todo('it throws an exception if the bridge is not reachable');

todo('it throws an exception if the resource is not found');

todo('it throws an exception if the request is invalid');
14 changes: 12 additions & 2 deletions tests/Unit/Fake/FakeHueClientTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

use jkniest\HueIt\Exceptions\NotAuthenticatedException;
use jkniest\HueIt\Fake\FakeHueClient;
use jkniest\HueIt\Fake\Model\FakeLight;

it('can return fake lights', function () {
$client = new FakeHueClient();
$client->authenticate(FakeHueClient::VALID_HOST, FakeHueClient::VALID_TOKEN);

$client->setFakeLights([
FakeLight::create()->id('id-123')->name('Test Light'),
FakeLight::create()->id('id-456')->name('Test Light 2'),
Expand All @@ -23,8 +26,15 @@
->and($lights['data'][1]['metadata']['name'])->toBe('Test Light 2');
});

it('returns an empty array if the endpoint is unknown', function () {
it('returns an empty array if the endpoint is unknown', function (): void {
$client = new FakeHueClient();
$client->authenticate(FakeHueClient::VALID_HOST, FakeHueClient::VALID_TOKEN);

expect($client->get('/unknown/endpoint'))->toBeEmpty();
});
});

it('throws exception if not authenticated', function (): void {
$client = new FakeHueClient();

$client->get('/resource/light');
})->throws(NotAuthenticatedException::class);
8 changes: 6 additions & 2 deletions tests/Unit/PhillipsHueTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Illuminate\Support\Collection;
use jkniest\HueIt\Clients\LocalHueClient;
use jkniest\HueIt\Fake\FakeHueClient;
use jkniest\HueIt\PhillipsHue;

Expand All @@ -13,4 +13,8 @@
expect($hue->getClient())->toBe($fakeClient);
});

todo('it uses the local client by default');
it('it uses the local client by default', function(): void {
$hue = new PhillipsHue();

expect($hue->getClient())->toBeInstanceOf(LocalHueClient::class);
});

0 comments on commit 0833c3e

Please sign in to comment.