Skip to content

Commit

Permalink
Refactor client to allow initialization with features
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrabera committed May 30, 2017
1 parent 5086ab6 commit daad5c7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 68 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
65 changes: 32 additions & 33 deletions src/Laika.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
42 changes: 8 additions & 34 deletions tests/LaikaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit daad5c7

Please sign in to comment.