Skip to content

Commit

Permalink
Merge pull request #14 from Art4/phpunit-10
Browse files Browse the repository at this point in the history
Phpunit 10 and Code Coverage
  • Loading branch information
Art4 committed Aug 31, 2023
2 parents 725531d + 0ca0ecf commit cc2767d
Show file tree
Hide file tree
Showing 58 changed files with 248 additions and 292 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
with:
php-version: ${{ matrix.php }}
ini-values: error_reporting=-1, display_errors=On, log_errors_max_len=0
coverage: none
coverage: xdebug
tools: none

# Install dependencies and handle caching in one go.
Expand All @@ -57,9 +57,17 @@ jobs:
composer-options: --ignore-platform-reqs

- name: Run phpunit tests
run: composer run phpunit -- --coverage-text
run: composer run phpunit -- --coverage-clover ./.code-coverage/clover.xml

- name: Run static code analysis
# Do not run phpstan on lowest php version
if: ${{ matrix.prefer-versions == 'latest' }}
run: composer run phpstan -- --error-format=github

- name: Send coverage report to Codecov
if: ${{ success() && matrix.php == '8.2' }}
uses: codecov/codecov-action@v3
with:
files: ./.code-coverage/clover.xml
fail_ci_if_error: true
verbose: true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/.code-coverage
/.php-cs-fixer.cache
/.phpunit.cache/
/.phpunit.result.cache
/composer.lock
/vendor/
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add support for PHP 8.2
- Add tests with PHPStan level 8

### Changed

- 100% code coverage
- Drop support for PHP 7.1

## [1.0.1 - 2023-05-31](https://github.com/Art4/WP-Requests-PSR18-Adapter/compare/1.0.0...1.0.1)
Expand Down Expand Up @@ -43,8 +45,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Change Code Style to PSR-12
- Drop support for PHP 7.0

## [1.0.0-beta - 2022-10-06](https://github.com/Art4/WP-Requests-PSR18-Adapter/compare/09aae5d7deac8058c5a25c1d951cd350d066ad6e...1.0.0-beta)
## [1.0.0-beta - 2022-10-06](https://github.com/Art4/WP-Requests-PSR18-Adapter/compare/f2ab377f89c42ed364f0324a99ff164ae929d22a...1.0.0-beta)

### Added

- Library created from pull request
- Library created from pull request: https://github.com/WordPress/Requests/pull/768
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# WordPress/Requests PSR-18 Adapter

