Skip to content

Commit

Permalink
Support testing against PHP 8 and 8.1. (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcr-stripe authored Dec 1, 2021
1 parent 3a02959 commit a6be5eb
Show file tree
Hide file tree
Showing 176 changed files with 490 additions and 502 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@ jobs:
- uses: actions/checkout@master

- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga:2.19.0
uses: docker://oskarstark/php-cs-fixer-ga:3.2.1
with:
args: --diff-format=udiff --dry-run --show-progress=dots --using-cache=no --verbose .
args: --format=txt --diff --dry-run --using-cache=no --verbose .

phpstan:
name: PHPStan

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-version:
- "7.4"

steps:
- uses: actions/checkout@master

- name: Setup PHP
uses: shivammathur/setup-php@master
with:
php-version: 7.4
php-version: ${{ matrix.php-version }}

- name: Get Composer Cache Directory
id: composer-cache
Expand Down Expand Up @@ -75,6 +81,8 @@ jobs:
- "7.2"
- "7.3"
- "7.4"
- "8.0"
- "8.1"

steps:
- uses: actions/checkout@master
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ clover.xml
# Ignore PHP CS Fixer local config and cache
.php_cs
.php_cs.cache
.php-cs-fixer.cache

# Ignore PHPStan local config
.phpstan.neon

# Ignore phpDocumentor's local config and artifacts
.phpdoc/*
phpdoc.xml

# Ignore cached PHPUnit results.
.phpunit.result.cache
61 changes: 61 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

$finder = PhpCsFixer\Finder::create()
->notPath('tests/TestCase.php');

$config = new PhpCsFixer\Config();
$config->setRiskyAllowed(true);
$config->setRules([
// Rulesets
'@PSR2' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'@PHP56Migration:risky' => true,
'@PHPUnit57Migration:risky' => true,

// Additional rules
'fopen_flags' => true,
'linebreak_after_opening_tag' => true,
// This one is non-deterministic based on what environment you are running it in and what `get_defined_constants` returns.
'native_constant_invocation' => false,
'native_function_invocation' => [
"strict" => false,
],

// --- Diffs from @PhpCsFixer / @PhpCsFixer:risky ---

// This is the same as the default for the @PhpCsFixer ruleset, minus
// the following values: ['include', 'include_once', 'require',
// 'require_once']. We could enable them and remove this line after
// updating codegen for the `init.php` file to be compliant.
'blank_line_before_statement' => ['statements' => ['break', 'case', 'continue', 'declare', 'default', 'exit', 'goto', 'return', 'switch', 'throw', 'try']],

// This is just prettier / easier to read.
'concat_space' => ['spacing' => 'one'],

// This causes strange ordering with codegen'd classes. We might be
// able to enable this if we update codegen to output class elements
// in the correct order.
'ordered_class_elements' => false,

// Keep this disabled to avoid unnecessary diffs in PHPDoc comments of
// codegen'd classes.
'phpdoc_align' => false,

// This is a "risky" rule that causes a bug in our codebase.
// Specifically, in `StripeObject.updateAttributes` we construct new
// `StripeObject`s for metadata. We can't use `self` there because it
// needs to be a raw `StripeObject`.
'self_accessor' => false,

// Visibility annotations are not supported by php5.6
'visibility_required' => false,

// Apparently "uninitialized" is distinct from "null" in some versions of PHP
// so I am defensively disabling this rule so as to not cause breaking changes
// but we can feel free to remove it in a major version (or maybe in a minor if
// we devote some effort into determining that it is safe)
'no_null_property_initialization' => false,
]);
$config->setFinder($finder);
return $config;
45 changes: 0 additions & 45 deletions .php_cs.dist

This file was deleted.

5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"phpunit/phpunit": "^5.7 || ^9.0",
"php-coveralls/php-coveralls": "^2.1",
"squizlabs/php_codesniffer": "^3.3",
"symfony/process": "~3.4",
"friendsofphp/php-cs-fixer": "2.17.1"
"friendsofphp/php-cs-fixer": "3.2.1 || 2.17.1"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ private function _requestRaw($method, $url, $params, $headers)

if (isset($rheaders['request-id'])
&& \is_string($rheaders['request-id'])
&& \strlen($rheaders['request-id']) > 0) {
&& '' !== $rheaders['request-id']) {
self::$requestTelemetry = new RequestTelemetry(
$rheaders['request-id'],
Util\Util::currentTimeMillis() - $requestStartMs
Expand Down Expand Up @@ -501,7 +501,7 @@ private function _requestRawStreaming($method, $url, $params, $headers, $readBod

if (isset($rheaders['request-id'])
&& \is_string($rheaders['request-id'])
&& \strlen($rheaders['request-id']) > 0) {
&& '' !== $rheaders['request-id']) {
self::$requestTelemetry = new RequestTelemetry(
$rheaders['request-id'],
Util\Util::currentTimeMillis() - $requestStartMs
Expand Down
4 changes: 2 additions & 2 deletions lib/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static function setVerifySslCerts($verify)
}

/**
* @return string | null The Stripe account ID for connected account
* @return null|string The Stripe account ID for connected account
* requests
*/
public static function getAccountId()
Expand All @@ -194,7 +194,7 @@ public static function setAccountId($accountId)
}

