From 190b2b3b611912008094cb978ed6417714073824 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Tue, 22 Sep 2020 17:05:48 +0200 Subject: [PATCH] Inject sylius.http_client rather than http_client to GenericApi --- spec/Api/GenericApiSpec.php | 52 +++++++++++++++++++++++++++ src/Api/GenericApi.php | 12 +++---- src/Resources/config/services/api.xml | 2 +- 3 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 spec/Api/GenericApiSpec.php diff --git a/spec/Api/GenericApiSpec.php b/spec/Api/GenericApiSpec.php new file mode 100644 index 00000000..e4002914 --- /dev/null +++ b/spec/Api/GenericApiSpec.php @@ -0,0 +1,52 @@ +beConstructedWith($client); + } + + function it_implements_generic_api_interface(): void + { + $this->shouldImplement(GenericApiInterface::class); + } + + function it_calls_api_by_url( + ClientInterface $client, + ResponseInterface $response, + StreamInterface $body + ): void { + $client->request('GET', 'http://url.com/', [ + 'headers' => [ + 'Authorization' => 'Bearer TOKEN', + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + ], + ])->willReturn($response); + + $response->getBody()->willReturn($body); + $body->getContents()->willReturn('{ "parameter": "VALUE" }'); + + $this->get('TOKEN', 'http://url.com/')->shouldReturn(['parameter' => 'VALUE']); + } +} diff --git a/src/Api/GenericApi.php b/src/Api/GenericApi.php index 149cb10a..ec93e135 100644 --- a/src/Api/GenericApi.php +++ b/src/Api/GenericApi.php @@ -4,28 +4,28 @@ namespace Sylius\PayPalPlugin\Api; -use Symfony\Contracts\HttpClient\HttpClientInterface; +use GuzzleHttp\ClientInterface; final class GenericApi implements GenericApiInterface { - /** @var HttpClientInterface */ + /** @var ClientInterface */ private $client; - public function __construct(HttpClientInterface $client) + public function __construct(ClientInterface $client) { $this->client = $client; } public function get(string $token, string $url): array { - $options = [ + $response = $this->client->request('GET', $url, [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json', ], - ]; + ]); - return $this->client->request('GET', $url, $options)->toArray(); + return (array) json_decode($response->getBody()->getContents(), true); } } diff --git a/src/Resources/config/services/api.xml b/src/Resources/config/services/api.xml index 90b263b9..6e85f0d6 100644 --- a/src/Resources/config/services/api.xml +++ b/src/Resources/config/services/api.xml @@ -54,7 +54,7 @@ id="Sylius\PayPalPlugin\Api\GenericApiInterface" class="Sylius\PayPalPlugin\Api\GenericApi" > - +