Skip to content

Commit

Permalink
Fixes after testing 'Enable' method
Browse files Browse the repository at this point in the history
  • Loading branch information
leszczuu committed Mar 27, 2024
1 parent a8265ab commit ec4a744
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
24 changes: 14 additions & 10 deletions spec/Api/GenericApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@

namespace spec\Sylius\PayPalPlugin\Api;

use GuzzleHttp\ClientInterface;
use PhpSpec\ObjectBehavior;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
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
function let(ClientInterface $client, RequestFactoryInterface $requestFactory): void
{
$this->beConstructedWith($client);
$this->beConstructedWith($client, $requestFactory);
}

function it_implements_generic_api_interface(): void
Expand All @@ -33,17 +35,19 @@ function it_implements_generic_api_interface(): void

function it_calls_api_by_url(
ClientInterface $client,
RequestFactoryInterface $requestFactory,
RequestInterface $request,
ResponseInterface $response,
StreamInterface $body
): void {
$client->request('GET', 'http://url.com/', [
'headers' => [
'Authorization' => 'Bearer TOKEN',
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
])->willReturn($response);

$requestFactory->createRequest('GET', 'http://url.com/')->willReturn($request);

$request->withHeader('Authorization', 'Bearer TOKEN')->willReturn($request);
$request->withHeader('Content-Type', 'application/json')->willReturn($request);
$request->withHeader('Accept', 'application/json')->willReturn($request);

$client->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->getContents()->willReturn('{ "parameter": "VALUE" }');

Expand Down
27 changes: 13 additions & 14 deletions src/Api/GenericApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@

namespace Sylius\PayPalPlugin\Api;

use GuzzleHttp\ClientInterface;

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

final class GenericApi implements GenericApiInterface
{
private ClientInterface $client;

public function __construct(ClientInterface $client)
{
$this->client = $client;
public function __construct(
private ClientInterface $client,
private RequestFactoryInterface $requestFactory
){
}

public function get(string $token, string $url): array
{
$response = $this->client->request('GET', $url, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
]);

return (array) json_decode($response->getBody()->getContents(), true);
$request = $this->requestFactory->createRequest('GET', $url)
->withHeader('Authorization', 'Bearer ' . $token)
->withHeader('Content-Type', 'application/json')
->withHeader('Accept', 'application/json');

return (array) json_decode($this->client->sendRequest($request)->getBody()->getContents(), true);
}
}
1 change: 0 additions & 1 deletion src/Client/PayPalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Psr18Client;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\PayPalPlugin\Exception\PayPalApiTimeoutException;
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
id="Sylius\PayPalPlugin\Api\GenericApiInterface"
class="Sylius\PayPalPlugin\Api\GenericApi"
>
<argument type="service" id="sylius.http_client" />
<argument type="service" id="Http\Discovery\Psr18Client" />
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
</service>

<service
Expand Down

0 comments on commit ec4a744

Please sign in to comment.