Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Httplug #26

Merged
merged 10 commits into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.4
- 5.5
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep PHP 5.4 support ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some dependencies (ie discovery package) requires php 5.5

On 18 aug. 2016, at 23:03, Florian Voutzinos notifications@github.com wrote:

In .travis.yml #26 (comment):

@@ -1,7 +1,6 @@
language: php

php:

    • 5.4
      Can we keep PHP 5.4 support ?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/florianv/swap/pull/26/files/80a48d67ee0057b66788b02e15f8a26114046c52#r75388823, or mute the thread https://github.com/notifications/unsubscribe-auth/ABN1Ri8THG72YngvqxEwpUcjnqax3oA_ks5qhMiegaJpZM4HqSa4.

- 5.6
- 7.0
Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
}
},
"require": {
"php": ">=5.4.0",
"egeloen/http-adapter": "~1.0"
"php": "^5.5|^7.0",
"php-http/httplug": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"php-http/guzzle6-adapter": "^1.0",
"php-http/message": "^1.0",
"doctrine/cache": "~1.0",
"illuminate/cache": "~5.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Model/CurrencyPair.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function __construct($baseCurrency, $quoteCurrency)
*
* @param string $string A string in the form EUR/USD
*
* @return CurrencyPair
*
* @throws \InvalidArgumentException
*
* @return CurrencyPair
*/
public static function createFromString($string)
{
Expand Down
28 changes: 23 additions & 5 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Swap\Provider;

use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\RequestFactory;
use Swap\ProviderInterface;

/**
Expand All @@ -21,11 +24,24 @@
*/
abstract class AbstractProvider implements ProviderInterface
{
protected $httpAdapter;
/**
* @var HttpClient
*/
private $httpClient;

/**
* @var RequestFactory
*/
private $requestFactory;

public function __construct(HttpAdapterInterface $httpAdapter)
/**
* @param HttpClient|null $httpClient
* @param RequestFactory|null $requestFactory
*/
public function __construct(HttpClient $httpClient = null, RequestFactory $requestFactory = null)
{
$this->httpAdapter = $httpAdapter;
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
}

/**
Expand All @@ -37,6 +53,8 @@ public function __construct(HttpAdapterInterface $httpAdapter)
*/
protected function fetchContent($url)
{
return (string) $this->httpAdapter->get($url)->getBody();
$request = $this->requestFactory->createRequest('GET', $url);

return $this->httpClient->sendRequest($request)->getBody()->__toString();
}
}
20 changes: 13 additions & 7 deletions src/Provider/CurrencyLayerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Swap\Provider;

use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Swap\Exception\Exception;
use Swap\Exception\UnsupportedCurrencyPairException;
use Swap\Model\CurrencyPair;
Expand All @@ -34,13 +35,18 @@ class CurrencyLayerProvider extends AbstractProvider
/**
* Creates a new provider.
*
* @param HttpAdapterInterface $httpAdapter The HTTP client
* @param string $accessKey The access key
* @param bool $enterprise A flag to tell if it is in enterprise mode
* @param string $accessKey The access key
* @param bool $enterprise A flag to tell if it is in enterprise mode
* @param HttpClient $httpClient
* @param RequestFactory $requestFactory
*/
public function __construct(HttpAdapterInterface $httpAdapter, $accessKey, $enterprise = false)
{
parent::__construct($httpAdapter);
public function __construct(
$accessKey,
$enterprise = false,
HttpClient $httpClient = null,
RequestFactory $requestFactory = null
) {
parent::__construct($httpClient, $requestFactory);

$this->accessKey = $accessKey;
$this->enterprise = $enterprise;
Expand Down
20 changes: 13 additions & 7 deletions src/Provider/OpenExchangeRatesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Swap\Provider;

use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Swap\Exception\Exception;
use Swap\Exception\UnsupportedCurrencyPairException;
use Swap\Model\CurrencyPair;
Expand All @@ -34,13 +35,18 @@ class OpenExchangeRatesProvider extends AbstractProvider
/**
* Creates a new provider.
*
* @param HttpAdapterInterface $httpAdapter The HTTP client
* @param string $appId The application id
* @param bool $enterprise A flag to tell if it is in enterprise mode
* @param string $appId The application id
* @param bool $enterprise A flag to tell if it is in enterprise mode
* @param HttpClient $httpClient
* @param RequestFactory $requestFactory
*/
public function __construct(HttpAdapterInterface $httpAdapter, $appId, $enterprise = false)
{
parent::__construct($httpAdapter);
public function __construct(
$appId,
$enterprise = false,
HttpClient $httpClient = null,
RequestFactory $requestFactory = null
) {
parent::__construct($httpClient, $requestFactory);

$this->appId = $appId;
$this->enterprise = $enterprise;
Expand Down
17 changes: 12 additions & 5 deletions src/Provider/XigniteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Swap\Provider;

use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Swap\Exception\Exception;
use Swap\Model\CurrencyPair;
use Swap\Model\Rate;
Expand All @@ -26,15 +27,21 @@ class XigniteProvider extends AbstractProvider
{
const URL = 'https://globalcurrencies.xignite.com/xGlobalCurrencies.json/GetRealTimeRates?Symbols=%s&_fields=Outcome,Message,Symbol,Date,Time,Bid&_Token=%s';

/**
* @var string
*/
private $token;

/**
* Creates a new provider.
*
* @param HttpAdapterInterface $httpAdapter The HTTP adapter
* @param string $token The application token
* @param string $token The application token
* @param HttpClient $httpClient
* @param RequestFactory $requestFactory
*/
public function __construct(HttpAdapterInterface $httpAdapter, $token)
public function __construct($token, HttpClient $httpClient = null, RequestFactory $requestFactory = null)
{
parent::__construct($httpAdapter);
parent::__construct($httpClient, $requestFactory);
$this->token = $token;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Util/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ final class StringUtil
*
* @param string $string
*
* @return \SimpleXMLElement
*
* @throws \RuntimeException
*
* @return \SimpleXMLElement
*/
public static function xmlToElement($string)
{
Expand Down Expand Up @@ -53,9 +53,9 @@ public static function xmlToElement($string)
*
* @param string $string
*
* @return array
*
* @throws \RuntimeException
*
* @return array
*/
public static function jsonToArray($string)
{
Expand Down
9 changes: 3 additions & 6 deletions tests/Tests/Cache/DoctrineCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function it_returns_null_if_rate_absent()
->expects($this->once())
->method('fetch')
->with($pair)
->will($this->returnValue(false))
;
->will($this->returnValue(false));

$doctrineCache = new DoctrineCache($cache);
$this->assertNull($doctrineCache->fetchRate($pair));
Expand All @@ -49,8 +48,7 @@ public function it_fetches_a_rate()
->expects($this->once())
->method('fetch')
->with($pair)
->will($this->returnValue($rate))
;
->will($this->returnValue($rate));

$doctrineCache = new DoctrineCache($cache);
$this->assertSame($rate, $doctrineCache->fetchRate($pair));
Expand All @@ -69,8 +67,7 @@ public function it_stores_a_rate()
->expects($this->once())
->method('save')
->with('EUR/USD', $rate, 3600)
->will($this->returnValue($rate))
;
->will($this->returnValue($rate));

$doctrineCache = new DoctrineCache($cache, 3600);
$doctrineCache->storeRate($pair, $rate);
Expand Down
9 changes: 3 additions & 6 deletions tests/Tests/Cache/IlluminateCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function it_returns_null_if_rate_absent()
->expects($this->once())
->method('get')
->with($pair)
->will($this->returnValue(null))
;
->will($this->returnValue(null));

$cache = new IlluminateCache($store);
$this->assertNull($cache->fetchRate($pair));
Expand All @@ -49,8 +48,7 @@ public function it_fetches_a_rate()
->expects($this->once())
->method('get')
->with($pair)
->will($this->returnValue($rate))
;
->will($this->returnValue($rate));

$cache = new IlluminateCache($store);
$this->assertSame($rate, $cache->fetchRate($pair));
Expand All @@ -68,8 +66,7 @@ public function it_stores_a_rate()
$store
->expects($this->once())
->method('put')
->with('EUR/USD', $rate, 60)
;
->with('EUR/USD', $rate, 60);

$cache = new IlluminateCache($store, 60);
$cache->storeRate($pair, $rate);
Expand Down
12 changes: 7 additions & 5 deletions tests/Tests/Provider/AbstractProviderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class AbstractProviderTestCase extends \PHPUnit_Framework_TestCase
* @param string $url The url
* @param string $content The body content
*
* @return \Ivory\HttpAdapter\HttpAdapterInterface
* @return \Http\Client\HttpClient
*/
protected function getHttpAdapterMock($url, $content)
{
Expand All @@ -29,18 +29,20 @@ protected function getHttpAdapterMock($url, $content)
->method('__toString')
->will($this->returnValue($content));

$response = $this->getMock('\Ivory\HttpAdapter\Message\ResponseInterface');
$response = $this->getMock('Psr\Http\Message\ResponseInterface');
$response
->expects($this->once())
->method('getBody')
->will($this->returnValue($body));

$adapter = $this->getMock('Ivory\HttpAdapter\HttpAdapterInterface');
$adapter = $this->getMock('Http\Client\HttpClient');

$adapter
->expects($this->once())
->method('get')
->with($url)
->method('sendRequest')
->with($this->callback(function ($arg) use ($url) {
return $arg->getUri()->__toString() === $url;
}))
->will($this->returnValue($response));

return $adapter;
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/Provider/ArrayProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function it_throws_an_exception_when_rate_not_supported()
public function it_throws_an_exception_when_fetching_invalid_rate()
{
$arrayProvider = new ArrayProvider([
'EUR/USD' => array(),
'EUR/USD' => [],
]);

$arrayProvider->fetchRate(CurrencyPair::createFromString('EUR/USD'));
Expand Down
21 changes: 7 additions & 14 deletions tests/Tests/Provider/ChainProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,21 @@ public function it_use_next_provider_in_the_chain()
->expects($this->once())
->method('fetchRate')
->with($pair)
->will($this->throwException(new Exception()))
;
->will($this->throwException(new Exception()));

$providerTwo = $this->getMock('Swap\ProviderInterface');

$providerTwo
->expects($this->once())
->method('fetchRate')
->with($pair)
->will($this->returnValue($rate))
;
->will($this->returnValue($rate));

$providerThree = $this->getMock('Swap\ProviderInterface');

$providerThree
->expects($this->never())
->method('fetchRate')
;
->method('fetchRate');

$chain = new ChainProvider([$providerOne, $providerTwo, $providerThree]);
$fetchedRate = $chain->fetchRate($pair);
Expand All @@ -70,16 +67,14 @@ public function it_throws_an_exception_when_all_providers_fail()
$providerOne
->expects($this->once())
->method('fetchRate')
->will($this->throwException($exception))
;
->will($this->throwException($exception));

$providerTwo = $this->getMock('Swap\ProviderInterface');

$providerTwo
->expects($this->once())
->method('fetchRate')
->will($this->throwException($exception))
;
->will($this->throwException($exception));

$chain = new ChainProvider([$providerOne, $providerTwo]);
$caught = false;
Expand All @@ -106,15 +101,13 @@ public function it_throws_an_exception_when_an_internal_exception_is_thrown()
$providerOne
->expects($this->once())
->method('fetchRate')
->will($this->throwException($internalException))
;
->will($this->throwException($internalException));

$providerTwo = $this->getMock('Swap\ProviderInterface');

$providerTwo
->expects($this->never())
->method('fetchRate')
;
->method('fetchRate');

$chain = new ChainProvider([$providerOne, $providerTwo]);
$chain->fetchRate(new CurrencyPair('EUR', 'USD'));
Expand Down
Loading