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

Add support for Guzzle 7 #5

Merged
merged 5 commits into from
Jul 8, 2024
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
16 changes: 13 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ jobs:
tests:
runs-on: ubuntu-latest

name: "Sylius ${{ matrix.sylius }}, PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }}, MySQL ${{ matrix.mysql }}"
name: "PHP ${{ matrix.php }} - Guzzle ${{ matrix.guzzle }}"

strategy:
fail-fast: false
matrix:
php: ["8.0", "8.1", "8.2"]
php: ["8.0", "8.1", "8.2", "8.3"]
guzzle: ["6.5.8", "7.5.2"]

steps:
-
uses: actions/checkout@v2
uses: actions/checkout@v4

-
name: Setup PHP
Expand All @@ -31,6 +32,11 @@ jobs:
php-version: "${{ matrix.php }}"
coverage: none

-
name: Restrict Guzzle version
if: matrix.guzzle != ''
run: composer require "guzzlehttp/guzzle:${{ matrix.guzzle }}" --no-update --no-scripts --no-interaction

-
name: Run Composer install
run: composer install -n
Expand All @@ -39,6 +45,10 @@ jobs:
name: Run CS
run: vendor/bin/phpcs --standard=PSR2 src/

-
name: Run PHPStan
run: vendor/bin/phpstan analyse

