diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index 6c244355..024f928b 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -8,9 +8,11 @@ use Joomla\Application\WebApplicationInterface; use Joomla\Http\Http; +use Joomla\Http\Response; use Joomla\Input\Input; use Joomla\OAuth2\Client; use Joomla\Registry\Registry; +use Laminas\Diactoros\StreamFactory; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -33,7 +35,7 @@ class ClientTest extends TestCase * * @var Http|MockObject */ - protected $client; + protected $http; /** * The input object to use in retrieving GET/POST data. @@ -347,13 +349,11 @@ public function testRefreshTokenJson() */ public function encodedGrantOauthCallback($url, $data, array $headers = null, $timeout = null) { - $response = new \stdClass; - - $response->code = 200; - $response->headers = ['Content-Type' => 'x-www-form-urlencoded']; - $response->body = 'access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600'; - - return $response; + return new Response( + (new StreamFactory)->createStream('access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600'), + 200, + ['Content-Type' => 'x-www-form-urlencoded'] + ); } /** @@ -368,13 +368,11 @@ public function encodedGrantOauthCallback($url, $data, array $headers = null, $t */ public function jsonGrantOauthCallback($url, $data, array $headers = null, $timeout = null) { - $response = new \stdClass; - - $response->code = 200; - $response->headers = ['Content-Type' => 'application/json']; - $response->body = '{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}'; - - return $response; + return new Response( + (new StreamFactory)->createStream('{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}'), + 200, + ['CONTENT-TYPE' => 'application/json'] + ); } /** @@ -389,13 +387,11 @@ public function jsonGrantOauthCallback($url, $data, array $headers = null, $time */ public function queryOauthCallback($url, $data, array $headers = null, $timeout = null) { - $response = new \stdClass; - - $response->code = 200; - $response->headers = ['Content-Type' => 'text/html']; - $response->body = 'Lorem ipsum dolor sit amet.'; - - return $response; + return new Response( + (new StreamFactory)->createStream('Lorem ipsum dolor sit amet.'), + 200, + ['Content-Type' => 'text/html'] + ); } /** @@ -409,12 +405,10 @@ public function queryOauthCallback($url, $data, array $headers = null, $timeout */ public function getOauthCallback($url, array $headers = null, $timeout = null) { - $response = new \stdClass; - - $response->code = 200; - $response->headers = ['Content-Type' => 'text/html']; - $response->body = 'Lorem ipsum dolor sit amet.'; - - return $response; + return new Response( + (new StreamFactory)->createStream('Lorem ipsum dolor sit amet.'), + 200, + ['Content-Type' => ['text/html']] + ); } } diff --git a/composer.json b/composer.json index dd8cefb0..0d524540 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ }, "require-dev": { "joomla/coding-standards": "^3.0@dev", - "phpunit/phpunit": "^8.5|^9.0" + "phpunit/phpunit": "^8.5|^9.0", + "laminas/laminas-diactoros": "^2.0" }, "autoload": { "psr-4": { diff --git a/src/Client.php b/src/Client.php index 44a8146e..424cd8ce 100644 --- a/src/Client.php +++ b/src/Client.php @@ -101,25 +101,25 @@ public function authenticate() $response = $this->http->post($this->getOption('tokenurl'), $data); - if (!($response->code >= 200 && $response->code < 400)) + if (!($response->getStatusCode() >= 200 && $response->getStatusCode() < 400)) { throw new UnexpectedResponseException( $response, sprintf( 'Error code %s received requesting access token: %s.', - $response->code, - $response->body + $response->getStatusCode(), + (string) $response->getBody() ) ); } - if (strpos($response->headers['Content-Type'], 'application/json') !== false) + if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== false) { - $token = array_merge(json_decode($response->body, true), ['created' => time()]); + $token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]); } else { - parse_str($response->body, $token); + parse_str((string) $response->getBody(), $token); $token = array_merge($token, ['created' => time()]); } @@ -274,14 +274,14 @@ public function query($url, $data = null, $headers = [], $method = 'get', $timeo throw new \InvalidArgumentException('Unknown HTTP request method: ' . $method . '.'); } - if ($response->code < 200 || $response->code >= 400) + if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 400) { throw new UnexpectedResponseException( $response, sprintf( 'Error code %s received requesting data: %s.', - $response->code, - $response->body + $response->getStatusCode(), + (string) $response->getBody() ) ); } @@ -394,25 +394,25 @@ public function refreshToken($token = null) $response = $this->http->post($this->getOption('tokenurl'), $data); - if (!($response->code >= 200 || $response->code < 400)) + if (!($response->getStatusCode() >= 200 || $response->getStatusCode() < 400)) { throw new UnexpectedResponseException( $response, sprintf( 'Error code %s received refreshing token: %s.', - $response->code, - $response->body + $response->getStatusCode(), + (string) $response->getBody() ) ); } - if (strpos($response->headers['Content-Type'], 'application/json') !== false) + if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== false) { - $token = array_merge(json_decode($response->body, true), ['created' => time()]); + $token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]); } else { - parse_str($response->body, $token); + parse_str((string) $response->getBody(), $token); $token = array_merge($token, ['created' => time()]); }