/**
* @return array | null The application's information
* @return null|array The application's information
*/
public static function getAppInfo()
{
Expand Down
4 changes: 2 additions & 2 deletions lib/Util/RandomGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function randFloat($max = 1.0)
public function uuid()
{
$arr = \array_values(\unpack('N1a/n4b/N1c', \openssl_random_pseudo_bytes(16)));
$arr[2] = ($arr[2] & 0x0fff) | 0x4000;
$arr[3] = ($arr[3] & 0x3fff) | 0x8000;
$arr[2] = ($arr[2] & 0x0FFF) | 0x4000;
$arr[3] = ($arr[3] & 0x3FFF) | 0x8000;

return \vsprintf('%08x-%04x-%04x-%04x-%04x%08x', $arr);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/AccountLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @internal
* @covers \Stripe\AccountLink
*/
final class AccountLinkTest extends \PHPUnit\Framework\TestCase
final class AccountLinkTest extends \Stripe\TestCase
{
use TestHelper;

Expand Down
12 changes: 6 additions & 6 deletions tests/Stripe/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @internal
* @covers \Stripe\Account
*/
final class AccountTest extends \PHPUnit\Framework\TestCase
final class AccountTest extends \Stripe\TestCase
{
use TestHelper;

Expand All @@ -22,7 +22,7 @@ public function testIsListable()
'/v1/accounts'
);
$resources = Account::all();
static::assertInternalType('array', $resources->data);
static::compatAssertIsArray($resources->data);
static::assertInstanceOf(\Stripe\Account::class, $resources->data[0]);
}

Expand Down Expand Up @@ -132,7 +132,7 @@ public function testPersons()
'/v1/accounts/' . $account->id . '/persons'
);
$persons = $account->persons();
static::assertInternalType('array', $persons->data);
static::compatAssertIsArray($persons->data);
static::assertInstanceOf(\Stripe\Person::class, $persons->data[0]);
}

Expand Down Expand Up @@ -165,7 +165,7 @@ public function testCanListCapabilities()
'/v1/accounts/' . self::TEST_RESOURCE_ID . '/capabilities'
);
$resources = Account::allCapabilities(self::TEST_RESOURCE_ID);
static::assertInternalType('array', $resources->data);
static::compatAssertIsArray($resources->data);
}

public function testCanCreateExternalAccount()
Expand Down Expand Up @@ -219,7 +219,7 @@ public function testCanListExternalAccounts()
'/v1/accounts/' . self::TEST_RESOURCE_ID . '/external_accounts'
);
$resources = Account::allExternalAccounts(self::TEST_RESOURCE_ID);
static::assertInternalType('array', $resources->data);
static::compatAssertIsArray($resources->data);
}

public function testCanCreateLoginLink()
Expand Down Expand Up @@ -287,7 +287,7 @@ public function testCanListPersons()
'/v1/accounts/' . self::TEST_RESOURCE_ID . '/persons'
);
$resources = Account::allPersons(self::TEST_RESOURCE_ID);
static::assertInternalType('array', $resources->data);
static::compatAssertIsArray($resources->data);
}

