Skip to content

Commit

Permalink
Merge pull request #37 from Judopay/Guzzle6-JR-3044
Browse files Browse the repository at this point in the history
Guzzle6-JR-3044
  • Loading branch information
chrisurun authored Jun 20, 2019
2 parents 5d87f83 + ddc6cc2 commit 266c93c
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 175 deletions.
19 changes: 14 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
script: ./code_check.sh
install:
- composer install
- 7.3

dist: xenial

install: composer install

before_script:
- php -r "printf('PHP %s', phpversion());"
- composer self-update
- composer install --no-interaction

script: ./code_check.sh
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
"repositories": [
],
"require": {
"php": ">=5.4",
"guzzlehttp/guzzle": "~5.0",
"php": ">=5.5",
"guzzlehttp/guzzle": "~6.0",
"monolog/monolog": "~1.10",
"pimple/pimple": "~3.0",
"curl/curl" : ">=0.0.0",
"ext-curl": ">=0.0.0",
"ext-openssl": ">=0.0.0"
"ext-openssl": ">=0.0.0",
"ext-json": "*",
"ext-curl": "*"
},
"require-dev": {
"phpspec/phpspec": "^2.0",
Expand Down
2 changes: 1 addition & 1 deletion spec/Judopay/ConfigurationSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function it_sets_the_correct_endpoint_url()
$this->beConstructedWith(array('useProduction' => true));

/** @var Configuration|ConfigurationSpec $this */
$this->get('endpointUrl')->shouldBe('https://gw1.judopay.com');
$this->get('endpointUrl')->shouldBe('https://gw1.judopay.com/');
}

public function it_should_allow_a_custom_api_version_to_be_set()
Expand Down
1 change: 0 additions & 1 deletion spec/Judopay/Model/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace spec\Judopay\Model;

use Judopay\Model\Collection;
use Tests\Builders\CardPaymentBuilder;
use Tests\Builders\GetTransactionBuilder;
use Tests\Builders\RefundBuilder;

