Skip to content

Commit

Permalink
Fix BC of guzzlehttp/psr7 2.0 and drop support for 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KentarouTakeda committed Jan 4, 2024
1 parent b26413c commit d362090
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 41 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"require-dev": {
"doctrine/coding-standard": "^8.0",
"guzzlehttp/psr7": "^1.5 || ^2.0",
"guzzlehttp/psr7": "^2.0",
"hansott/psr7-cookies": "^3.0.2",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^1",
Expand Down
5 changes: 2 additions & 3 deletions tests/FromCommunity/EmptyObjectValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;

use function GuzzleHttp\Psr7\stream_for;

final class EmptyObjectValidationTest extends TestCase
{
/**
Expand Down Expand Up @@ -40,7 +39,7 @@ public function testIssue57000(): void

$psrRequest = (new ServerRequest('post', 'http://localhost:8000/api/v1/products.create'))
->withHeader('Content-Type', 'application/json')
->withBody(stream_for('{}'));
->withBody(Utils::streamFor('{}'));

$validator->validate($psrRequest);

Expand Down
4 changes: 2 additions & 2 deletions tests/FromCommunity/Issue12Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

final class Issue12Test extends TestCase
Expand Down Expand Up @@ -61,7 +61,7 @@ public function testIssue12(?array $example): void

$psrRequest = (new ServerRequest('post', 'http://localhost:8000/api/v1/products.create'))
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode(['test' => $example])));
->withBody(Utils::streamFor(json_encode(['test' => $example])));

$validator->validate($psrRequest);

Expand Down
4 changes: 2 additions & 2 deletions tests/FromCommunity/Issue32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\Exception\ValidationFailed;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

final class Issue32Test extends TestCase
Expand Down Expand Up @@ -51,7 +51,7 @@ enum:

$psrRequest = (new ServerRequest('post', 'http://localhost:8000/api/test/create'))
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode($data)));
->withBody(Utils::streamFor(json_encode($data)));

$serverRequestValidator = (new ValidatorBuilder())->fromYaml($yaml)->getServerRequestValidator();

Expand Down
4 changes: 2 additions & 2 deletions tests/FromCommunity/Issue3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

final class Issue3Test extends TestCase
Expand Down Expand Up @@ -52,7 +52,7 @@ public function testIssue3(): void

$psrRequest = (new ServerRequest('post', 'http://localhost:8000/api/v1/products.create'))
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode(['test' => 20])));
->withBody(Utils::streamFor(json_encode(['test' => 20])));

$validator->validate($psrRequest);

Expand Down
4 changes: 2 additions & 2 deletions tests/FromCommunity/Issue50Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidBody;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use League\OpenAPIValidation\Schema\Exception\KeywordMismatch;
use PHPUnit\Framework\TestCase;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

final class Issue50Test extends TestCase
Expand Down Expand Up @@ -54,7 +54,7 @@ public function testIssue50(): void
$psrRequest = (new ServerRequest('post', 'http://localhost:8000/api/v1/products.create'))
->withHeader('Content-Type', 'application/json')
->withBody(
stream_for(
Utils::streamFor(
json_encode(
[
'body' =>
Expand Down
4 changes: 2 additions & 2 deletions tests/FromCommunity/Issue57Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

final class Issue57Test extends TestCase
Expand Down Expand Up @@ -60,7 +60,7 @@ public function testIssue57(): void

$psrRequest = (new ServerRequest('post', 'http://localhost:8000/api/v1/products.create'))
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode(['test' => (object) ['some_property_here' => (object) []]])));
->withBody(Utils::streamFor(json_encode(['test' => (object) ['some_property_here' => (object) []]])));

$validator->validate($psrRequest);

Expand Down
8 changes: 4 additions & 4 deletions tests/PSR7/BaseValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Psr7\Utils;
use HansOtt\PSR7Cookies\SetCookie;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;
use function sprintf;

Expand All @@ -33,7 +33,7 @@ protected function makeGoodResponse(string $path, string $method): ResponseInter
return (new Response())
->withHeader('Content-Type', 'application/json')
->withHeader('Header-B', 'good value')
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

case 'post /cookies':
$response = (new Response())
Expand Down Expand Up @@ -72,7 +72,7 @@ protected function makeGoodServerRequest(string $path, string $method): ServerRe

return $request
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

default:
throw new InvalidArgumentException(sprintf("unexpected operation '%s %s''", $method, $path));
Expand Down Expand Up @@ -104,7 +104,7 @@ protected function makeGoodRequest(string $path, string $method): RequestInterfa

return $request
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

default:
throw new InvalidArgumentException(sprintf("unexpected operation '%s %s''", $method, $path));
Expand Down
6 changes: 3 additions & 3 deletions tests/PSR7/CompleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

use const PHP_INT_MAX;
Expand Down Expand Up @@ -56,13 +56,13 @@ protected function buildGoodRequest(): ServerRequestInterface
->withCookieParams(['session_id' => 100])
->withHeader('X-RequestId', 'abcd')
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode(['propB' => 'good value'])));
->withBody(Utils::streamFor(json_encode(['propB' => 'good value'])));
}

protected function buildGoodResponse(): ResponseInterface
{
return (new Response())
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode(['propA' => PHP_INT_MAX])));
->withBody(Utils::streamFor(json_encode(['propA' => PHP_INT_MAX])));
}
}
6 changes: 3 additions & 3 deletions tests/PSR7/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace League\OpenAPIValidation\Tests\PSR7;

use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidBody;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidHeaders;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

final class RequestTest extends BaseValidatorTest
Expand All @@ -27,7 +27,7 @@ public function testItValidatesBodyGreen(): void
{
$body = ['name' => 'Alex'];
$request = $this->makeGoodRequest('/request-body', 'post')
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getRequestValidator();
$validator->validate($request);
Expand All @@ -39,7 +39,7 @@ public function testItValidatesBodyHasInvalidPayloadRed(): void
$addr = new OperationAddress('/request-body', 'post');
$body = ['name' => 1000];
$request = $this->makeGoodRequest($addr->path(), $addr->method())
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

$this->expectException(InvalidBody::class);
$this->expectExceptionMessage(
Expand Down
6 changes: 3 additions & 3 deletions tests/PSR7/RoutedServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace League\OpenAPIValidation\Tests\PSR7;

use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidBody;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidHeaders;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

final class RoutedServerRequestTest extends BaseValidatorTest
Expand All @@ -27,7 +27,7 @@ public function testItValidatesBodyGreen(): void
{
$body = ['name' => 'Alex'];
$request = $this->makeGoodServerRequest('/request-body', 'post')
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getRoutedRequestValidator();
$validator->validate(new OperationAddress('/request-body', 'post'), $request);
Expand All @@ -39,7 +39,7 @@ public function testItValidatesBodyHasInvalidPayloadRed(): void
$addr = new OperationAddress('/request-body', 'post');
$body = ['name' => 1000];
$request = $this->makeGoodServerRequest($addr->path(), $addr->method())
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

$this->expectException(InvalidBody::class);
$this->expectExceptionMessage(
Expand Down
6 changes: 3 additions & 3 deletions tests/PSR7/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace League\OpenAPIValidation\Tests\PSR7;

use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidBody;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidHeaders;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

final class ServerRequestTest extends BaseValidatorTest
Expand All @@ -27,7 +27,7 @@ public function testItValidatesBodyGreen(): void
{
$body = ['name' => 'Alex'];
$request = $this->makeGoodServerRequest('/request-body', 'post')
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getServerRequestValidator();
$validator->validate($request);
Expand All @@ -39,7 +39,7 @@ public function testItValidatesBodyHasInvalidPayloadRed(): void
$addr = new OperationAddress('/request-body', 'post');
$body = ['name' => 1000];
$request = $this->makeGoodServerRequest($addr->path(), $addr->method())
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

$this->expectException(InvalidBody::class);
$this->expectExceptionMessage(
Expand Down
8 changes: 4 additions & 4 deletions tests/PSR7/ValidateResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace League\OpenAPIValidation\Tests\PSR7;

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidBody;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidHeaders;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;

use function GuzzleHttp\Psr7\stream_for;
use function json_encode;

final class ValidateResponseTest extends BaseValidatorTest
Expand All @@ -32,7 +32,7 @@ public function testItValidatesMessageWithReferencesGreen(): void
];
$response = (new Response())
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode($body)));
->withBody(Utils::streamFor(json_encode($body)));

$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate(new OperationAddress('/ref', 'post'), $response);
Expand All @@ -43,7 +43,7 @@ public function testItValidatesBinaryResponseGreen(): void
{
$response = $this->makeGoodResponse('/path1', 'get')
->withHeader('Content-Type', 'image/jpeg')
->withBody(stream_for(__DIR__ . '/../stubs/image.jpg'));
->withBody(Utils::streamFor(__DIR__ . '/../stubs/image.jpg'));

$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate(new OperationAddress('/path1', 'get'), $response);
Expand All @@ -54,7 +54,7 @@ public function testItValidatesMessageWrongBodyValueRed(): void
{
$addr = new OperationAddress('/path1', 'get');
$body = [];
$response = $this->makeGoodResponse('/path1', 'get')->withBody(stream_for(json_encode($body)));
$response = $this->makeGoodResponse('/path1', 'get')->withBody(Utils::streamFor(json_encode($body)));

$this->expectException(InvalidBody::class);
$this->expectExceptionMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace League\OpenAPIValidation\Tests\PSR7\Validators\BodyValidator;

use GuzzleHttp\Psr7\Message;
use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\UploadedFile;
use GuzzleHttp\Psr7\Uri;
Expand All @@ -14,7 +15,6 @@
use Psr\Http\Message\UploadedFileInterface;

use function filesize;
use function GuzzleHttp\Psr7\parse_request;

class MultipartValidatorTest extends TestCase
{
Expand Down Expand Up @@ -388,7 +388,7 @@ public function testValidateMultipartGreen(string $message): void
{
$specFile = __DIR__ . '/../../../stubs/multipart.yaml';

$request = parse_request($message); // convert a text HTTP message to a PSR7 message
$request = Message::parseRequest($message); // convert a text HTTP message to a PSR7 message
$serverRequest = new ServerRequest(
$request->getMethod(),
$request->getUri(),
Expand All @@ -410,7 +410,7 @@ public function testValidateMultipartRed(string $message, string $expectedExcept

$specFile = __DIR__ . '/../../../stubs/multipart.yaml';

$request = parse_request($message); // convert a text HTTP message to a PSR7 message
$request = Message::parseRequest($message); // convert a text HTTP message to a PSR7 message
$serverRequest = new ServerRequest(
$request->getMethod(),
$request->getUri(),
Expand Down
Loading

0 comments on commit d362090

Please sign in to comment.