-
name: Run PHPSpec
run: vendor/bin/phpspec run
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
"require": {
"ext-ctype": "*",
"ext-json": "*",
"ext-dom": "*",
"ext-simplexml": "*",
"psr/log": "^2.0",
"guzzlehttp/guzzle": "^6.2",
"respect/validation": "^1.1",
"guzzlehttp/guzzle": "^6.5 || ^7.5",
"respect/validation": "^2.2",
"php": "^8.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.7",
"phpspec/phpspec": "^7.3"
"phpspec/phpspec": "^7.3",
"phpstan/phpstan": "^1.11"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 6
paths:
- src
63 changes: 36 additions & 27 deletions spec/Api/ClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,42 @@
namespace spec\Webgriffe\LibQuiPago\Api;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\RequestOptions;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Webgriffe\LibQuiPago\Api\Client;
use Webgriffe\LibQuiPago\Api\EcRequest;
use Webgriffe\LibQuiPago\Api\EcResponse;

class ClientSpec extends ObjectBehavior
{
private $merchantAlias = 'ALIAS123123';
private $macKey = 'KEY123123';
private $user = 'John Doe';

private $transactionCode = '00000123';
private $requestType = EcRequest::REQUEST_TYPE_FIRST_ATTEMPT;
private $operationId = '1213123';
private $originalAmount = 230.78;
private $currency = 'EUR';
private $authCode = '00901';
private $operationAmount = 200;

function it_is_initializable(ClientInterface $client)
private string $merchantAlias = 'ALIAS123123';
private string $macKey = 'KEY123123';
private string $user = 'JohnDoe';
private string $transactionCode = '00000123';
private string $requestType = EcRequest::REQUEST_TYPE_FIRST_ATTEMPT;
private string $operationId = '1213123';
private float $originalAmount = 230.78;
private string $currency = 'EUR';
private string $authCode = '00901';
private int $operationAmount = 200;

public function it_is_initializable(ClientInterface $client): void
{
$this->beConstructedWith($client, $this->merchantAlias, $this->macKey, $this->user);
$this->shouldHaveType('Webgriffe\LibQuiPago\Api\Client');
$this->shouldHaveType(Client::class);
$this->getMerchantAlias()->shouldReturn($this->merchantAlias);
$this->getMacKey()->shouldReturn($this->macKey);
$this->getUser()->shouldReturn($this->user);
}

function it_should_capture_funds(ClientInterface $client, ResponseInterface $response)
public function it_should_capture_funds(ClientInterface $client, ResponseInterface $response): void
{
$response->getBody()->willReturn($this->get_positive_capture_response_body());
$response->getBody()->willReturn($this->getResponseBody($this->getPositiveCaptureResponseXML()));
$client->send(Argument::type(RequestInterface::class), [])->shouldBeCalled()->willReturn($response);

$this->beConstructedWith($client, $this->merchantAlias, $this->macKey, $this->user);
Expand All @@ -49,15 +51,15 @@ function it_should_capture_funds(ClientInterface $client, ResponseInterface $res
$this->currency,
$this->authCode,
$this->operationAmount,
false // $isTest
false,
);
$response->shouldHaveType(EcResponse::class);
$response->isPositive()->shouldReturn(true);
}

function it_should_disable_ssl_verify_when_is_test(ClientInterface $client, ResponseInterface $response)
public function it_should_disable_ssl_verify_when_is_test(ClientInterface $client, ResponseInterface $response): void
{
$response->getBody()->willReturn($this->get_positive_capture_response_body());
$response->getBody()->willReturn($this->getResponseBody($this->getPositiveCaptureResponseXML()));
$client
->send(Argument::type(RequestInterface::class), [RequestOptions::VERIFY => false])
->shouldBeCalled()
Expand All @@ -74,15 +76,15 @@ function it_should_disable_ssl_verify_when_is_test(ClientInterface $client, Resp
$this->currency,
$this->authCode,
$this->operationAmount,
true // $isTest
true,
);
$response->shouldHaveType(EcResponse::class);
$response->isPositive()->shouldReturn(true);
}

function it_should_void_transaction(ClientInterface $client, ResponseInterface $response)
public function it_should_void_transaction(ClientInterface $client, ResponseInterface $response): void
{
$response->getBody()->willReturn($this->get_positive_void_response_body());
$response->getBody()->willReturn($this->getResponseBody($this->getPositiveVoidResponseXML()));
$client->send(Argument::type(RequestInterface::class), [])->shouldBeCalled()->willReturn($response);

$this->beConstructedWith($client, $this->merchantAlias, $this->macKey, $this->user);
Expand All @@ -95,18 +97,25 @@ function it_should_void_transaction(ClientInterface $client, ResponseInterface $
$this->currency,
$this->authCode,
$this->operationAmount,
false // $isTest
false,
);
$response->shouldHaveType(EcResponse::class);
$response->isPositive()->shouldReturn(true);
}

private function get_raw_operation_amount()
private function get_raw_operation_amount(): string
{
return str_pad((string)round($this->operationAmount, 2)*100, 9, '0', STR_PAD_LEFT);
return str_pad((string) round($this->operationAmount, 2)*100, 9, '0', STR_PAD_LEFT);
}

private function get_positive_capture_response_body()
private function getResponseBody(string $body): StreamInterface
{
$resource = fopen('data://text/xml,' . $body, 'rb');

return new Stream($resource);
}

private function getPositiveCaptureResponseXML(): string
{
return <<<XML
<?xml version="1.0" encoding="ISO-8859-15"?>
Expand All @@ -125,7 +134,7 @@ private function get_positive_capture_response_body()
XML;
}

private function get_positive_void_response_body()
private function getPositiveVoidResponseXML(): string
{
return <<<XML
<?xml version="1.0" encoding="ISO-8859-15"?>
Expand Down
30 changes: 12 additions & 18 deletions spec/Api/EcRequestSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
namespace spec\Webgriffe\LibQuiPago\Api;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Webgriffe\LibQuiPago\Api\EcRequest;
use Webgriffe\LibQuiPago\Api\ValidationException;

class EcRequestSpec extends ObjectBehavior
{
function it_is_initializable_as_capture_request()
public function it_is_initializable_as_capture_request(): void
{
$this->beConstructedThrough('createCaptureRequest', $this->get_valid_capture_request_data());
$this->beConstructedThrough('createCaptureRequest', $this->getValidCaptureRequestData());
$this->shouldHaveType(EcRequest::class);
$this->getUrl()->shouldReturn('https://ecommerce.nexi.it/ecomm/ecomm/XPayBo');

Expand All @@ -38,9 +37,9 @@ function it_is_initializable_as_capture_request()
$this->getBody()->shouldReturn($body);
}

function it_should_return_itself_as_psr_request()
public function it_should_return_itself_as_psr_request(): void
{
$this->beConstructedThrough('createCaptureRequest', $this->get_valid_capture_request_data());
$this->beConstructedThrough('createCaptureRequest', $this->getValidCaptureRequestData());
$this->shouldHaveType(EcRequest::class);
/** @var RequestInterface $psrRequest */
$psrRequest = $this->asPsrRequest();
Expand All @@ -50,18 +49,19 @@ function it_should_return_itself_as_psr_request()
$psrRequest->getUri()->__toString()->shouldReturn($this->getUrl());
}

function it_should_throw_an_exception_if_data_are_not_valid()
public function it_should_throw_an_exception_if_data_are_not_valid(): void
{
$data = $this->get_valid_capture_request_data();
$data = $this->getValidCaptureRequestData();
$data[0] = '...'; // Not valid
$this->beConstructedThrough('createCaptureRequest', $data);
$exception = new ValidationException('- merchantAlias must contain only letters (a-z), digits (0-9) and "_"');
$exception = new ValidationException('- These rules must pass for `[object] (Webgriffe\LibQuiPago\Api\EcRequest: { })`
- merchantAlias must contain only letters (a-z), digits (0-9) and "_"');
$this->shouldThrow($exception)->duringInstantiation();
}

function it_is_initializable_as_void_request()
public function it_is_initializable_as_void_request(): void
{
$this->beConstructedThrough('createVoidRequest', $this->get_valid_void_request_data());
$this->beConstructedThrough('createVoidRequest', $this->getValidVoidRequestData());
$this->shouldHaveType(EcRequest::class);
$this->getUrl()->shouldReturn('https://ecommerce.nexi.it/ecomm/ecomm/XPayBo');

Expand All @@ -87,10 +87,7 @@ function it_is_initializable_as_void_request()
$this->getBody()->shouldReturn($body);
}

/**
* @return array
*/
private function get_valid_capture_request_data()
private function getValidCaptureRequestData(): array
{
return [
'0000000050242004', // $merchantAlias,
Expand All @@ -107,10 +104,7 @@ private function get_valid_capture_request_data()
];
}

/**
* @return array
*/
private function get_valid_void_request_data()
private function getValidVoidRequestData(): array
{
return [
'0000000050242004', // $merchantAlias,
Expand Down
Loading