From 94dd102908917db6afd7a9328753d5f91037db3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Heleno?= Date: Wed, 11 Dec 2024 20:27:45 -0300 Subject: [PATCH] Replace GuzzleHttp with PSR-18 HTTP Clients (#1032) * Replace GuzzleHttp with psr/http-client and psr/http-factory * Improve AbstractProvider code coverage --- composer.json | 5 +- src/Provider/AbstractProvider.php | 82 +++++++-- src/Tool/RequestFactory.php | 87 --------- test/src/Grant/GrantTestCase.php | 10 +- test/src/Provider/AbstractProviderTest.php | 195 ++++++++++++++------- test/src/Provider/Generic.php | 2 +- test/src/Provider/GenericProviderTest.php | 27 ++- test/src/Tool/RequestFactoryTest.php | 56 ------ 8 files changed, 228 insertions(+), 236 deletions(-) delete mode 100644 src/Tool/RequestFactory.php delete mode 100644 test/src/Tool/RequestFactoryTest.php diff --git a/composer.json b/composer.json index f6773fc1..69c0a6de 100644 --- a/composer.json +++ b/composer.json @@ -8,9 +8,12 @@ "require": { "php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0", "ext-json": "*", - "guzzlehttp/guzzle": "^7.4.5" + "psr/http-client": "^1.0", + "psr/http-factory": "^1.1", + "psr/http-message": "^2.0" }, "require-dev": { + "guzzlehttp/guzzle": "^7.4.5", "mockery/mockery": "^1.6", "php-parallel-lint/php-parallel-lint": "^1.4", "phpunit/phpunit": "^10.5 || ^11.5", diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 26f6d1c6..9c11181e 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -15,7 +15,6 @@ namespace League\OAuth2\Client\Provider; use GuzzleHttp\Client as HttpClient; -use GuzzleHttp\ClientInterface as HttpClientInterface; use GuzzleHttp\Exception\BadResponseException; use InvalidArgumentException; use League\OAuth2\Client\Grant\AbstractGrant; @@ -28,9 +27,11 @@ use League\OAuth2\Client\Tool\ArrayAccessorTrait; use League\OAuth2\Client\Tool\GuardedPropertyTrait; use League\OAuth2\Client\Tool\QueryBuilderTrait; -use League\OAuth2\Client\Tool\RequestFactory; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamFactoryInterface; use UnexpectedValueException; /** @@ -103,12 +104,17 @@ abstract class AbstractProvider protected $grantFactory; /** - * @var RequestFactory + * @var RequestFactoryInterface */ protected $requestFactory; /** - * @var HttpClientInterface + * @var StreamFactoryInterface + */ + protected $streamFactory; + + /** + * @var ClientInterface */ protected $httpClient; @@ -140,16 +146,17 @@ public function __construct(array $options = [], array $collaborators = []) $this->setGrantFactory($collaborators['grantFactory']); if (empty($collaborators['requestFactory'])) { - $collaborators['requestFactory'] = new RequestFactory(); + throw new InvalidArgumentException('No request factory set'); } $this->setRequestFactory($collaborators['requestFactory']); - if (empty($collaborators['httpClient'])) { - $client_options = $this->getAllowedClientOptions($options); + if (empty($collaborators['streamFactory'])) { + throw new InvalidArgumentException('No stream factory set'); + } + $this->setStreamFactory($collaborators['streamFactory']); - $collaborators['httpClient'] = new HttpClient( - array_intersect_key($options, array_flip($client_options)) - ); + if (empty($collaborators['httpClient'])) { + throw new InvalidArgumentException('No http client set'); } $this->setHttpClient($collaborators['httpClient']); @@ -205,10 +212,10 @@ public function getGrantFactory() /** * Sets the request factory instance. * - * @param RequestFactory $factory + * @param RequestFactoryInterface $factory * @return self */ - public function setRequestFactory(RequestFactory $factory) + public function setRequestFactory(RequestFactoryInterface $factory) { $this->requestFactory = $factory; @@ -218,20 +225,43 @@ public function setRequestFactory(RequestFactory $factory) /** * Returns the request factory instance. * - * @return RequestFactory + * @return RequestFactoryInterface */ public function getRequestFactory() { return $this->requestFactory; } + /** + * Sets the stream factory instance. + * + * @param StreamFactoryInterface $factory + * @return self + */ + public function setStreamFactory(StreamFactoryInterface $factory) + { + $this->streamFactory = $factory; + + return $this; + } + + /** + * Returns the stream factory instance. + * + * @return StreamFactoryInterface + */ + public function getStreamFactory() + { + return $this->streamFactory; + } + /** * Sets the HTTP client instance. * - * @param HttpClientInterface $client + * @param ClientInterface $client * @return self */ - public function setHttpClient(HttpClientInterface $client) + public function setHttpClient(ClientInterface $client) { $this->httpClient = $client; @@ -241,7 +271,7 @@ public function setHttpClient(HttpClientInterface $client) /** * Returns the HTTP client instance. * - * @return HttpClientInterface + * @return ClientInterface */ public function getHttpClient() { @@ -696,9 +726,23 @@ protected function createRequest($method, $url, $token, array $options) ]; $options = array_merge_recursive($defaults, $options); - $factory = $this->getRequestFactory(); + $requestFactory = $this->getRequestFactory(); + $streamFactory = $this->getStreamFactory(); + + $request = $requestFactory->createRequest($method, $url); + foreach ($options['headers'] as $name => $value) { + $request = $request->withAddedHeader($name, $value); + } + + $request = $request->withProtocolVersion($options['version'] ?? '1.1'); + + if (!empty($options['body'])) { + $request = $request->withBody( + $streamFactory->createStream($options['body'] ?? null) + ); + } - return $factory->getRequestWithOptions($method, $url, $options); + return $request; } /** @@ -712,7 +756,7 @@ protected function createRequest($method, $url, $token, array $options) */ public function getResponse(RequestInterface $request) { - return $this->getHttpClient()->send($request); + return $this->getHttpClient()->sendRequest($request); } /** diff --git a/src/Tool/RequestFactory.php b/src/Tool/RequestFactory.php deleted file mode 100644 index 1af43429..00000000 --- a/src/Tool/RequestFactory.php +++ /dev/null @@ -1,87 +0,0 @@ - - * @license http://opensource.org/licenses/MIT MIT - * @link http://thephpleague.com/oauth2-client/ Documentation - * @link https://packagist.org/packages/league/oauth2-client Packagist - * @link https://github.com/thephpleague/oauth2-client GitHub - */ - -namespace League\OAuth2\Client\Tool; - -use GuzzleHttp\Psr7\Request; - -/** - * Used to produce PSR-7 Request instances. - * - * @link https://github.com/guzzle/guzzle/pull/1101 - */ -class RequestFactory -{ - /** - * Creates a PSR-7 Request instance. - * - * @param null|string $method HTTP method for the request. - * @param null|string $uri URI for the request. - * @param array $headers Headers for the message. - * @param string|resource|StreamInterface $body Message body. - * @param string $version HTTP protocol version. - * - * @return Request - */ - public function getRequest( - $method, - $uri, - array $headers = [], - $body = null, - $version = '1.1' - ) { - return new Request($method, $uri, $headers, $body, $version); - } - - /** - * Parses simplified options. - * - * @param array $options Simplified options. - * - * @return array Extended options for use with getRequest. - */ - protected function parseOptions(array $options) - { - // Should match default values for getRequest - $defaults = [ - 'headers' => [], - 'body' => null, - 'version' => '1.1', - ]; - - return array_merge($defaults, $options); - } - - /** - * Creates a request using a simplified array of options. - * - * @param null|string $method - * @param null|string $uri - * @param array $options - * - * @return Request - */ - public function getRequestWithOptions($method, $uri, array $options = []) - { - $options = $this->parseOptions($options); - - return $this->getRequest( - $method, - $uri, - $options['headers'], - $options['body'], - $options['version'] - ); - } -} diff --git a/test/src/Grant/GrantTestCase.php b/test/src/Grant/GrantTestCase.php index e09e22de..00249cc0 100644 --- a/test/src/Grant/GrantTestCase.php +++ b/test/src/Grant/GrantTestCase.php @@ -2,11 +2,13 @@ namespace League\OAuth2\Client\Test\Grant; -use GuzzleHttp\ClientInterface; +use GuzzleHttp\Client; +use GuzzleHttp\Psr7\HttpFactory; use Mockery; use Mockery\MockInterface; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use Psr\Http\Client\ClientInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; use League\OAuth2\Client\Token\AccessTokenInterface; @@ -20,6 +22,10 @@ protected function getMockProvider() 'clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none', + ],[ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); } @@ -72,7 +78,7 @@ public function testGetAccessToken($grant, array $params = []) /** @var ClientInterface & MockInterface $client */ $client = Mockery::spy(ClientInterface::class)->makePartial(); $client - ->shouldReceive('send') + ->shouldReceive('sendRequest') ->once() ->withArgs(function ($request) { parse_str((string) $request->getBody(), $body); diff --git a/test/src/Provider/AbstractProviderTest.php b/test/src/Provider/AbstractProviderTest.php index 842e1fe0..f388c0ec 100644 --- a/test/src/Provider/AbstractProviderTest.php +++ b/test/src/Provider/AbstractProviderTest.php @@ -2,13 +2,15 @@ namespace League\OAuth2\Client\Test\Provider; +use InvalidArgumentException; use League\OAuth2\Client\OptionProvider\PostAuthOptionProvider; use Mockery; use PHPUnit\Framework\Attributes\DataProvider; use ReflectionClass; use UnexpectedValueException; use GuzzleHttp\Exception\BadResponseException; -use GuzzleHttp\ClientInterface; +use GuzzleHttp\Client; +use GuzzleHttp\Psr7\HttpFactory; use League\OAuth2\Client\Provider\AbstractProvider; use League\OAuth2\Client\Test\Provider\Fake as MockProvider; use League\OAuth2\Client\Grant\AbstractGrant; @@ -16,11 +18,13 @@ use League\OAuth2\Client\Grant\Exception\InvalidGrantException; use League\OAuth2\Client\Token\AccessToken; use League\OAuth2\Client\Token\AccessTokenInterface; -use League\OAuth2\Client\Tool\RequestFactory; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use PHPUnit\Framework\TestCase; +use Psr\Http\Client\ClientInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; class AbstractProviderTest extends TestCase @@ -31,6 +35,10 @@ protected function getMockProvider() 'clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none', + ], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); } @@ -55,6 +63,38 @@ public function testInvalidGrantObject() $this->getMockProvider()->getAccessToken($grant, ['invalid_parameter' => 'none']); } + public function testMissingRequestFactory() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('No request factory set'); + $provider = new Fake(); + } + + public function testMissingStreamFactory() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('No stream factory set'); + $provider = new Fake( + [], + [ + 'requestFactory' => new HttpFactory() + ] + ); + } + + public function testMissingHttpClient() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('No http client set'); + $provider = new Fake( + [], + [ + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() + ] + ); + } + public function testAuthorizationUrlStateParam() { $authUrl = $this->getMockProvider()->getAuthorizationUrl([ @@ -91,62 +131,27 @@ public function testConstructorSetsProperties() 'redirectUri' => 'http://example.org/redirect' ]; - $mockProvider = new MockProvider($options); + $mockProvider = new MockProvider($options, [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() + ]); $this->assertSame($options['clientId'], $mockProvider->getClientId()); $this->assertSame($options['clientSecret'], $mockProvider->getClientSecret()); $this->assertSame($options['redirectUri'], $mockProvider->getRedirectUri()); } - public function testConstructorSetsClientOptions() - { - $timeout = rand(100, 900); - - $mockProvider = new MockProvider(compact('timeout')); - - $config = $mockProvider->getHttpClient()->getConfig(); - - $this->assertArrayHasKey('timeout', $config); - $this->assertEquals($timeout, $config['timeout']); - } - - public function testCanSetAProxy() - { - $proxy = '192.168.0.1:8888'; - - $mockProvider = new MockProvider(['proxy' => $proxy]); - - $config = $mockProvider->getHttpClient()->getConfig(); - - $this->assertArrayHasKey('proxy', $config); - $this->assertEquals($proxy, $config['proxy']); - } - - public function testCannotDisableVerifyIfNoProxy() - { - $mockProvider = new MockProvider(['verify' => false]); - - $config = $mockProvider->getHttpClient()->getConfig(); - - $this->assertArrayHasKey('verify', $config); - $this->assertTrue($config['verify']); - } - - public function testCanDisableVerificationIfThereIsAProxy() - { - $mockProvider = new MockProvider(['proxy' => '192.168.0.1:8888', 'verify' => false]); - - $config = $mockProvider->getHttpClient()->getConfig(); - - $this->assertArrayHasKey('verify', $config); - $this->assertFalse($config['verify']); - } - public function testConstructorSetsGrantFactory() { $mockAdapter = Mockery::mock(GrantFactory::class); - $mockProvider = new MockProvider([], ['grantFactory' => $mockAdapter]); + $mockProvider = new MockProvider([], [ + 'grantFactory' => $mockAdapter, + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() + ]); $this->assertSame($mockAdapter, $mockProvider->getGrantFactory()); } @@ -154,18 +159,38 @@ public function testConstructorSetsHttpAdapter() { $mockAdapter = Mockery::mock(ClientInterface::class); - $mockProvider = new MockProvider([], ['httpClient' => $mockAdapter]); + $mockProvider = new MockProvider([], [ + 'httpClient' => $mockAdapter, + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() + ]); $this->assertSame($mockAdapter, $mockProvider->getHttpClient()); } public function testConstructorSetsRequestFactory() { - $mockAdapter = Mockery::mock(RequestFactory::class); + $mockAdapter = Mockery::mock(RequestFactoryInterface::class); - $mockProvider = new MockProvider([], ['requestFactory' => $mockAdapter]); + $mockProvider = new MockProvider([], [ + 'httpClient' => new Client(), + 'requestFactory' => $mockAdapter, + 'streamFactory' => new HttpFactory() + ]); $this->assertSame($mockAdapter, $mockProvider->getRequestFactory()); } + public function testConstructorSetsStreamFactory() + { + $mockAdapter = Mockery::mock(StreamFactoryInterface::class); + + $mockProvider = new MockProvider([], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => $mockAdapter + ]); + $this->assertSame($mockAdapter, $mockProvider->getStreamFactory()); + } + public function testSetRedirectHandler() { $testFunction = false; @@ -192,6 +217,10 @@ public function testGetUserProperties($name = null, $email = null, $id = null) 'clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none', + ], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); $token = new AccessToken(['access_token' => 'abc', 'expires_in' => 3600]); @@ -219,7 +248,7 @@ public function testGetUserProperties($name = null, $email = null, $id = null) $client = Mockery::spy(ClientInterface::class, [ - 'send' => $response, + 'sendRequest' => $response, ]); $provider->setHttpClient($client); @@ -234,7 +263,7 @@ public function testGetUserProperties($name = null, $email = null, $id = null) $this->assertArrayHasKey('email', $user->toArray()); $client - ->shouldHaveReceived('send') + ->shouldHaveReceived('sendRequest') ->once() ->withArgs(function ($request) use ($url) { return $request->getMethod() === 'GET' @@ -249,6 +278,10 @@ public function testGetUserPropertiesThrowsExceptionWhenNonJsonResponseIsReceive 'clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none', + ], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); $token = new AccessToken(['access_token' => 'abc', 'expires_in' => 3600]); @@ -267,7 +300,7 @@ public function testGetUserPropertiesThrowsExceptionWhenNonJsonResponseIsReceive ->andReturn(['text/html']); $client = Mockery::mock(ClientInterface::class, [ - 'send' => $response, + 'sendRequest' => $response, ]); $provider->setHttpClient($client); @@ -384,7 +417,7 @@ public function testPkceMethod($pkceMethod, $pkceCode, $expectedChallenge) ->andReturn(['application/json']); $client = Mockery::spy(ClientInterface::class, [ - 'send' => $response, + 'sendRequest' => $response, ]); $provider->setHttpClient($client); @@ -394,7 +427,7 @@ public function testPkceMethod($pkceMethod, $pkceCode, $expectedChallenge) $provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); $client - ->shouldHaveReceived('send') + ->shouldHaveReceived('sendRequest') ->once() ->withArgs(function ($request) use ($pkceCode) { parse_str((string)$request->getBody(), $body); @@ -457,6 +490,10 @@ public function testErrorResponsesCanBeCustomizedAtTheProvider() 'clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none', + ], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); $error = ["error" => "Foo error", "code" => 1337]; @@ -480,7 +517,7 @@ public function testErrorResponsesCanBeCustomizedAtTheProvider() ->andReturn(['application/json']); $client = Mockery::spy(ClientInterface::class, [ - 'send' => $response, + 'sendRequest' => $response, ]); $provider->setHttpClient($client); @@ -505,7 +542,7 @@ public function testErrorResponsesCanBeCustomizedAtTheProvider() $this->assertEquals($error, $errorBody); $client - ->shouldHaveReceived('send') + ->shouldHaveReceived('sendRequest') ->once() ->withArgs(function ($request) use ($method, $url) { return $request->getMethod() === $method @@ -520,6 +557,10 @@ public function testClientErrorTriggersProviderException() 'clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none', + ], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); $stream = Mockery::mock(StreamInterface::class, [ @@ -539,7 +580,7 @@ public function testClientErrorTriggersProviderException() $client = Mockery::mock(ClientInterface::class); $client - ->shouldReceive('send') + ->shouldReceive('sendRequest') ->andThrow(new BadResponseException('test exception', $request, $response)); $provider->setHttpClient($client); @@ -548,14 +589,18 @@ public function testClientErrorTriggersProviderException() public function testGetResponse() { - $provider = new MockProvider(); + $provider = new MockProvider([], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() + ]); $request = Mockery::mock(RequestInterface::class); $response = Mockery::mock(ResponseInterface::class); $client = Mockery::mock(ClientInterface::class); $client - ->shouldReceive('send') + ->shouldReceive('sendRequest') ->with($request) ->andReturn($response); @@ -567,7 +612,11 @@ public function testGetResponse() public function testAuthenticatedRequestAndResponse() { - $provider = new MockProvider(); + $provider = new MockProvider([], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() + ]); $token = new AccessToken(['access_token' => 'abc', 'expires_in' => 3600]); $request = $provider->getAuthenticatedRequest('get', 'https://api.example.com/v1/test', $token); @@ -586,7 +635,7 @@ public function testAuthenticatedRequestAndResponse() $client = Mockery::mock(ClientInterface::class); $client - ->shouldReceive('send') + ->shouldReceive('sendRequest') ->with($request) ->andReturn($response); @@ -620,6 +669,10 @@ public function testGetAccessToken($method) 'clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none', + ], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); $provider->setAccessTokenMethod($method); @@ -654,7 +707,7 @@ public function testGetAccessToken($method) ->andReturn(['application/json']); $client = Mockery::spy(ClientInterface::class, [ - 'send' => $response, + 'sendRequest' => $response, ]); $provider->setHttpClient($client); @@ -667,7 +720,7 @@ public function testGetAccessToken($method) $this->assertSame($raw_response['expires'], $token->getExpires()); $client - ->shouldHaveReceived('send') + ->shouldHaveReceived('sendRequest') ->once() ->withArgs(function ($request) use ($provider) { return $request->getMethod() === $provider->getAccessTokenMethod() @@ -693,7 +746,7 @@ public function testGetAccessTokenWithNonJsonResponse() ->andReturn(['text/plain']); $client = Mockery::mock(ClientInterface::class, [ - 'send' => $response, + 'sendRequest' => $response, ]); $provider->setHttpClient($client); @@ -832,7 +885,11 @@ public function testGuardedProperties() 'guarded' => 'foo' ]; - $provider = new Fake\ProviderWithGuardedProperties($options); + $provider = new Fake\ProviderWithGuardedProperties($options, [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() + ]); $this->assertNotEquals( $options['skipMeDuringMassAssignment'], @@ -916,6 +973,10 @@ public function testExtendedProviderDoesNotErrorWhenUsingAccessTokenAsTheTypeHin 'urlAuthorize' => 'https://example.com/authorize', 'urlAccessToken' => 'https://example.com/accessToken', 'urlResourceOwnerDetails' => 'https://api.example.com/owner', + ], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); $reflectedProvider = new \ReflectionObject($provider); diff --git a/test/src/Provider/Generic.php b/test/src/Provider/Generic.php index 9701de3c..002e3605 100644 --- a/test/src/Provider/Generic.php +++ b/test/src/Provider/Generic.php @@ -16,7 +16,7 @@ public function __construct($options = [], array $collaborators = []) 'redirectUri' => 'none', ]; - parent::__construct($options); + parent::__construct($options, $collaborators); } protected function fetchResourceOwnerDetails(AccessToken $token) diff --git a/test/src/Provider/GenericProviderTest.php b/test/src/Provider/GenericProviderTest.php index 52ce2bc2..01e577f0 100644 --- a/test/src/Provider/GenericProviderTest.php +++ b/test/src/Provider/GenericProviderTest.php @@ -3,6 +3,8 @@ namespace League\OAuth2\Client\Test\Provider; use InvalidArgumentException; +use GuzzleHttp\Client; +use GuzzleHttp\Psr7\HttpFactory; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Test\Provider\Generic as MockProvider; use League\OAuth2\Client\Provider\GenericProvider; @@ -39,7 +41,10 @@ public function testRequiredOptions() } } - $provider = new GenericProvider($required + [ + $provider = new GenericProvider($required, [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); } @@ -63,6 +68,10 @@ public function testConfigurableOptions() 'clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none', + ], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); foreach ($options as $key => $expected) { @@ -105,6 +114,10 @@ public function testResourceOwnerDetails() 'urlAccessToken' => 'http://example.com/token', 'urlResourceOwnerDetails' => 'http://example.com/user', 'responseResourceOwnerId' => 'mock_response_uid', + ], [ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() ]); $user = $provider->getResourceOwner($token); @@ -130,7 +143,11 @@ public function testCheckResponse() 'urlResourceOwnerDetails' => 'http://example.com/user', ]; - $provider = new GenericProvider($options); + $provider = new GenericProvider($options,[ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() + ]); $reflection = new ReflectionClass(get_class($provider)); @@ -157,7 +174,11 @@ public function testCheckResponseThrowsException(array $error, array $extraOptio 'urlResourceOwnerDetails' => 'http://example.com/user', ]; - $provider = new GenericProvider($options + $extraOptions); + $provider = new GenericProvider($options + $extraOptions,[ + 'httpClient' => new Client(), + 'requestFactory' => new HttpFactory(), + 'streamFactory' => new HttpFactory() + ]); $reflection = new ReflectionClass(get_class($provider)); diff --git a/test/src/Tool/RequestFactoryTest.php b/test/src/Tool/RequestFactoryTest.php deleted file mode 100644 index 8a9dbc7c..00000000 --- a/test/src/Tool/RequestFactoryTest.php +++ /dev/null @@ -1,56 +0,0 @@ -getRequest($method, $uri); - - $this->assertInstanceOf(RequestInterface::class, $request); - $this->assertSame(strtoupper($method), $request->getMethod()); - $this->assertSame($uri, (string) $request->getUri()); - - $headers = ['X-Test' => 'Foo']; - $body = 'test body'; - $protocolVersion = '1.0'; - - $request = $factory->getRequest($method, $uri, $headers, $body, $protocolVersion); - - $this->assertTrue($request->hasHeader('X-Test')); - $this->assertSame($body, (string) $request->getBody()); - $this->assertSame($protocolVersion, $request->getProtocolVersion()); - } - - public function testGetRequestWithOptions() - { - $method = 'head'; - $uri = '/test/options'; - - $factory = new RequestFactory(); - $request = $factory->getRequestWithOptions($method, $uri); - - $this->assertInstanceOf(RequestInterface::class, $request); - $this->assertSame(strtoupper($method), $request->getMethod()); - $this->assertSame($uri, (string) $request->getUri()); - - $options = [ - 'body' => 'another=test&form=body', - 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], - ]; - - $request = $factory->getRequestWithOptions($method, $uri, $options); - - $this->assertContains($options['headers']['Content-Type'], $request->getHeader('Content-Type')); - $this->assertSame($options['body'], (string) $request->getBody()); - } -}