Skip to content

Commit

Permalink
Add selector function for json_encode.
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Sep 30, 2023
1 parent 6f66e66 commit 442862f
Show file tree
Hide file tree
Showing 33 changed files with 130 additions and 118 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4']
php-versions: ['7.1', '7.2', '7.3', '7.4']
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -28,7 +28,7 @@ jobs:
- name: Install Composer dependencies
env:
PHP_VERSION: ${{ matrix.php-versions }}
run: composer install --no-progress --prefer-dist --optimize-autoloader $(if [ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" ]; then echo "--ignore-platform-reqs"; fi;)
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: Run type checking analysis
env:
PHP_VERSION: ${{ matrix.php-versions }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Install Composer dependencies
env:
PHP_VERSION: ${{ matrix.php-versions }}
run: composer install --no-progress --prefer-dist --optimize-autoloader $(if [ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" ]; then echo "--ignore-platform-reqs"; fi;)
run: $(if [ "$PHP_VERSION" == "7.0" ]; then composer config --no-plugins allow-plugins.kylekatarnls/update-helper && fi;) composer install --no-progress --prefer-dist --optimize-autoloader
- name: Run tests with code coverage
env:
PHP_VERSION: ${{ matrix.php-versions }}
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM srmklive/docker-php-cli:7.2
FROM srmklive/docker-php-cli:7.1

LABEL maintainer="Raza Mehdi<[email protected]>"

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"nesbot/carbon": "~1.0|~2.0"
},
"require-dev": {
"phpstan/phpstan": "~0.1|~1.0",
"phpunit/phpunit": "^5.7|^6.0|^7.0|^8.0",
"symfony/var-dumper": "~3.0|~4.0|~5.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Services/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Srmklive\PayPal\Services;

use GuzzleHttp\Utils;
use Srmklive\PayPal\Traits\JsonDecodeSelector;
use Srmklive\PayPal\Traits\JsonEncodeDecodeSelector;

class Str extends \Illuminate\Support\Str
{
use JsonDecodeSelector;
use JsonEncodeDecodeSelector;

/**
* Determine if a given value is valid JSON.
Expand Down
16 changes: 0 additions & 16 deletions src/Traits/JsonDecodeSelector.php

This file was deleted.

30 changes: 30 additions & 0 deletions src/Traits/JsonEncodeDecodeSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Srmklive\PayPal\Traits;

trait JsonEncodeDecodeSelector
{
/**
* Decide the function to use for decoding json.
*
* @return string
*/
protected function jsonEncodeFunction(): string
{
return class_exists(\GuzzleHttp\Utils::class) &&
method_exists(\GuzzleHttp\Utils::class, 'jsonEncode') ?
'\GuzzleHttp\Utils::jsonEncode' : '\GuzzleHttp\json_encode';
}

/**
* Decide the function to use for decoding json.
*
* @return string
*/
protected function jsonDecodeFunction(): string
{
return class_exists(\GuzzleHttp\Utils::class) &&
method_exists(\GuzzleHttp\Utils::class, 'jsonDecode') ?
'\GuzzleHttp\Utils::jsonDecode' : '\GuzzleHttp\json_decode';
}
}
2 changes: 1 addition & 1 deletion src/Traits/PayPalRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait PayPalRequest
{
use JsonDecodeSelector;
use JsonEncodeDecodeSelector;
use PayPalHttpClient;
use PayPalAPI;
use PayPalExperienceContext;
Expand Down
9 changes: 3 additions & 6 deletions tests/MockClientClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
use GuzzleHttp\Utils;
use Psr\Http\Message\ResponseInterface;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
use Srmklive\PayPal\Traits\JsonDecodeSelector;
use Srmklive\PayPal\Traits\JsonEncodeDecodeSelector;

trait MockClientClasses
{
use JsonDecodeSelector;
use JsonEncodeDecodeSelector;

private function mock_http_client($response): HttpClient
{
$mock = new HttpMockHandler([
new HttpResponse(
200,
[],
($response === false) ? '' : Utils::jsonEncode($response)
($response === false) ? '' : $this->jsonEncodeFunction()($response)
),
]);

Expand Down Expand Up @@ -58,9 +58,6 @@ private function mock_http_request($expectedResponse, $expectedEndpoint, $expect
private function mock_client($expectedResponse, $expectedMethod, $token = false, $additionalMethod = null)
{
$set_method_name = 'setMethods';
if (strpos(phpversion(), '8.1') !== false || strpos(phpversion(), '8.2') !== false) {
$set_method_name = 'onlyMethods';
}

$methods = [$expectedMethod, 'setApiCredentials'];
$methods[] = ($token) ? 'getAccessToken' : '';
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function it_throws_exception_if_invalid_credentials_are_provided()
public function it_throws_exception_if_invalid_mode_is_provided()
{
$this->expectException(\RuntimeException::class);
$this->expectErrorMessage('Invalid configuration provided. Please provide valid configuration for PayPal API. You can also refer to the documentation at https://srmklive.github.io/laravel-paypal/docs.html to setup correct configuration.');
$this->expectExceptionMessage('Invalid configuration provided. Please provide valid configuration for PayPal API. You can also refer to the documentation at https://srmklive.github.io/laravel-paypal/docs.html to setup correct configuration.');

$credentials = $this->getMockCredentials();
$credentials['mode'] = '';
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/Client/BillingPlansTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function it_can_create_a_billing_plan()
'json' => $this->createPlanParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -48,7 +48,7 @@ public function it_can_list_billing_plans()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'get');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -68,7 +68,7 @@ public function it_can_update_a_billing_plan()
'json' => $this->updatePlanParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'patch');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'patch');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->patch($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -87,7 +87,7 @@ public function it_can_show_details_for_a_billing_plan()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'get');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -106,7 +106,7 @@ public function it_can_activate_a_billing_plan()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -125,7 +125,7 @@ public function it_can_deactivate_a_billing_plan()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -145,7 +145,7 @@ public function it_can_update_pricing_for_a_billing_plan()
'json' => $this->updatePlanPricingParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Client/CatalogProductsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function it_can_create_a_product()
'json' => $this->createProductParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -48,7 +48,7 @@ public function it_can_list_products()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'get');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -68,7 +68,7 @@ public function it_can_update_a_product()
'json' => $this->updateProductParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'patch');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'patch');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->patch($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -87,7 +87,7 @@ public function it_can_get_details_for_a_product()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'get');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Client/DisputeActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function it_can_accept_dispute_claim()
'json' => $this->acceptDisputeClaimParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -49,7 +49,7 @@ public function it_can_accept_dispute_offer_resolution()
'json' => $this->acceptDisputeResolutionParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -69,7 +69,7 @@ public function it_can_acknowledge_item_is_returned_for_raised_dispute()
'json' => $this->acknowledgeItemReturnedParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Client/DisputesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function it_can_list_disputes()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'get');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -48,7 +48,7 @@ public function it_can_partially_update_a_dispute()
'json' => $this->updateDisputeParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'patch');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'patch');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->patch($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -67,7 +67,7 @@ public function it_can_get_details_for_a_dispute()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'get');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Client/IdentityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function it_can_user_profile_details()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'get');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Client/InvoicesSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function it_can_search_invoices()
'json' => $this->invoiceSearchParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Client/InvoicesTemplatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function it_can_create_invoice_template()
'json' => $this->mockCreateInvoiceTemplateParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'post');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -48,7 +48,7 @@ public function it_can_list_invoice_templates()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'get');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -67,7 +67,7 @@ public function it_can_delete_an_invoice_template()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'delete');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'delete');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->delete($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -87,7 +87,7 @@ public function it_can_update_an_invoice_template()
'json' => $this->mockUpdateInvoiceTemplateParams(),
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'put');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'put');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->put($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand All @@ -106,7 +106,7 @@ public function it_can_get_details_for_an_invoice_template()
],
];

$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$mockHttpClient = $this->mock_http_request($this->jsonEncodeFunction()($expectedResponse), $expectedEndpoint, $expectedParams, 'get');

$this->assertEquals($expectedResponse, $this->jsonDecodeFunction()($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
Expand Down
Loading

0 comments on commit 442862f

Please sign in to comment.