Skip to content

Commit

Permalink
Merge pull request #1 from cassell/guzzle6
Browse files Browse the repository at this point in the history
Guzzle6
  • Loading branch information
hthabet authored Feb 10, 2020
2 parents c356ef7 + f70fdfe commit eb83961
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/vendor
.idea
composer.lock
tests
/phpunit.xml
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"description": "PHP SDK for Tremendous API",
"type": "library",
"require": {
"php": "^7.0",
"guzzlehttp/guzzle": "^5.0",
"symfony/http-foundation": "2.8.*|3.0.*"
"php": "^7.1",
"guzzlehttp/guzzle": "^6.0",
"symfony/http-foundation": "2.8.*|3.0.*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
"phpunit/phpunit": "^6.0",
"fzaninotto/faker": "^1.9"
},
"autoload": {
"psr-4": {
Expand Down
26 changes: 26 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Tremendous Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="TREMENDOUS_ENDPOINT" value="https://testflight.tremendous.com/api/v2/"/>
<env name="TREMENDOUS_API_TOKEN" value=""/>
<env name="TREMENDOUS_FUNDING_SOURCE" value=""/>
<env name="TREMENDOUS_TEST_CAMPAIGN_IDENTIFIER" value="" />
</php>
</phpunit>
83 changes: 0 additions & 83 deletions src/Client.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/Exception/CreateOrderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class CreateOrderException extends Exception
* CreateOrderException constructor.
*
* @param string $errorMessage
* @param Exception|null $previous
*/
public function __construct(string $errorMessage)
public function __construct(string $errorMessage, $previous = null)
{
parent::__construct($errorMessage);
parent::__construct($errorMessage, 0, $previous);
}
}
70 changes: 0 additions & 70 deletions src/Response/CreateOrderResponse.php

This file was deleted.

55 changes: 44 additions & 11 deletions src/Tremendous.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,58 @@

namespace Tremendous;

use GuzzleHttp\ClientInterface;
use Tremendous\Exception\CreateOrderException;
use Tremendous\Request\CreateOrderRequest;
use Tremendous\Response\CreateOrderResponse;

class Tremendous extends Client
class Tremendous
{
public function createOrder(CreateOrderRequest $orderRequest) {
/**
* @var ClientInterface Http Client
*/
protected $httpClient;

$uri = "orders";
/**
* Client constructor.
*
* @param ClientInterface $httpClient
*
*/
public function __construct(ClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}

$request = $this->httpClient->createRequest('POST', $uri);
$response = new CreateOrderResponse(
$this->sendRequest($request, $orderRequest->getPayload())
);
protected function decode(string $string): array
{
$array = \json_decode($string, true);

if (!$response->getClientResponse()->isSuccessful()) {
throw new CreateOrderException($response->getErrorMessage());
if (json_last_error() !== JSON_ERROR_NONE || !is_array($array)) {
throw new \InvalidArgumentException('Invalid json data');
}

return Order::fromCreateArray($response->getOrder());
return $array;
}

public function createOrder(CreateOrderRequest $orderRequest)
{
$uri = "orders";

try {

$request = $this->httpClient->request('POST', $uri, [
'body' => json_encode($orderRequest->getPayload()),
'headers' => [
'Content-Type' => 'application/json'
]
]);

$data = $this->decode((string)$request->getBody());

return Order::fromCreateArray($data['order']);

} catch (\GuzzleHttp\Exception\ClientException $exception) {
throw new CreateOrderException($exception->getMessage(), $exception);
}
}
}
107 changes: 107 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);

use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;
use Tremendous\Request\CreateOrderRequest;
use Tremendous\Tremendous;

class ClientTest extends TestCase
{
/**
* @throws \Tremendous\Exception\CreateOrderException
* @expectedException \Tremendous\Exception\CreateOrderException
* @expectedExceptionMessage Invalid Access token
*/
public function testFailure()
{
$faker = \Faker\Factory::create();

$fundingSource = (string)getenv('TREMENDOUS_FUNDING_SOURCE');

$reward = [
'campaign_id' => getenv('TREMENDOUS_TEST_CAMPAIGN_IDENTIFIER'),
'value' => [
'denomination' => 5.00,
'currency_code' => 'USD'
],
'recipient' => [
'name' => $faker->name(),
'email' => $faker->safeEmail()
],
'delivery' => [
'method' => 'LINK'
],
];

$rewardExternalId = \Faker\Provider\Uuid::uuid();

$invalidToken = 'invalid83924ded57c64add9881e70e35f92a46efbccdd1067a4830b6d6d89b4';

$config = [
'base_uri' => (string)getenv('TREMENDOUS_ENDPOINT'),
'headers' => [
'Authorization' => 'Bearer ' . $invalidToken,
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
];

$httpClient = new Client($config);

$tremendousApi = new Tremendous($httpClient);

$orderRequest = new CreateOrderRequest($fundingSource, $reward, $rewardExternalId);

$tremendousApi->createOrder($orderRequest);
}

public function testLink()
{
$faker = \Faker\Factory::create();

$fundingSource = (string)getenv('TREMENDOUS_FUNDING_SOURCE');

$reward = [
'campaign_id' => getenv('TREMENDOUS_TEST_CAMPAIGN_IDENTIFIER'),
'value' => [
'denomination' => 5.00,
'currency_code' => 'USD'
],
'recipient' => [
'name' => $faker->name(),
'email' => $faker->safeEmail()
],
'delivery' => [
'method' => 'LINK'
],
];

$rewardExternalId = \Faker\Provider\Uuid::uuid();

$config = [
'base_uri' => (string)getenv('TREMENDOUS_ENDPOINT'),
'headers' => [
'Authorization' => 'Bearer ' . (string)getenv('TREMENDOUS_API_TOKEN'),
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
];


$httpClient = new Client($config);

$tremendousApi = new Tremendous($httpClient);

$orderRequest = new CreateOrderRequest($fundingSource, $reward, $rewardExternalId);

$order = $tremendousApi->createOrder($orderRequest);

$this->assertEquals(5.00, $order->getRewards()[0]->getValue()->getDenomination());
$this->assertEquals('USD', $order->getRewards()[0]->getValue()->getCurrencyCode());
$this->assertEquals('SUCCEEDED', $order->getRewards()[0]->getDelivery()->getStatus());

}


}

0 comments on commit eb83961

Please sign in to comment.