[![Latest Version](https://img.shields.io/github/release/Art4/WP-Requests-PSR18-Adapter.svg)](https://github.com/Art4/WP-Requests-PSR18-Adapter/releases)
[![Software License](https://img.shields.io/badge/license-GPL3-brightgreen.svg)](LICENSE.md)
[![Build Status](https://github.com/Art4/WP-Requests-PSR18-Adapter/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/Art4/WP-Requests-PSR18-Adapter/actions)
[![codecov](https://codecov.io/gh/Art4/WP-Requests-PSR18-Adapter/branch/main/graph/badge.svg?token=NWWJXG6MIL)](https://codecov.io/gh/Art4/WP-Requests-PSR18-Adapter)
[![Total Downloads](https://img.shields.io/packagist/dt/art4/requests-psr18-adapter.svg)](https://packagist.org/packages/art4/requests-psr18-adapter)

Use [WordPress/Requests](https://github.com/WordPress/Requests) as a [PSR-18](https://www.php-fig.org/psr/psr-18/) HTTP client adapter.

Requires PHP 7.2+
- Requires PHP 7.2+
- Supports Requests v1.8+ and v2

## Why?

Requests is a HTTP library written in PHP, that [lacks of support for PSR-7](https://github.com/WordPress/Requests/issues/320) and also for PSR-18 because of the compatability with PHP 5.6+.

[I've created a PR in Requests to add PSR-7 support](https://github.com/WordPress/Requests/pull/768) but I would be able to use it today. So I created this library. If one day Requests nativly supports PSR-7 and PSR-18, this library will become obsolete.
[I've created a PR in Requests to add PSR-7 support](https://github.com/WordPress/Requests/pull/768) but this would add new direct dependencies to Requests. So I created this library as an optional wrapper for Requests. If one day Requests nativly supports PSR-7 and PSR-18, this library might become obsolete.

## How to use

Expand Down Expand Up @@ -54,5 +61,5 @@ try {
}

// Use the PSR-7 Response
var_dump($response);
var_dump($response->getBody()->__toString());
```
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^8 || ^9",
"yoast/phpunit-polyfills": "^1.0.3"
"phpunit/phpunit": "^8 || ^9 || ^10",
"yoast/phpunit-polyfills": "^2"
},
"autoload": {
"files": ["v1-compat/autoload.php"],
Expand All @@ -37,6 +37,7 @@
}
},
"scripts": {
"coverage": "phpunit --coverage-html=.code-coverage",
"phpstan": "phpstan analyze --memory-limit 512M --configuration .phpstan.neon",
"phpunit": "phpunit"
},
Expand Down
27 changes: 13 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutCoverageMetadata="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
cacheDirectory=".phpunit.cache"
requireCoverageMetadata="false">
<coverage/>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
8 changes: 4 additions & 4 deletions src/Psr/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public static function fromResponse(RequestsResponse $response)

$headers = [];

/**
* @var string $name
* @var string[] $value
*/
foreach ($response->headers->getAll() as $name => $value) {
if (!is_array($value)) {
$value = [$value];
}

$headers[$name] = $value;
}

Expand Down
4 changes: 0 additions & 4 deletions src/Psr/StringBasedStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,6 @@ public function getContents(): string
*/
public function getMetadata(?string $key = null)
{
if (func_num_args() > 0 && !is_string($key)) {
throw InvalidArgument::create(1, '$key', 'string', gettype($key));
}

if (is_string($key)) {
return null;
}
Expand Down
19 changes: 9 additions & 10 deletions src/Psr/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,20 @@ public function getPort(): ?int
$port = $this->iri->port;
$scheme = $this->getScheme();

try {
$portFromScheme = Port::get($scheme);
} catch (RequestsException $th) {
$portFromScheme = null;
}

if ($port === null) {
try {
return Port::get($scheme);
} catch (RequestsException $th) {
return null;
}
return $portFromScheme;
}

$port = intval($port);

try {
if ($port === Port::get($scheme)) {
return null;
}
} catch (RequestsException $th) {
if ($port === $portFromScheme) {
return null;
}

return $port;
Expand Down
31 changes: 31 additions & 0 deletions tests/Exception/Psr/NetworkException/GetRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Art4\Requests\Tests\Exception\Psr\NetworkException;

use Art4\Requests\Exception\Psr\NetworkException;
use Psr\Http\Message\RequestInterface;
use WpOrg\Requests\Exception\Transport;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

final class GetRequestTest extends TestCase
{
/**
* Tests receiving the request when using getRequest().
*
* @covers \Art4\Requests\Exception\Psr\NetworkException::__construct
* @covers \Art4\Requests\Exception\Psr\NetworkException::getRequest
*
* @return void
*/
public function testGetRequestReturnsRequest()
{
$request = $this->createMock(RequestInterface::class);
$previous = $this->createMock(Transport::class);

$exception = new NetworkException($request, $previous);

$this->assertSame($request, $exception->getRequest());
}
}
31 changes: 31 additions & 0 deletions tests/Exception/Psr/RequestException/GetRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Art4\Requests\Tests\Exception\Psr\RequestException;

use Art4\Requests\Exception\Psr\RequestException;
use Psr\Http\Message\RequestInterface;
use WpOrg\Requests\Exception\Transport;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

final class GetRequestTest extends TestCase
{
/**
* Tests receiving the request when using getRequest().
*
* @covers \Art4\Requests\Exception\Psr\RequestException::__construct
* @covers \Art4\Requests\Exception\Psr\RequestException::getRequest
*
* @return void
*/
public function testGetRequestReturnsRequest()
{
$request = $this->createMock(RequestInterface::class);
$previous = $this->createMock(Transport::class);

$exception = new RequestException($request, $previous);

$this->assertSame($request, $exception->getRequest());
}
}
2 changes: 1 addition & 1 deletion tests/Psr/HttpClient/CreateRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testCreateRequestWithoutUriStringThrowsException($input)
*
* @return array<string, mixed>
*/
public function dataInvalidTypeNotString()
public static function dataInvalidTypeNotString()
{
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_STRING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
use WpOrg\Requests\Transport;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

final class HttpClientTest extends TestCase
final class SendRequestTest extends TestCase
{
/**
* Tests receiving a response when using sendRequest().
*
* @covers \Art4\Requests\Psr\HttpClient::__construct
* @covers \Art4\Requests\Psr\HttpClient::sendRequest
*
* @return void
Expand Down
14 changes: 1 addition & 13 deletions tests/Psr/Request/GetHeaderLineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Art4\Requests\Tests\Psr\Request;

use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use Art4\Requests\Psr\Request;
use Psr\Http\Message\UriInterface;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Art4\Requests\Tests\TypeProviderHelper;

final class GetHeaderLineTest extends TestCase
{
Expand Down Expand Up @@ -55,14 +53,4 @@ public function testGetHeaderLineWithCaseInsensitiveNameReturnsString()

$this->assertSame('value', $request->getHeaderLine('NAME'));
}

/**
* Data Provider.
*
* @return array<string, mixed>
*/
public function dataInvalidTypeNotString()
{
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_STRING);
}
}
14 changes: 1 addition & 13 deletions tests/Psr/Request/GetHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Art4\Requests\Tests\Psr\Request;

use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use Art4\Requests\Psr\Request;
use Psr\Http\Message\UriInterface;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Art4\Requests\Tests\TypeProviderHelper;

final class GetHeaderTest extends TestCase
{
Expand Down Expand Up @@ -55,14 +53,4 @@ public function testGetHeaderWithCaseInsensitiveNameReturnsArray()

$this->assertSame(['value'], $request->getHeader('NAME'));
}

/**
* Data Provider.
*
* @return array<string, mixed>
*/
public function dataInvalidTypeNotString()
{
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_STRING);
}
}
2 changes: 1 addition & 1 deletion tests/Psr/Request/GetRequestTargetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testGetRequestTargetReturnsString(string $path, string $query, s
*
* @return array<string,string[]>
*/
public function dataGetRequestTarget(): array
public static function dataGetRequestTarget(): array
{
return [
'Retrieves the message request target' => ['path', '', 'path'],
Expand Down
14 changes: 1 addition & 13 deletions tests/Psr/Request/HasHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Art4\Requests\Tests\Psr\Request;

use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use Art4\Requests\Psr\Request;
use Psr\Http\Message\UriInterface;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Art4\Requests\Tests\TypeProviderHelper;

final class HasHeaderTest extends TestCase
{
Expand Down Expand Up @@ -55,14 +53,4 @@ public function testHasHeaderWithCaseInsensitiveNameReturnsTrue()

$this->assertTrue($request->hasHeader('name'));
}

/**
* Data Provider.
*
* @return array<string, mixed>
*/
public function dataInvalidTypeNotString()
{
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_STRING);
}
}
Loading

0 comments on commit cc2767d

Please sign in to comment.