public function testSerializeNewAdditionalOwners()
Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/AlipayAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @internal
* @covers \Stripe\AlipayAccount
*/
final class AlipayAccountTest extends \PHPUnit\Framework\TestCase
final class AlipayAccountTest extends \Stripe\TestCase
{
use TestHelper;

Expand Down
20 changes: 10 additions & 10 deletions tests/Stripe/ApiRequestorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @internal
* @covers \Stripe\ApiRequestor
*/
final class ApiRequestorTest extends \PHPUnit\Framework\TestCase
final class ApiRequestorTest extends \Stripe\TestCase
{
use TestHelper;

Expand Down Expand Up @@ -88,7 +88,7 @@ public function testDefaultHeaders()
public function testRaisesAuthenticationErrorWhenNoApiKey()
{
$this->expectException(\Stripe\Exception\AuthenticationException::class);
$this->expectExceptionMessageRegExp('#No API key provided#');
$this->compatExpectExceptionMessageMatches('#No API key provided#');

Stripe::setApiKey(null);
Charge::create();
Expand Down Expand Up @@ -117,7 +117,7 @@ public function testRaisesInvalidRequestErrorOn400()
static::fail('Did not raise error');
} catch (Exception\InvalidRequestException $e) {
static::assertSame(400, $e->getHttpStatus());
static::assertInternalType('array', $e->getJsonBody());
static::compatAssertIsArray($e->getJsonBody());
static::assertSame('Missing id', $e->getMessage());
static::assertSame('id', $e->getStripeParam());
} catch (\Exception $e) {
Expand Down Expand Up @@ -147,7 +147,7 @@ public function testRaisesIdempotencyErrorOn400AndTypeIdempotencyError()
static::fail('Did not raise error');
} catch (Exception\IdempotencyException $e) {
static::assertSame(400, $e->getHttpStatus());
static::assertInternalType('array', $e->getJsonBody());
static::compatAssertIsArray($e->getJsonBody());
static::assertSame("Keys for idempotent requests can only be used with the same parameters they were first used with. Try using a key other than 'abc' if you meant to execute a different request.", $e->getMessage());
} catch (\Exception $e) {
static::fail('Unexpected exception: ' . \get_class($e));
Expand Down Expand Up @@ -176,7 +176,7 @@ public function testRaisesAuthenticationErrorOn401()
static::fail('Did not raise error');
} catch (Exception\AuthenticationException $e) {
static::assertSame(401, $e->getHttpStatus());
static::assertInternalType('array', $e->getJsonBody());
static::compatAssertIsArray($e->getJsonBody());
static::assertSame('You did not provide an API key.', $e->getMessage());
} catch (\Exception $e) {
static::fail('Unexpected exception: ' . \get_class($e));
Expand Down Expand Up @@ -209,7 +209,7 @@ public function testRaisesCardErrorOn402()
static::fail('Did not raise error');
} catch (Exception\CardException $e) {
static::assertSame(402, $e->getHttpStatus());
static::assertInternalType('array', $e->getJsonBody());
static::compatAssertIsArray($e->getJsonBody());
static::assertSame('Your card was declined.', $e->getMessage());
static::assertSame('card_declined', $e->getStripeCode());
static::assertSame('generic_decline', $e->getDeclineCode());
Expand Down Expand Up @@ -241,7 +241,7 @@ public function testRaisesPermissionErrorOn403()
static::fail('Did not raise error');
} catch (Exception\PermissionException $e) {
static::assertSame(403, $e->getHttpStatus());
static::assertInternalType('array', $e->getJsonBody());
static::compatAssertIsArray($e->getJsonBody());
static::assertSame("The provided key 'sk_test_********************1234' does not have access to account 'foo' (or that account does not exist). Application access may have been revoked.", $e->getMessage());
} catch (\Exception $e) {
static::fail('Unexpected exception: ' . \get_class($e));
Expand Down Expand Up @@ -271,7 +271,7 @@ public function testRaisesInvalidRequestErrorOn404()
static::fail('Did not raise error');
} catch (Exception\InvalidRequestException $e) {
static::assertSame(404, $e->getHttpStatus());
static::assertInternalType('array', $e->getJsonBody());
static::compatAssertIsArray($e->getJsonBody());
static::assertSame('No such charge: foo', $e->getMessage());
static::assertSame('id', $e->getStripeParam());
} catch (\Exception $e) {
Expand Down Expand Up @@ -300,7 +300,7 @@ public function testRaisesRateLimitErrorOn429()
static::fail('Did not raise error');
} catch (Exception\RateLimitException $e) {
static::assertSame(429, $e->getHttpStatus());
static::assertInternalType('array', $e->getJsonBody());
static::compatAssertIsArray($e->getJsonBody());
static::assertSame('Too many requests', $e->getMessage());
} catch (\Exception $e) {
\var_dump($e);
Expand Down Expand Up @@ -330,7 +330,7 @@ public function testRaisesRateLimitErrorOn400AndCodeRateLimit()
static::fail('Did not raise error');
} catch (Exception\RateLimitException $e) {
static::assertSame(400, $e->getHttpStatus());
static::assertInternalType('array', $e->getJsonBody());
static::compatAssertIsArray($e->getJsonBody());
static::assertSame('Too many requests', $e->getMessage());
} catch (\Exception $e) {
static::fail('Unexpected exception: ' . \get_class($e));
Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/ApiResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function pdf($readBodyChunk, $params = null, $opts = null)
* @internal
* @coversNothing
*/
final class ApiResourceTest extends \PHPUnit\Framework\TestCase
final class ApiResourceTest extends \Stripe\TestCase
{
use TestHelper;

Expand Down
Loading

0 comments on commit a6be5eb

Please sign in to comment.