Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
rearrange tests
update error class
  • Loading branch information
adrianmejias committed Sep 20, 2017
1 parent 1b44ea4 commit 7444a60
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 24 deletions.
72 changes: 72 additions & 0 deletions src/Exceptions/InvalidFactomApiConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,83 @@

class InvalidFactomApiConfig extends Exception
{
/**
* @return static
*/
public static function noCurlFound()
{
return new static('The Factom API integration requires the cURL extension, please see http://curl.haxx.se/docs/install.html to install it');
}

/**
* @return static
*/
public static function noUrlDefined()
{
return new static('The Factom API requires a url to connect to');
}

/**
* @return static
*/
public static function noCertificateDefined()
{
return new static('When enabling SSL configuration, you must ensure to define a certificate');
}

/**
* @return static
*/
public static function noSecureUrlDefined()
{
return new static('When defining a certificate, you must ensure the host is using HTTPS');
}

/**
* @return static
*/
public static function noCertificateExists()
{
return new static('Can\'t find provided certificate file');
}

/**
* @return static
*/
public static function noUsernameDefined()
{
return new static('You must provide a password with a username');
}

/**
* @return static
*/
public static function noPasswordDefined()
{
return new static('You must provide a username with a password');
}

/**
* @return static
*/
public static function invalidMethodCalled()
{
return new static('Supplied method must match GET or POST');
}

/**
* @return static
*/
public static function invalidApiResponse(string $error, string $actionName)
{
return new static('Received error "'.$error.'" when hitting "'.$actionName.'" within the Factom API');
}

/**
* @return static
*/
public static function emptyApiResponse(string $actionName)
{
return new static('Received an empty response when hitting "'.$actionName.'" within the Factom API');
}
}
33 changes: 9 additions & 24 deletions src/FactomConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ public function __construct(string $url, bool $ssl = false, string $certificate
$this->password = $password;

if (! function_exists('curl_init')) {
abort(503, 'The Factom API integration requires the cURL extension, please see http://curl.haxx.se/docs/install.html to install it');
throw InvalidFactomApiConfig::noCurlFound();
} elseif (empty($this->url)) {
throw InvalidFactomApiConfig::noUrlDefined();
} elseif (empty($this->certificate) && $this->ssl) {
abort(503, 'When enabling SSL configuration, you must ensure to define a certificate');
throw InvalidFactomApiConfig::noCertificateDefined();
} elseif (! empty($this->certificate) && $this->ssl) {
if (preg_match('/^(https:\/\/)/i', $this->url)) {
abort(503, 'When defining a certificate, you must ensure the host is using HTTPS');
throw InvalidFactomApiConfig::noSecureUrlDefined();
} elseif (! file_exists($this->certificate)) {
abort(503, 'Can\'t find provided certificate file');
throw InvalidFactomApiConfig::noCertificateExists();
}
} elseif (! empty($this->username) && empty($this->password)) {
abort(503, 'You must provide a password with a username.');
throw InvalidFactomApiConfig::noUsernameDefined();
} elseif (empty($this->username) && ! empty($this->password)) {
abort(503, 'You must provide a username with a password.');
throw InvalidFactomApiConfig::noPasswordDefined();
}

if (! $this->ssl) {
Expand All @@ -102,7 +102,7 @@ public function __construct(string $url, bool $ssl = false, string $certificate
private function gatherCurlOptions(string $actionName, string $method, array $binaryDataParams = [], array $customOptions = [])
{
if (! in_array(strtoupper($method), ['GET', 'POST'])) {
abort(503, 'Supplied method must match GET or POST');
throw InvalidFactomApiConfig::invalidMethodCalled();
}

$options = [
Expand Down Expand Up @@ -164,9 +164,9 @@ public function callEndpoint(string $actionName, string $method, array $binaryDa
}

if ($error) {
abort(503, 'Received error "'.$error.'" when hitting "'.$actionName.'" within the Factom API');
throw InvalidFactomApiConfig::invalidApiResponse($error, $actionName);
} elseif (! $result) {
abort(503, 'Received an empty response when hitting "'.$actionName.'" within the Factom API');
throw InvalidFactomApiConfig::emptyApiResponse($actionName);
}

$response = json_decode($result);
Expand All @@ -181,19 +181,4 @@ public function callEndpoint(string $actionName, string $method, array $binaryDa

// return Result
}

/**
* Returns various heights that allows you to view the state of the blockchain.
*
* @url https://docs.factom.com/api#heights
*
* @return json { ... }
*/
public function heights()
{
$result = $this->callEndpoint('heights', 'POST');

// return Result
return $result;
}
}
14 changes: 14 additions & 0 deletions tests/FactomApi/FactomApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace AdrianMejias\FactomApi\Test;

use PHPUnit\Framework\TestCase;

class FactomApiTest extends TestCase
{
/* @test */
// public function it_can_get_heights()
// {
// $this->factomApi->heights();
// }
}
24 changes: 24 additions & 0 deletions tests/FactomApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,37 @@

namespace AdrianMejias\FactomApi\Test;

use Mockery;
use PHPUnit\Framework\TestCase;
use AdrianMejias\FactomApi\FactomApi;

class FactomApiTest extends TestCase
{
/** @var Mockery\Mock */
protected $factomApi;

public function setUp()
{
$this->factomApi = Mockery::mock(FactomApi::class);
$this->factomApi->shouldReceive('success')->andReturn(true);
}

public function tearDown()
{
parent::tearDown();

if ($container = Mockery::getContainer()) {
$this->addToAssertionCount($container->mockery_getExpectationCount());
}

Mockery::close();
}

/* @test */
// public function it_can_get_heights()
// {
// $this->factomApi->shouldReceive('post');

// $this->factomApi->heights();
// }
}

0 comments on commit 7444a60

Please sign in to comment.