Skip to content

Commit

Permalink
PHP8 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsacksick committed Aug 14, 2024
1 parent ad0b052 commit f6aabe6
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
65 changes: 26 additions & 39 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
verbose="true">

<testsuites>
<testsuite name="Unit Tests">
<directory>tests/Unit</directory>
</testsuite>

<testsuite name="Component Tests">
<directory>tests/Component</directory>
</testsuite>

<testsuite name="Integration Tests">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-html" target="build/coverage"
lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="junit" target="build/logs/junit.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="tests/bootstrap.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage" lowUpperBound="35" highLowerBound="70"/>
</report>
</coverage>
<testsuites>
<testsuite name="Unit Tests">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Component Tests">
<directory>tests/Component</directory>
</testsuite>
<testsuite name="Integration Tests">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/logs/junit.xml"/>
</logging>
<source>
<include>
<directory>src/</directory>
</include>
</source>
</phpunit>
8 changes: 7 additions & 1 deletion src/Klarna/Rest/Resource.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* Copyright 2019 Klarna AB
*
Expand Down Expand Up @@ -106,16 +109,19 @@ public function setLocation($url)
*
* @param array $array Data to be exchanged
*/
public function exchangeArray($array)
public function exchangeArray($array): array
{
$id = $this->getId();
$return = $this;

if (!is_null($array)) {
parent::exchangeArray($array);
}
if (is_null($this->getId()) && !is_null($id)) {
$this->setId($id);
}

return (array) $return;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Klarna/Rest/Transport/Exception/ConnectorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class ConnectorException extends \RuntimeException
*/
protected $correlationId;

/**
* The service version.
*
* @var string
*/
protected $serviceVersion;

/**
* Constructs a connector exception instance.
*
Expand All @@ -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" : '';

Expand Down
4 changes: 1 addition & 3 deletions tests/Component/InstantShopping/ButtonKeysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Component/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}");
}
}
}
Expand All @@ -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;
}
}
28 changes: 19 additions & 9 deletions tests/Unit/Checkout/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,6 +36,11 @@ class ResourceTest extends TestCase

const PATH = '/test/url';

/**
* @var \GuzzleHttp\ClientInterface;
*/
protected $client;

/**
* @var Connector
*/
Expand All @@ -54,7 +54,7 @@ class ResourceTest extends TestCase
/**
* Set up the test fixtures.
*/
protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand All @@ -79,7 +79,7 @@ protected function setUp()
/**
* Set up the test fixtures.
*/
protected function tearDown()
protected function tearDown(): void
{
parent::tearDown();

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Transport/GuzzleConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class GuzzleConnectorTest extends TestCase
/**
* Set up the test fixtures.
*/
protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Transport/ResponseValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Transport/UserAgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class UserAgentTest extends TestCase
/**
* Set up the test fixtures
*/
protected function setUp()
protected function setUp(): void
{
$this->agent = new UserAgent();
}
Expand All @@ -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',
Expand Down

0 comments on commit f6aabe6

Please sign in to comment.