From f6aabe6a5a80fb5710c9101238b1a557c09c7e58 Mon Sep 17 00:00:00 2001 From: Jonathan Sacksick Date: Wed, 14 Aug 2024 11:15:18 +0300 Subject: [PATCH] PHP8 support. --- .github/workflows/php.yml | 2 +- composer.json | 5 +- phpunit.xml.dist | 65 ++++++++----------- src/Klarna/Rest/Resource.php | 8 ++- .../Exception/ConnectorException.php | 9 ++- .../InstantShopping/ButtonKeysTest.php | 4 +- tests/Component/TestCase.php | 2 +- tests/Integration/TestCase.php | 6 +- tests/Unit/Checkout/OrderTest.php | 28 +++++--- tests/Unit/ResourceTest.php | 14 ++-- tests/Unit/TestCase.php | 2 +- tests/Unit/Transport/GuzzleConnectorTest.php | 4 +- .../Unit/Transport/ResponseValidatorTest.php | 2 +- tests/Unit/Transport/UserAgentTest.php | 8 +-- 14 files changed, 83 insertions(+), 76 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 5a0ae0f..867022a 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,7 +11,7 @@ jobs: strategy: matrix: php-version: - - "7.4" + - "8.1" # @todo need to bump phpunit for 8.0 testing # - "8.0" steps: diff --git a/composer.json b/composer.json index 320107a..3727d57 100644 --- a/composer.json +++ b/composer.json @@ -16,15 +16,14 @@ }, "minimum-stability": "stable", "require": { - "php": ">=7.1", + "php": ">=8.1", "guzzlehttp/guzzle": "~6.0 || ^7.0" }, "require-dev": { - "phpunit/phpunit": "~7.0", + "phpunit/phpunit": "~10.0", "squizlabs/php_codesniffer": "1.5.*", "phpmd/phpmd": "2.1.*", "phploc/phploc": "2.0.*", - "sebastian/phpcpd": "2.0.*", "php-coveralls/php-coveralls": "^2.1", "apigen/apigen": "4.1.*", "klarna/apigen-theme": "2.1.0" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 02457d5..f1674d5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,41 +1,28 @@ - - - - - - tests/Unit - - - - tests/Component - - - - tests/Integration - - - - - - src/ - - - - - - - - + + + + + + + + + + tests/Unit + + + tests/Component + + + tests/Integration + + + + + + + + src/ + + diff --git a/src/Klarna/Rest/Resource.php b/src/Klarna/Rest/Resource.php index 35a78c2..0399aac 100644 --- a/src/Klarna/Rest/Resource.php +++ b/src/Klarna/Rest/Resource.php @@ -1,4 +1,7 @@ getId(); + $return = $this; if (!is_null($array)) { parent::exchangeArray($array); @@ -116,6 +120,8 @@ public function exchangeArray($array) if (is_null($this->getId()) && !is_null($id)) { $this->setId($id); } + + return (array) $return; } /** diff --git a/src/Klarna/Rest/Transport/Exception/ConnectorException.php b/src/Klarna/Rest/Transport/Exception/ConnectorException.php index ce0dcd9..21e49c7 100644 --- a/src/Klarna/Rest/Transport/Exception/ConnectorException.php +++ b/src/Klarna/Rest/Transport/Exception/ConnectorException.php @@ -45,6 +45,13 @@ class ConnectorException extends \RuntimeException */ protected $correlationId; + /** + * The service version. + * + * @var string + */ + protected $serviceVersion; + /** * Constructs a connector exception instance. * @@ -57,7 +64,7 @@ public function __construct( $data = self::setDefaultData($data); $messages = implode(', ', $data['error_messages']); - $serviceVersion = isset($data['service_version']) ? $data['service_version'] : ''; + $serviceVersion = $data['service_version'] ?? ''; $message = "{$data['error_code']}: {$messages} (#{$data['correlation_id']})"; $message .= $serviceVersion ? " ServiceVersion: $serviceVersion" : ''; diff --git a/tests/Component/InstantShopping/ButtonKeysTest.php b/tests/Component/InstantShopping/ButtonKeysTest.php index e51b9ae..6d37b34 100644 --- a/tests/Component/InstantShopping/ButtonKeysTest.php +++ b/tests/Component/InstantShopping/ButtonKeysTest.php @@ -155,11 +155,9 @@ public function testRetrieve() $this->assertAuthorization($request); } - /** - * @expectedException RuntimeException - */ public function testRetrieveException() { + $this->expectException(\RuntimeException::class); $button = new ButtonKeys($this->connector); $button->retrieve(); } diff --git a/tests/Component/TestCase.php b/tests/Component/TestCase.php index 8409b3b..16772b5 100644 --- a/tests/Component/TestCase.php +++ b/tests/Component/TestCase.php @@ -63,7 +63,7 @@ class TestCase extends BaseTestCase /** * Sets up the test fixtures. */ - protected function setUp() + protected function setUp(): void { $this->mock = new MockHandler(); $this->history = []; diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php index 9ef131b..2cd2970 100644 --- a/tests/Integration/TestCase.php +++ b/tests/Integration/TestCase.php @@ -35,7 +35,7 @@ protected function hasCredentials() /** * Sets up the test fixtures. */ - protected function setUp() + protected function setUp(): void { $this->rootPath = dirname(dirname(__DIR__)); $this->credentials = json_decode(getenv('CREDENTIALS'), true); @@ -51,7 +51,7 @@ protected function setUp() if (!empty($this->credentials) && is_array($this->credentials)) { foreach ($this->credentials as $field => $value) { $field = strtoupper($field); - putenv("${field}=${value}"); + putenv("{$field}={$value}"); } } } @@ -73,6 +73,6 @@ protected function hasException($output) protected function isTextPresents($pattern, $output) { - return preg_match("/${pattern}/ims", $output) === 1; + return preg_match("/{$pattern}/ims", $output) === 1; } } diff --git a/tests/Unit/Checkout/OrderTest.php b/tests/Unit/Checkout/OrderTest.php index ab87263..89e677b 100644 --- a/tests/Unit/Checkout/OrderTest.php +++ b/tests/Unit/Checkout/OrderTest.php @@ -73,9 +73,13 @@ public function testCreate() ->method('getBody') ->will($this->returnValue('{}')); - $this->response->method('getHeader') - ->withConsecutive(['Content-Type'], ['Location']) - ->willReturnOnConsecutiveCalls(['application/json'], ['http://somewhere/a-path']); + + $this->response->expects($this->exactly(2)) + ->method('getHeader') + ->willReturnMap([ + ['Content-Type', ['application/json']], + ['Location', ['http://somewhere/a-path']] + ]); $order = new Order($this->connector); $location = $order->create($data) @@ -122,9 +126,12 @@ public function testCreateNoContentType() ->method('getStatus') ->will($this->returnValue('201')); - $this->response->method('getHeader') - ->withConsecutive(['Content-Type'], ['Location']) - ->willReturnOnConsecutiveCalls([], []); + $this->response + ->expects($this->exactly(1)) + ->method('getHeader') + ->willReturnMap([ + ['Content-Type', []] + ]); $order = new Order($this->connector); @@ -153,9 +160,12 @@ public function testCreateNoLocation() ->method('getBody') ->will($this->returnValue('{}')); - $this->response->method('getHeader') - ->withConsecutive(['Content-Type'], ['Location']) - ->willReturnOnConsecutiveCalls(['application/json'], null); + $this->response->expects($this->exactly(2)) + ->method('getHeader') + ->willReturnMap([ + ['Content-Type', ['application/json']], + ['Location', null] + ]); $order = new Order($this->connector); diff --git a/tests/Unit/ResourceTest.php b/tests/Unit/ResourceTest.php index 154dec9..081f9da 100644 --- a/tests/Unit/ResourceTest.php +++ b/tests/Unit/ResourceTest.php @@ -20,13 +20,8 @@ namespace Klarna\Rest\Tests\Unit; use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\RequestException; -use Klarna\Rest\Tests\Unit\TestCase; use Klarna\Rest\Transport\Connector; -use Klarna\Rest\Transport\Exception\ConnectorException; use Klarna\Rest\Transport\UserAgent; -use Klarna\Rest\Resource; -use GuzzleHttp\Psr7\Response; /** * Unit test cases for the resource class. @@ -41,6 +36,11 @@ class ResourceTest extends TestCase const PATH = '/test/url'; + /** + * @var \GuzzleHttp\ClientInterface; + */ + protected $client; + /** * @var Connector */ @@ -54,7 +54,7 @@ class ResourceTest extends TestCase /** * Set up the test fixtures. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -79,7 +79,7 @@ protected function setUp() /** * Set up the test fixtures. */ - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php index ddfb270..23756d9 100644 --- a/tests/Unit/TestCase.php +++ b/tests/Unit/TestCase.php @@ -42,7 +42,7 @@ class TestCase extends BaseTestCase /** * Sets up the test fixtures. */ - protected function setUp() + protected function setUp(): void { $this->response = $this->getMockBuilder(ApiResponse::class) ->getMock(); diff --git a/tests/Unit/Transport/GuzzleConnectorTest.php b/tests/Unit/Transport/GuzzleConnectorTest.php index 4753761..7db858b 100644 --- a/tests/Unit/Transport/GuzzleConnectorTest.php +++ b/tests/Unit/Transport/GuzzleConnectorTest.php @@ -62,7 +62,7 @@ class GuzzleConnectorTest extends TestCase /** * Set up the test fixtures. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -274,7 +274,7 @@ public function testCreateDefaultUserAgent() $userAgent = $connector->getUserAgent(); $this->assertInstanceOf('Klarna\Rest\Transport\UserAgent', $userAgent); - $this->assertContains('Library/Klarna.kco_rest_php', strval($userAgent)); + $this->assertStringContainsString('Library/Klarna.kco_rest_php', strval($userAgent)); } /** diff --git a/tests/Unit/Transport/ResponseValidatorTest.php b/tests/Unit/Transport/ResponseValidatorTest.php index d4883f7..645b271 100644 --- a/tests/Unit/Transport/ResponseValidatorTest.php +++ b/tests/Unit/Transport/ResponseValidatorTest.php @@ -42,7 +42,7 @@ class ResponseValidatorTest extends TestCase /** * Set up the test fixtures */ - protected function setUp() + protected function setUp(): void { $this->response = $this->getMockBuilder(ApiResponse::class) ->getMock(); diff --git a/tests/Unit/Transport/UserAgentTest.php b/tests/Unit/Transport/UserAgentTest.php index 9c2a69a..699ff27 100644 --- a/tests/Unit/Transport/UserAgentTest.php +++ b/tests/Unit/Transport/UserAgentTest.php @@ -35,7 +35,7 @@ class UserAgentTest extends TestCase /** * Set up the test fixtures */ - protected function setUp() + protected function setUp(): void { $this->agent = new UserAgent(); } @@ -50,19 +50,19 @@ public function testCreateDefault() $agent = UserAgent::createDefault(); $text = $agent->__toString(); - $this->assertContains( + $this->assertStringContainsString( 'Language/PHP_' . phpversion(), $text, 'No PHP language component present' ); - $this->assertContains( + $this->assertStringContainsString( 'OS/' . php_uname('s') . '_' . php_uname('r'), $text, 'No OS component present' ); - $this->assertContains( + $this->assertStringContainsString( 'Library/' . UserAgent::NAME . '_' . UserAgent::VERSION, $text, 'No Library component present',