Skip to content

Commit

Permalink
Merge pull request #6 from kiddivouchers/php8.1
Browse files Browse the repository at this point in the history
Allow library to work on PHP 8.1
  • Loading branch information
cs278 authored Aug 29, 2024
2 parents a8c722e + 851d4c2 commit caddc55
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 241 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
matrix:
os: [ubuntu-latest]
php:
- '8.2'
- '8.1'
fail-fast: true
name: PHP ${{ matrix.php }}
steps:
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
matrix:
os: [ubuntu-latest]
php:
- '8.1'
- '8.2'
- '8.3'
deps:
Expand All @@ -36,7 +37,7 @@ jobs:
experimental: [false]
include:
- os: ubuntu-latest
php: '8.2'
php: '8.1'
phpstan: 1
- os: ubuntu-latest
php: '8.3'
Expand All @@ -50,6 +51,10 @@ jobs:
psr17: 'nyholm/psr7:^1.8'
psr18: 'symfony/http-client:^7.1'
experimental: true
exclude:
- os: ubuntu-latest
php: 8.1
psr18: symfony/http-client:^7.1
fail-fast: true
name: 'PHP ${{ matrix.php }} / ${{ matrix.deps }} (PSR-17: ${{ matrix.psr17 }}) (PSR-18: ${{ matrix.psr18 }})'
steps:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
},
"require": {
"php": ">= 8.2",
"php": ">= 8.1",
"psr/http-client": "^1.0",
"psr/http-client-implementation": "*",
"psr/http-factory-implementation": "*",
Expand All @@ -19,8 +19,8 @@
"bamarni/composer-bin-plugin": "^1.8",
"nyholm/psr7": "^1.8",
"php-http/discovery": "^1.17",
"symfony/http-client": "^7.1",
"symfony/var-dumper": "^7.1"
"symfony/http-client": "^6.4 || ^7.1",
"symfony/var-dumper": "^6.4 || ^7.1"
},
"suggest": {
"php-http/discovery": "For automatic discovery and configuration of HTTP client."
Expand Down
5 changes: 2 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
shortenArraysForExportThreshold="10"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
Expand All @@ -18,7 +17,7 @@
</testsuite>
</testsuites>

<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<source restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
Expand Down
18 changes: 17 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,27 @@ public function __construct(
private readonly string $secretKey,
) {}

/**
* @param (HttpClientInterface&RequestFactoryInterface&StreamFactoryInterface)|null $httpClient
*/
public static function create(
#[\SensitiveParameter()]
string $secretKey,
(HttpClientInterface&RequestFactoryInterface&StreamFactoryInterface)|null $httpClient = null,
object|null $httpClient = null,
): self {
// @todo This can be removed when minimum requirement is raised to PHP 8.2.
if ($httpClient !== null && !($httpClient instanceof HttpClientInterface && $httpClient instanceof RequestFactoryInterface && $httpClient instanceof StreamFactoryInterface)) {
throw new \TypeError(sprintf(
'%s::%s(): Argument #2 ($httpClient) must be of type (%s&%s&%s)|null, %s given',
self::class,
__METHOD__,
HttpClientInterface::class,
RequestFactoryInterface::class,
StreamFactoryInterface::class,
\get_debug_type($httpClient),
));
}

if ($httpClient === null) {
if (!\class_exists(Psr18Client::class)) {
throw new \LogicException('Pass in a suitable object or install package `php-http/discovery` to have one automatically created');
Expand Down
18 changes: 18 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface as HttpClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

#[CoversClass(Client::class)]
#[UsesClass(Result::class)]
#[UsesClass(Exception\NotSupportedResponseException::class)]
final class ClientTest extends TestCase
{
public function testCreateWithInvalidObject(): void
{
$httpClient = new class implements HttpClientInterface {
public function sendRequest(RequestInterface $request): ResponseInterface
{
throw new \Exception();
}
};

$this->expectException(\TypeError::class);
$this->expectExceptionMessage('WiderPlan\\Hcaptcha\\Client::create(): Argument #2 ($httpClient) must be of type (Psr\\Http\\Client\\ClientInterface&Psr\\Http\\Message\\RequestFactoryInterface&Psr\\Http\\Message\\StreamFactoryInterface)|null, Psr\Http\Client\ClientInterface@anonymous given');

// @phpstan-ignore-next-line argument.type
Client::create('', $httpClient);
}

public function testWithoutSiteKeyAndRemoteIp(): void
{
$client = $this->createClient(function (string $method, string $url, string $body) {
Expand Down
2 changes: 1 addition & 1 deletion vendor-bin/phpunit/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require-dev": {
"phpunit/phpunit": "^11.3"
"phpunit/phpunit": "^10.5"
}
}
Loading

0 comments on commit caddc55

Please sign in to comment.