From daad5c7d3932ac12d263844d8ca17e598a33b7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Barber=C3=A1?= Date: Tue, 30 May 2017 12:09:45 +0200 Subject: [PATCH] Refactor client to allow initialization with features --- composer.json | 2 +- src/Laika.php | 65 ++++++++++++++++++++++----------------------- tests/LaikaTest.php | 42 ++++++----------------------- 3 files changed, 41 insertions(+), 68 deletions(-) diff --git a/composer.json b/composer.json index 5d68e52..401ffc4 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "medigo/laika", - "version": "1.0.0", + "version": "2.0.0", "type": "library", "homepage": "https://github.com/MEDIGO/laika-php", "license": "MIT", diff --git a/src/Laika.php b/src/Laika.php index e0133fc..6fe6f1b 100644 --- a/src/Laika.php +++ b/src/Laika.php @@ -48,85 +48,84 @@ class Laika /** * Constructor function for Laika * - * @param string $environmentName environment in which the code is being executed. - * @param string $url url for the API server. - * @param string $username username for the basic authentication. - * @param string $password password for the basic authentication. + * @param string $environmentName Environment in which the code is being executed. + * @param string $url URL for the API server. + * @param string $username Username for the basic authentication. + * @param string $password Password for the basic authentication. + * @param array $features Optional array with features indexed by name. If the array is not + * provided the features will be fetched from the client. */ - public function __construct($environmentName, $url, $username, $password) + public function __construct($environmentName, $url, $username, $password, $features = null) { $this->client = new Client(); $this->environmentName = $environmentName; $this->url = $url; $this->username = $username; $this->password = $password; + $this->features = $features; } /** - * Setter for the feature array. + * Get all the features. * - * @param array $features array with features. Each feature is also an array. + * @return array $features array with features indexed by name. */ - public function setFeatures($features) + public function getFeatures() { - $this->features = $features; + if (!$this->features) $this->preloadFeatures(); + return $this->features; } /** * Retrieves all the existing features through an HTTP request and adds them to the features array. * - * @return boolean true if function executed as expected, false if problems occurred. + * @return boolean True if function executed as expected, false if problems occurred. */ - public function fetchAllFeatures() + protected function preloadFeatures() { - $requestResult = $this->httpRequest('api/features'); - if ($requestResult === false) { - return false; - } + $features = $this->get('api/features'); - foreach ($requestResult as $featureValue) { - $this->features[$featureValue['name']] = $featureValue; + $this->features = array(); + foreach ($features as $feature) { + $this->features[$feature['name']] = $feature; } - - return true; } /** - * Executes HTTP requests. + * Performs an HTTP Get requests, returning the body parsed as JSON. * * @param string API endpoint. - * @return array|boolean returns an array with the information decoded from the json. If problems occurred, returns false. + * @return array|boolean Returns an array with the information decoded from the json. If problems + * occurred, returns false. */ - protected function httpRequest($endpoint) + protected function get($endpoint) { $res = $this->client->get($this->url . $endpoint, [ 'auth' => [$this->username, $this->password] ]); - if ($res->getStatusCode() === '200') { - $body = $res->getBody(); - - $decodeResult = json_decode($body, true); - if (is_null($decodeResult)) { - return false; - } - - return $decodeResult; + $payload = json_decode($res->getBody(), true); + if (is_null($payload)) { + throw new Exception('Failed to decode JSON'); } - return false; + + return $payload; } /** * Checks if feature is enabled in the current environment. * * @param string $featureName name of the feature to check. - * @return boolean true if feature is enabled, false if it is disabled or if problems occurred. + * @return boolean True if feature is enabled, false if it is disabled or if problems occurred. */ public function isEnabled($featureName) { + if (!$this->features) $this->preloadFeatures(); + if (!isset($this->features[$featureName])) { return false; } + return $this->features[$featureName]['status'][$this->environmentName]; } } diff --git a/tests/LaikaTest.php b/tests/LaikaTest.php index 3d91a0f..f8df77a 100644 --- a/tests/LaikaTest.php +++ b/tests/LaikaTest.php @@ -4,66 +4,40 @@ use Medigo\Laika; -/** - * Class LaikaTest - **/ - -$mock = Phake::mock('Medigo\Laika'); - class LaikaTest extends \PHPUnit_Framework_TestCase { - /** - * Partial mock of Laika, the class being tested. - * - * @var Laika - */ + private $laika; - /** - * Array used on method stubbing to fake the information received from the server. - * - * @var array - */ private $features = array( "f1" => array( "id" => 1, "created_at" => "2016-03-04T00:00:00Z", "name" => "f1", - "status" => array('e' => true) + "status" => array('test' => true) ), "f2" => array( "id" => 2, "created_at" => "2016-03-04T00:00:00Z", "name" => "f2", - "status" => array('e' => false) + "status" => array('test' => false) ) ); - /** - * Constructor function for LaikaTest. - */ public function setUp() { - $this->laika = Phake::partialMock('Medigo\Laika', 'e', 'url', null, null); + $this->laika = Phake::partialMock('Medigo\Laika', 'test', 'http://example.org/', 'user', 'password'); + Phake::when($this->laika)->get('api/features')->thenReturn($this->features); } - /** - * Tests Laika's "fetchAllFeatures" function. - */ - public function testFetchAllFeatures() + public function testGetFeatures() { - //checks if laika can get the features and process them successfully - Phake::when($this->laika)->httpRequest('api/features')->thenReturn($this->features); - $this->assertEquals(true, $this->laika->fetchAllFeatures()); + $features = $this->laika->getFeatures(); + $this->assertEquals($this->features, $features); } - /** - * Tests Laika's "isEnabled" function. - */ public function testIsEnabled() { - $this->laika->setFeatures($this->features); - //checks the status of an enabled feature $this->assertEquals(true, $this->laika->isEnabled('f1')); //checks the status of a disabled feature