Expand Down
2 changes: 1 addition & 1 deletion spec/Judopay/Model/ModelObjectBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function concoctRequest($fixtureFile)
{
$request = new Request($this->configuration);

$mockClient = SpecHelper::getMockResponseClient(
$mockClient = SpecHelper::getMockResponseClientFromFixture(
200,
$fixtureFile
);
Expand Down
1 change: 0 additions & 1 deletion spec/Judopay/ModelSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Judopay\Request;
use spec\SpecHelper;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ModelSpec extends ObjectBehavior
{
Expand Down
14 changes: 8 additions & 6 deletions spec/Judopay/RequestSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace spec\Judopay;

use GuzzleHttp\Message\Request;
use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\Assert;
use spec\SpecHelper;
use PhpSpec\ObjectBehavior;
Expand All @@ -25,14 +25,16 @@ public function it_sends_the_right_authorization_header_for_oauth2()

// Create a request
$this->beConstructedWith($config);
$request = new Request('get', 'http://example.com');

/** @var \Judopay\Request $this */
// Add the auth information to the request
$requestWithAuth = $this->setRequestAuthentication($request);
$headers = $this->getHeaders();
$request = new Request(
'get',
'http://example.com',
$headers->getWrappedObject()
);

// Make sure the Authorization header is correct
Assert::assertEquals('Bearer '.$oauthAccessToken, $request->getHeader('Authorization'));
Assert::assertEquals('Bearer '.$oauthAccessToken, $request->getHeader('Authorization')[0]);
}

public function getMatchers()
Expand Down
1 change: 0 additions & 1 deletion spec/JudopaySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace spec;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class JudopaySpec extends ObjectBehavior
{
Expand Down
137 changes: 91 additions & 46 deletions spec/SpecHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,122 @@

namespace spec;

use GuzzleHttp\Client;
use GuzzleHttp\Event\EmitterInterface;
use GuzzleHttp\Event\SubscriberInterface;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Subscriber\Mock;
use Judopay\Configuration;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Subscriber\History;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7;

/*
* Test the client without sending the requests
*/
class SpecHelper
{
public static function getMockResponse($responseCode, $responseBody = null)
/**
* Returns a mock configuration
*/
public static function getConfiguration()
{
$mockResponse = new Response($responseCode, null, $responseBody);
$configuration = new Configuration(
array(
'apiToken' => 'token',
'apiSecret' => 'secret',
'judoId' => '123-456',
)
);

return $mockResponse;
return $configuration;
}

public static function getMockResponseFromFixture($responseCode, $fixtureFile = null)
/**
* Creates a mock Handler with a response in the queue
* @param $mockResponse
* @return HandlerStack
*/
public static function getMockResponseHandler($mockResponse)
{
$fixtureContent = null;
if (!empty($fixtureFile)) {
$fixtureContent = file_get_contents(__DIR__.'/fixtures/'.$fixtureFile);
}
$mock = new MockHandler([$mockResponse]);
$handler = HandlerStack::create($mock);

return $handler;
}

/**
* Creates a mock client with the provided body
* @param $responseCode
* @param $responseBody
* @return Client
*/
public static function getMockResponseClientFromBody($responseCode, $responseBody)
{
// Create the response
$mockResponse = SpecHelper::getBodyResponse($responseCode, $responseBody);

// Create the handler with a response
$handlerStack = SpecHelper::getMockResponseHandler($mockResponse);

$container = [];
$history = Middleware::history($container);

// Add the history middleware to the handler stack.
$handlerStack->push($history);

return new Client(['handler' => $handlerStack]);
}

$stream = Stream::factory($fixtureContent);

public static function getBodyResponse($responseCode, $responseBody = null)
{
$mockResponse = new Response(
$responseCode, // statusCode
[], // headers
$stream // StreamInterface body
$responseCode,
$responseBody
);

return $mockResponse;
}

public static function getMockResponseClient($responseCode, $fixtureFile)
/**
* Creates a mock client with the provided fixture
* @param $responseCode
* @param $fixtureFile
* @return Client
*/
public static function getMockResponseClientFromFixture($responseCode, $fixtureFile)
{
$client = new Client();
// Create the response
$mockResponse = SpecHelper::getFixtureResponse($responseCode, $fixtureFile);

$history = new History();
$client->getEmitter()->attach($history);
// Create the handler with a response
$handlerStack = SpecHelper::getMockResponseHandler($mockResponse);

$mock = new Mock();
$container = [];
$history = Middleware::history($container);

// Preparing the response for the client
/* @var $mockResponse ResponseInterface */
$mockResponse = SpecHelper::getMockResponseFromFixture($responseCode, $fixtureFile);
$mock->addResponse($mockResponse);
// Add the history middleware to the handler stack.
$handlerStack->push($history);

// Guzzle event emitter (onBefore)
/** @var EmitterInterface $emitter*/
$emitter = $client->getEmitter();

// Attach the mock to the emitter
$emitter->attach($mock);

return $client;
return new Client(['handler' => $handlerStack]);
}

public static function getConfiguration()
public static function getFixtureResponse($responseCode, $fixtureFile = null)
{
$configuration = new Configuration(
array(
'apiToken' => 'token',
'apiSecret' => 'secret',
'judoId' => '123-456',
)
$fixtureContent = null;
if (!empty($fixtureFile)) {
$fixtureContent = file_get_contents(__DIR__.'/fixtures/'.$fixtureFile);
}

$stream = Psr7\stream_for($fixtureContent);

$mockResponse = new Response(
$responseCode, // statusCode
[], // headers
$stream // StreamInterface body
);

return $configuration;
return $mockResponse;
}
}
17 changes: 13 additions & 4 deletions src/Judopay.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use Judopay\Configuration;
use Pimple\Container;
use Judopay\Client;
use Judopay\Request;

/**
* Main Judopay wrapper
Expand All @@ -10,8 +12,8 @@
*/
class Judopay
{
const SDK_VERSION = '2.2.0';
const API_VERSION = '5.5.0';
const SDK_VERSION = '4.0.0';
const API_VERSION = '5.5.1';
/**
* Pimple DI container
* @var object Pimple\Container
Expand All @@ -36,8 +38,15 @@ public function __construct($settings = null)
function ($c) {
/** @var Configuration $configuration */
$configuration = $c['configuration'];
$request = new \Judopay\Request($configuration);
$request->setClient(new \Judopay\Client());
$request = new Request($configuration);

// The client is now immutable and needs all the options on creation
$client = new Client([
'base_uri' => 'http://httpbin.org', // Base URI is used with relative requests
'verify' => __DIR__.'/../cert/digicert_sha256_ca.pem'
]);

$request->setClient($client);

return $request;
}
Expand Down
37 changes: 19 additions & 18 deletions src/Judopay/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@

namespace Judopay;

use Judopay;
use Judopay\Exception\ValidationError;
use Psr\Log\NullLogger;

class Configuration
{
const ENDPOINT_URL = 'endpointUrl';
const USE_PRODUCTION = 'useProduction';

protected $settings = array();
protected $valid_config_keys
= array(
'apiVersion',
'apiToken',
'apiSecret',
'oauthAccessToken',
'format',
self::ENDPOINT_URL,
'userAgent',
'judoId',
self::USE_PRODUCTION,
'logger',
'httpLogFormat',
);
protected $valid_config_keys = array(
'apiVersion',
'apiToken',
'apiSecret',
'oauthAccessToken',
'format',
self::ENDPOINT_URL,
'userAgent',
'judoId',
self::USE_PRODUCTION,
'logger',
'httpLogFormat',
);

public function __construct($settings = null)
{
// Set sensible defaults
$this->settings['apiVersion'] = \Judopay::API_VERSION;
$this->settings['apiVersion'] = Judopay::API_VERSION;
$this->settings['logger'] = new NullLogger();
$this->settings['userAgent'] = 'Judopay PHP v'.phpversion().' SDK v'.\Judopay::SDK_VERSION;
$this->settings['userAgent'] = 'Judopay PHP v'.phpversion().' SDK v'.Judopay::SDK_VERSION;
$this->settings['httpLogFormat'] = "\"{method} {resource} {protocol}/{version}\" ".
"{code} Content-Length: {res_header_Content-Length}\n| Response: {response}";

Expand Down Expand Up @@ -94,8 +95,8 @@ protected function setEndpointUrl()
} else {
$this->settings[static::ENDPOINT_URL] = isset($this->settings[static::USE_PRODUCTION])
&& $this->settings[static::USE_PRODUCTION] === true
? 'https://gw1.judopay.com'
: 'https://gw1.judopay-sandbox.com';
? 'https://gw1.judopay.com/'
: 'https://gw1.judopay-sandbox.com/';
}
}

Expand Down
Loading

0 comments on commit 266c93c

Please sign in to comment.