diff --git a/composer.json b/composer.json index f549972..c780b64 100644 --- a/composer.json +++ b/composer.json @@ -31,14 +31,14 @@ "php-http/discovery": "^1.6", "psr/http-factory": "^1.0.2", "php-http/client-implementation": "^1.0", - "psr/simple-cache": "^1.0 || ^2.0 || ^3.0", - "symfony/http-client": "^5.4", - "php-http/message-factory": "^1.1" + "psr/simple-cache": "^1.0 || ^2.0 || ^3.0" }, "require-dev": { "phpunit/phpunit": "^7 || ^8 || ^9.4", "php-http/message": "^1.7", "php-http/mock-client": "^1.0", + "php-http/message-factory": "^1.1", + "symfony/http-client": "^5.4 || ^6.4 || ^7.0", "nyholm/psr7": "^1.0" }, "scripts": { diff --git a/src/Service/HttpService.php b/src/Service/HttpService.php index c24daf0..a423ed9 100644 --- a/src/Service/HttpService.php +++ b/src/Service/HttpService.php @@ -14,8 +14,10 @@ namespace Exchanger\Service; use Http\Client\HttpClient; +use Http\Discovery\Exception\NotFoundException; use Http\Discovery\HttpClientDiscovery; use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; @@ -50,7 +52,11 @@ abstract class HttpService extends Service public function __construct($httpClient = null, RequestFactoryInterface $requestFactory = null, array $options = []) { if (null === $httpClient) { - $httpClient = HttpClientDiscovery::find(); + try { + $httpClient = Psr18ClientDiscovery::find(); + } catch (NotFoundException $e) { + $httpClient = HttpClientDiscovery::find(); + } } else { if (!$httpClient instanceof ClientInterface && !$httpClient instanceof HttpClient) { throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface'); diff --git a/tests/Tests/Service/CentralBankOfCzechRepublicTest.php b/tests/Tests/Service/CentralBankOfCzechRepublicTest.php index 87bdea9..4021a09 100644 --- a/tests/Tests/Service/CentralBankOfCzechRepublicTest.php +++ b/tests/Tests/Service/CentralBankOfCzechRepublicTest.php @@ -111,7 +111,7 @@ public function it_fetches_idr_rate() $pair = CurrencyPair::createFromString('IDR/CZK'); $rate = $this->createService()->getExchangeRate(new ExchangeRateQuery($pair)); - $this->assertSame(0.001798, $rate->getValue()); + $this->assertSame(0.001798, (float)\number_format($rate->getValue(), 6)); $this->assertEquals('central_bank_of_czech_republic', $rate->getProviderName()); $this->assertSame($pair, $rate->getCurrencyPair()); } diff --git a/tests/Tests/Service/CurrencyLayerTest.php b/tests/Tests/Service/CurrencyLayerTest.php index 4a442b3..0d4b459 100644 --- a/tests/Tests/Service/CurrencyLayerTest.php +++ b/tests/Tests/Service/CurrencyLayerTest.php @@ -106,7 +106,7 @@ public function it_fetches_a_historical_rate_normal_mode() $content = file_get_contents(__DIR__.'/../../Fixtures/Service/CurrencyLayer/historical_success.json'); $date = new \DateTime('2015-05-06'); $expectedDate = new \DateTime(); - $expectedDate->setTimestamp(1430870399); + $expectedDate->setTimestamp(1430784000); $service = new CurrencyLayer($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'secret']); $rate = $service->getExchangeRate(new HistoricalExchangeRateQuery($pair, $date)); @@ -126,7 +126,7 @@ public function it_fetches_a_historical_rate_enterprise_mode() $content = file_get_contents(__DIR__.'/../../Fixtures/Service/CurrencyLayer/historical_success.json'); $date = new \DateTime('2015-05-06'); $expectedDate = new \DateTime(); - $expectedDate->setTimestamp(1430870399); + $expectedDate->setTimestamp(1430784000); $pair = CurrencyPair::createFromString('USD/AED'); $service = new CurrencyLayer($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'secret', 'enterprise' => true]); diff --git a/tests/Tests/Service/HttpServiceTest.php b/tests/Tests/Service/HttpServiceTest.php index b557756..6a693f2 100644 --- a/tests/Tests/Service/HttpServiceTest.php +++ b/tests/Tests/Service/HttpServiceTest.php @@ -50,8 +50,8 @@ public function initialize_with_httplug_client() */ public function initialize_with_null_as_client() { - $this->expectException(\Http\Discovery\Exception\NotFoundException::class); - $this->expectExceptionMessage('No HTTPlug clients found. Make sure to install a package providing "php-http/client-implementation"'); + $this->expectNotToPerformAssertions(); + // if null is passed a new instance HttpClient is generated in the HttpService. $this->createAnonymousClass(null); }