Skip to content

Commit

Permalink
bug #110 Inject sylius.http_client rather than http_client to Generic…
Browse files Browse the repository at this point in the history
…Api (Zales0123)

This PR was merged into the 1.0-dev branch.

Discussion
----------



Commits
-------

190b2b3 Inject sylius.http_client rather than http_client to GenericApi
  • Loading branch information
Zales0123 authored Sep 22, 2020
2 parents b85703a + 190b2b3 commit 29a0592
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
52 changes: 52 additions & 0 deletions spec/Api/GenericApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\PayPalPlugin\Api;

use GuzzleHttp\ClientInterface;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Sylius\PayPalPlugin\Api\GenericApiInterface;

final class GenericApiSpec extends ObjectBehavior
{
function let(ClientInterface $client): void
{
$this->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']);
}
}
12 changes: 6 additions & 6 deletions src/Api/GenericApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/services/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
id="Sylius\PayPalPlugin\Api\GenericApiInterface"
class="Sylius\PayPalPlugin\Api\GenericApi"
>
<argument type="service" id="http_client" />
<argument type="service" id="sylius.http_client" />
</service>

<service
Expand Down

0 comments on commit 29a0592

Please sign in to comment.