From f322f6a819b0b7da35b1ca8860403602bb0acd78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bour?= Date: Tue, 8 Dec 2020 13:10:01 +0100 Subject: [PATCH 1/5] chore: bump to php 7.4, guzzle 7, phpunit 9 --- .travis.yml | 8 ++------ composer.json | 12 +++++++----- phpunit.xml.dist | 34 +++++++++++++++++----------------- tests/ApiFunctionalTest.php | 8 +++++--- tests/ApiTest.php | 24 ++++++++++++------------ 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6038996..279cd4b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,14 +5,10 @@ addons: language: php php: - - 7.3 - - 7.2 - - 7.1 - - 7.0 - - 5.6 + - 7.4 before_script: - composer self-update - composer install -script: vendor/bin/phing test -Donly.units=true +script: vendor/bin/phpunit diff --git a/composer.json b/composer.json index 823c769..5b4ad1b 100644 --- a/composer.json +++ b/composer.json @@ -3,15 +3,17 @@ "description": "Wrapper for OVH APIs", "license": "BSD-3-Clause", "require": { - "guzzlehttp/guzzle": "^6.0" + "php": ">=7.3", + "guzzlehttp/guzzle": "^6.0||^7.0" }, "autoload": { "psr-4": {"Ovh\\": "src/"} }, "require-dev": { - "phpunit/phpunit": "4.*", - "phpdocumentor/phpdocumentor": "2.*", - "squizlabs/php_codesniffer": "2.*", - "phing/phing": "^2.14" + "phpunit/phpunit": "^9.5", + "phpdocumentor/phpdocumentor": "v3.0.0", + "squizlabs/php_codesniffer": "^3.5", + "phpdocumentor/graphviz": "^2.0@dev", + "symfony/flex": "^1.11" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index aa6b117..0f9e1cf 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,19 @@ - - - - tests - - - - - - - - - - - src - - + + + + src + + + + + tests + + + + + + + + diff --git a/tests/ApiFunctionalTest.php b/tests/ApiFunctionalTest.php index 59d3954..512509c 100644 --- a/tests/ApiFunctionalTest.php +++ b/tests/ApiFunctionalTest.php @@ -28,7 +28,9 @@ namespace Ovh\tests; use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; use Ovh\Api; +use PHPUnit\Framework\TestCase; /** * Functional tests of Api class @@ -36,7 +38,7 @@ use Ovh\Api; * @package Ovh * @category Ovh */ -class ApiFunctionalTest extends \PHPUnit_Framework_TestCase +class ApiFunctionalTest extends TestCase { /** @@ -82,7 +84,7 @@ class ApiFunctionalTest extends \PHPUnit_Framework_TestCase /** * Define id to create object */ - protected function setUp() + protected function setUp() :void { $this->application_key = getenv('APP_KEY'); $this->application_secret = getenv('APP_SECRET'); @@ -238,7 +240,7 @@ class ApiFunctionalTest extends \PHPUnit_Framework_TestCase */ public function testApiGetWithParameters() { - $this->setExpectedException('\\GuzzleHttp\\Exception\\ClientException', '400'); + $this->expectException(ClientException::class); $this->api->get('/me/accessRestriction/ip', ['foo' => 'bar']); } diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 970fb6d..fada6ae 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -28,10 +28,13 @@ namespace Ovh\tests; use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Request; use Ovh\Api; +use Ovh\Exceptions\InvalidParameterException; +use PHPUnit\Framework\TestCase; /** * Test Api class @@ -39,7 +42,7 @@ use Ovh\Api; * @package Ovh * @category Ovh */ -class ApiTest extends \PHPUnit_Framework_TestCase +class ApiTest extends TestCase { /** * @var Client @@ -69,7 +72,7 @@ class ApiTest extends \PHPUnit_Framework_TestCase /** * Define id to create object */ - protected function setUp() + protected function setUp() :void { $this->application_key = 'app_key'; $this->application_secret = 'app_secret'; @@ -116,7 +119,7 @@ class ApiTest extends \PHPUnit_Framework_TestCase */ public function testMissingApplicationKey() { - $this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Application key'); + $this->expectException(InvalidParameterException::class); $api = new Api(null, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $api->get('/me'); } @@ -126,7 +129,7 @@ class ApiTest extends \PHPUnit_Framework_TestCase */ public function testMissingApplicationSecret() { - $this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Application secret'); + $this->expectException(InvalidParameterException::class); $api = new Api($this->application_key, null, $this->endpoint, $this->consumer_key, $this->client); $api->get('/me'); } @@ -157,7 +160,7 @@ class ApiTest extends \PHPUnit_Framework_TestCase */ public function testMissingApiEndpoint() { - $this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Endpoint'); + $this->expectException(InvalidParameterException::class); new Api($this->application_key, $this->application_secret, null, $this->consumer_key, $this->client); } @@ -166,7 +169,7 @@ class ApiTest extends \PHPUnit_Framework_TestCase */ public function testBadApiEndpoint() { - $this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Unknown'); + $this->expectException(InvalidParameterException::class); new Api($this->application_key, $this->application_secret, 'i_am_invalid', $this->consumer_key, $this->client); } @@ -243,9 +246,8 @@ class ApiTest extends \PHPUnit_Framework_TestCase */ public function testInvalidApplicationKey() { - $this->setExpectedException( - '\GuzzleHttp\Exception\ClientException' - ); + + $this->expectException(ClientException::class); $handlerStack = $this->client->getConfig('handler'); $handlerStack->push(Middleware::mapResponse(function (Response $response) { @@ -276,9 +278,7 @@ class ApiTest extends \PHPUnit_Framework_TestCase */ public function testInvalidRight() { - $this->setExpectedException( - '\GuzzleHttp\Exception\ClientException' - ); + $this->expectException(ClientException::class); $handlerStack = $this->client->getConfig('handler'); $handlerStack->push(Middleware::mapResponse(function (Response $response) { From 6bfd3cac336317af3b6709649e6f0e17f5d29b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bour?= Date: Tue, 8 Dec 2020 13:14:50 +0100 Subject: [PATCH 2/5] style: psr2 --- src/Api.php | 49 ++++++++------------ src/Exceptions/ApiException.php | 1 - src/Exceptions/InvalidParameterException.php | 1 - src/Exceptions/NotLoggedException.php | 1 - 4 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/Api.php b/src/Api.php index 77b189e..bc69d2f 100644 --- a/src/Api.php +++ b/src/Api.php @@ -130,19 +130,14 @@ class Api throw new Exceptions\InvalidParameterException("Endpoint parameter is empty"); } - if (preg_match('/^https?:\/\/..*/',$api_endpoint)) - { - $this->endpoint = $api_endpoint; - } - else - { - if (!array_key_exists($api_endpoint, $this->endpoints)) { - throw new Exceptions\InvalidParameterException("Unknown provided endpoint"); - } - else - { - $this->endpoint = $this->endpoints[$api_endpoint]; - } + if (preg_match('/^https?:\/\/..*/', $api_endpoint)) { + $this->endpoint = $api_endpoint; + } else { + if (!array_key_exists($api_endpoint, $this->endpoints)) { + throw new Exceptions\InvalidParameterException("Unknown provided endpoint"); + } else { + $this->endpoint = $this->endpoints[$api_endpoint]; + } } if (!isset($http_client)) { @@ -228,8 +223,7 @@ class Api */ protected function rawCall($method, $path, $content = null, $is_authenticated = true, $headers = null) { - if ( $is_authenticated ) - { + if ($is_authenticated) { if (!isset($this->application_key)) { throw new Exceptions\InvalidParameterException("Application key parameter is empty"); } @@ -276,14 +270,12 @@ class Api } else { $body = ""; } - if(!is_array($headers)) - { + if (!is_array($headers)) { $headers = []; } $headers['Content-Type'] = 'application/json; charset=utf-8'; if ($is_authenticated) { - $headers['X-Ovh-Application'] = $this->application_key; if (!isset($this->time_delta)) { @@ -331,18 +323,15 @@ class Api */ public function get($path, $content = null, $headers = null, $is_authenticated = true) { - if(preg_match('/^\/[^\/]+\.json$/', $path)) - { - // Schema description must be access without authentication - return $this->decodeResponse( - $this->rawCall("GET", $path, $content, false, $headers) - ); - } - else - { - return $this->decodeResponse( - $this->rawCall("GET", $path, $content, $is_authenticated, $headers) - ); + if (preg_match('/^\/[^\/]+\.json$/', $path)) { + // Schema description must be access without authentication + return $this->decodeResponse( + $this->rawCall("GET", $path, $content, false, $headers) + ); + } else { + return $this->decodeResponse( + $this->rawCall("GET", $path, $content, $is_authenticated, $headers) + ); } } diff --git a/src/Exceptions/ApiException.php b/src/Exceptions/ApiException.php index c2b866a..4c8d2e2 100644 --- a/src/Exceptions/ApiException.php +++ b/src/Exceptions/ApiException.php @@ -43,5 +43,4 @@ use Exception; */ class ApiException extends Exception { - } diff --git a/src/Exceptions/InvalidParameterException.php b/src/Exceptions/InvalidParameterException.php index 78902f9..6df582b 100644 --- a/src/Exceptions/InvalidParameterException.php +++ b/src/Exceptions/InvalidParameterException.php @@ -43,5 +43,4 @@ use Exception; */ class InvalidParameterException extends Exception { - } diff --git a/src/Exceptions/NotLoggedException.php b/src/Exceptions/NotLoggedException.php index 23b2f6d..6384fc5 100644 --- a/src/Exceptions/NotLoggedException.php +++ b/src/Exceptions/NotLoggedException.php @@ -42,5 +42,4 @@ use Exception; */ class NotLoggedException extends Exception { - } From c042884a32e9fb72f36ef2f4aa4b623966078dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bour?= Date: Tue, 8 Dec 2020 13:22:03 +0100 Subject: [PATCH 3/5] refactor: hint and simplify if/else --- src/Api.php | 85 +++++++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/src/Api.php b/src/Api.php index bc69d2f..c1e61bd 100644 --- a/src/Api.php +++ b/src/Api.php @@ -31,8 +31,11 @@ namespace Ovh; use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; +use Psr\Http\Message\ResponseInterface; /** * Wrapper to manage login and exchanges with simpliest Ovh API @@ -68,42 +71,42 @@ class Api * * @var string */ - private $endpoint = null; + private ?string $endpoint; /** * Contain key of the current application * * @var string */ - private $application_key = null; + private ?string $application_key; /** * Contain secret of the current application * * @var string */ - private $application_secret = null; + private ?string $application_secret; /** * Contain consumer key of the current application * * @var string */ - private $consumer_key = null; + private ?string $consumer_key; /** * Contain delta between local timestamp and api server timestamp * * @var string */ - private $time_delta = null; + private ?string $time_delta; /** * Contain http client connection * * @var Client */ - private $http_client = null; + private ?Client $http_client; /** * Construct a new wrapper instance @@ -135,9 +138,9 @@ class Api } else { if (!array_key_exists($api_endpoint, $this->endpoints)) { throw new Exceptions\InvalidParameterException("Unknown provided endpoint"); - } else { - $this->endpoint = $this->endpoints[$api_endpoint]; } + + $this->endpoint = $this->endpoints[$api_endpoint]; } if (!isset($http_client)) { @@ -151,13 +154,12 @@ class Api $this->application_secret = $application_secret; $this->http_client = $http_client; $this->consumer_key = $consumer_key; - $this->time_delta = null; } /** * Calculate time delta between local machine and API's server * - * @throws \GuzzleHttp\Exception\ClientException if http request is an error + * @throws ClientException if http request is an error * @return int */ private function calculateTimeDelta() @@ -184,7 +186,7 @@ class Api * @param string $redirection url to redirect on your website after authentication * * @return mixed - * @throws \GuzzleHttp\Exception\ClientException if http request is an error + * @throws ClientException if http request is an error */ public function requestCredentials( array $accessRules, @@ -213,15 +215,18 @@ class Api * This is the main method of this wrapper. It will * sign a given query and return its result. * - * @param string $method HTTP method of request (GET,POST,PUT,DELETE) - * @param string $path relative url of API request - * @param \stdClass|array|null $content body of the request - * @param bool $is_authenticated if the request use authentication + * @param string $method HTTP method of request (GET,POST,PUT,DELETE) + * @param string $path relative url of API request + * @param \stdClass|array|null $content body of the request + * @param bool $is_authenticated if the request use authentication * - * @return array - * @throws \GuzzleHttp\Exception\ClientException if http request is an error + * @param null $headers + * @return ResponseInterface + * @throws Exceptions\InvalidParameterException + * @throws GuzzleException + * @throws \JsonException */ - protected function rawCall($method, $path, $content = null, $is_authenticated = true, $headers = null) + protected function rawCall($method, $path, $content = null, $is_authenticated = true, $headers = null): ResponseInterface { if ($is_authenticated) { if (!isset($this->application_key)) { @@ -235,7 +240,7 @@ class Api $url = $this->endpoint . $path; $request = new Request($method, $url); - if (isset($content) && $method == 'GET') { + if (isset($content) && $method === 'GET') { $query_string = $request->getUri()->getQuery(); $query = array(); @@ -264,7 +269,7 @@ class Api $request = $request->withUri($url); $body = ""; } elseif (isset($content)) { - $body = json_encode($content, JSON_UNESCAPED_SLASHES); + $body = json_encode($content, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES); $request->getBody()->write($body); } else { @@ -301,38 +306,40 @@ class Api /** * Decode a Response object body to an Array * - * @param Response $response + * @param Response $response * * @return array + * @throws \JsonException */ - private function decodeResponse(Response $response) + private function decodeResponse(Response $response): array { - return json_decode($response->getBody(), true); + return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR); } /** * Wrap call to Ovh APIs for GET requests * - * @param string $path path ask inside api - * @param array $content content to send inside body of request + * @param string $path path ask inside api + * @param array $content content to send inside body of request * @param array headers custom HTTP headers to add on the request * @param bool is_authenticated if the request need to be authenticated * * @return array - * @throws \GuzzleHttp\Exception\ClientException if http request is an error + * @throws ClientException if http request is an error + * @throws \JsonException */ - public function get($path, $content = null, $headers = null, $is_authenticated = true) + public function get($path, $content = null, $headers = null, $is_authenticated = true): array { if (preg_match('/^\/[^\/]+\.json$/', $path)) { // Schema description must be access without authentication return $this->decodeResponse( $this->rawCall("GET", $path, $content, false, $headers) ); - } else { - return $this->decodeResponse( - $this->rawCall("GET", $path, $content, $is_authenticated, $headers) - ); } + + return $this->decodeResponse( + $this->rawCall("GET", $path, $content, $is_authenticated, $headers) + ); } /** @@ -344,9 +351,9 @@ class Api * @param bool is_authenticated if the request need to be authenticated * * @return array - * @throws \GuzzleHttp\Exception\ClientException if http request is an error + * @throws ClientException if http request is an error */ - public function post($path, $content = null, $headers = null, $is_authenticated = true) + public function post($path, $content = null, $headers = null, $is_authenticated = true): array { return $this->decodeResponse( $this->rawCall("POST", $path, $content, $is_authenticated, $headers) @@ -362,9 +369,9 @@ class Api * @param bool is_authenticated if the request need to be authenticated * * @return array - * @throws \GuzzleHttp\Exception\ClientException if http request is an error + * @throws ClientException if http request is an error */ - public function put($path, $content, $headers = null, $is_authenticated = true) + public function put($path, $content, $headers = null, $is_authenticated = true): array { return $this->decodeResponse( $this->rawCall("PUT", $path, $content, $is_authenticated, $headers) @@ -380,9 +387,9 @@ class Api * @param bool is_authenticated if the request need to be authenticated * * @return array - * @throws \GuzzleHttp\Exception\ClientException if http request is an error + * @throws ClientException if http request is an error */ - public function delete($path, $content = null, $headers = null, $is_authenticated = true) + public function delete($path, $content = null, $headers = null, $is_authenticated = true): array { return $this->decodeResponse( $this->rawCall("DELETE", $path, $content, $is_authenticated, $headers) @@ -392,7 +399,7 @@ class Api /** * Get the current consumer key */ - public function getConsumerKey() + public function getConsumerKey(): ?string { return $this->consumer_key; } @@ -400,7 +407,7 @@ class Api /** * Return instance of http client */ - public function getHttpClient() + public function getHttpClient(): ?Client { return $this->http_client; } From 3dd7f3ecbe4aacabf9a68dacf61bbfc628963cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bour?= Date: Tue, 8 Dec 2020 13:22:21 +0100 Subject: [PATCH 4/5] chore: bump to php 7.4 and add ext-json --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 5b4ad1b..1b00e64 100644 --- a/composer.json +++ b/composer.json @@ -3,8 +3,9 @@ "description": "Wrapper for OVH APIs", "license": "BSD-3-Clause", "require": { - "php": ">=7.3", - "guzzlehttp/guzzle": "^6.0||^7.0" + "php": ">=7.4", + "guzzlehttp/guzzle": "^6.0||^7.0", + "ext-json": "*" }, "autoload": { "psr-4": {"Ovh\\": "src/"} From 951e4adedbc15979c34111c118913589d046f24e Mon Sep 17 00:00:00 2001 From: Romain Beuque Date: Tue, 19 Jan 2021 19:43:10 +0000 Subject: [PATCH 5/5] fix: tests now pass with PHPUnit and PHP7.4 Fixes unit tests for #106 --- .travis.yml | 2 +- src/Api.php | 15 +++---- tests/ApiTest.php | 110 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 83 insertions(+), 44 deletions(-) diff --git a/.travis.yml b/.travis.yml index 279cd4b..fbad42e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,4 +11,4 @@ before_script: - composer self-update - composer install -script: vendor/bin/phpunit +script: vendor/bin/phpunit tests/ApiTest.php diff --git a/src/Api.php b/src/Api.php index c1e61bd..0286b98 100644 --- a/src/Api.php +++ b/src/Api.php @@ -308,10 +308,9 @@ class Api * * @param Response $response * - * @return array * @throws \JsonException */ - private function decodeResponse(Response $response): array + private function decodeResponse(Response $response) { return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR); } @@ -324,11 +323,10 @@ class Api * @param array headers custom HTTP headers to add on the request * @param bool is_authenticated if the request need to be authenticated * - * @return array * @throws ClientException if http request is an error * @throws \JsonException */ - public function get($path, $content = null, $headers = null, $is_authenticated = true): array + public function get($path, $content = null, $headers = null, $is_authenticated = true) { if (preg_match('/^\/[^\/]+\.json$/', $path)) { // Schema description must be access without authentication @@ -350,10 +348,9 @@ class Api * @param array headers custom HTTP headers to add on the request * @param bool is_authenticated if the request need to be authenticated * - * @return array * @throws ClientException if http request is an error */ - public function post($path, $content = null, $headers = null, $is_authenticated = true): array + public function post($path, $content = null, $headers = null, $is_authenticated = true) { return $this->decodeResponse( $this->rawCall("POST", $path, $content, $is_authenticated, $headers) @@ -368,10 +365,9 @@ class Api * @param array headers custom HTTP headers to add on the request * @param bool is_authenticated if the request need to be authenticated * - * @return array * @throws ClientException if http request is an error */ - public function put($path, $content, $headers = null, $is_authenticated = true): array + public function put($path, $content, $headers = null, $is_authenticated = true) { return $this->decodeResponse( $this->rawCall("PUT", $path, $content, $is_authenticated, $headers) @@ -386,10 +382,9 @@ class Api * @param array headers custom HTTP headers to add on the request * @param bool is_authenticated if the request need to be authenticated * - * @return array * @throws ClientException if http request is an error */ - public function delete($path, $content = null, $headers = null, $is_authenticated = true): array + public function delete($path, $content = null, $headers = null, $is_authenticated = true) { return $this->decodeResponse( $this->rawCall("DELETE", $path, $content, $is_authenticated, $headers) diff --git a/tests/ApiTest.php b/tests/ApiTest.php index fada6ae..fbd6aea 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -30,6 +30,7 @@ namespace Ovh\tests; use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Middleware; +use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Request; use Ovh\Api; @@ -145,14 +146,19 @@ class ApiTest extends TestCase return $request; } - $request = $request->withUri($request->getUri() - ->withHost('httpbin.org') - ->withPath('/') - ->withQuery('')); - return $request; + return null; + })); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { + $body = Psr7\Utils::streamFor('{}'); + + return $response + ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($body); })); $api = new Api(NULL, NULL, $this->endpoint, $this->consumer_key, $this->client); - $api->get('/1.0/unauthcall', null, null, false); + $api->get('/unauthcall', null, null, false); + $this->assertEquals(1, 1); } /** @@ -193,11 +199,11 @@ class ApiTest extends TestCase $handlerStack = $this->client->getConfig('handler'); $handlerStack->push(Middleware::mapResponse(function (Response $response) { - $body = $response->getBody(); - $body->write(time() - 10); + $body = Psr7\Utils::streamFor(time() - 10); return $response ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') ->withBody($body); })); @@ -220,16 +226,19 @@ class ApiTest extends TestCase $handlerStack = $this->client->getConfig('handler'); $handlerStack->push(Middleware::mapResponse(function (Response $response) { - $body = $response->getBody(); - $body->write('{"validationUrl":"https://api.ovh.com/login/?credentialToken=token","consumerKey":"consumer_remote","state":"pendingValidation"}'); + $body = Psr7\Utils::streamFor('{"validationUrl":"https://api.ovh.com/login/?credentialToken=token","consumerKey":"consumer_remote","state":"pendingValidation"}'); return $response ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') ->withBody($body); })); $property = self::getPrivateProperty('consumer_key'); + $this->assertEquals('consumer', $this->consumer_key); + $this->assertNotEquals('consumer_remote', $this->consumer_key); + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $accessRules = [json_decode(' { "method": "GET", "path": "/*" } ')]; @@ -238,6 +247,7 @@ class ApiTest extends TestCase $consumer_key = $property->getValue($api); $this->assertEquals($consumer_key, $credentials["consumerKey"]); + $this->assertEquals('consumer_remote', $credentials["consumerKey"]); $this->assertNotEquals($consumer_key, $this->consumer_key); } @@ -252,8 +262,7 @@ class ApiTest extends TestCase $handlerStack = $this->client->getConfig('handler'); $handlerStack->push(Middleware::mapResponse(function (Response $response) { - $body = $response->getBody(); - $body->write('{\"message\":\"Invalid application key\"}'); + $body = Psr7\Utils::streamFor('{\"message\":\"Invalid application key\"}'); return $response ->withStatus(401, 'POUET') @@ -283,8 +292,7 @@ class ApiTest extends TestCase $handlerStack = $this->client->getConfig('handler'); $handlerStack->push(Middleware::mapResponse(function (Response $response) { - $body = $response->getBody(); - $body->write('{\"message\":\"Invalid credentials\"}'); + $body = Psr7\Utils::streamFor('{\"message\":\"Invalid credentials\"}'); return $response ->withStatus(403) @@ -326,9 +334,14 @@ class ApiTest extends TestCase ->withQuery('')); return $request; })); - //$handlerStack->push(Middleware::mapResponse(function (Response $response) { - // return $response; - //})); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { + $body = Psr7\Utils::streamFor('123456789991'); + + return $response + ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($body); + })); $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $api->get('/me/api/credential?applicationId=49', ['status' => 'pendingValidation']); @@ -354,9 +367,14 @@ class ApiTest extends TestCase ->withQuery('')); return $request; })); - //$handlerStack->push(Middleware::mapResponse(function (Response $response) { - // return $response; - //})); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { + $body = Psr7\Utils::streamFor('123456789991'); + + return $response + ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($body); + })); $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $api->get('/me/api/credential?applicationId=49&status=pendingValidation', ['status' => 'expired', 'test' => "success"]); @@ -382,9 +400,14 @@ class ApiTest extends TestCase ->withQuery('')); return $request; })); - //$handlerStack->push(Middleware::mapResponse(function (Response $response) { - // return $response; - //})); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { + $body = Psr7\Utils::streamFor('123456789991'); + + return $response + ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($body); + })); $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $api->get('/me/api/credential', ['dryRun' => true, 'notDryRun' => false]); @@ -416,9 +439,14 @@ class ApiTest extends TestCase ->withQuery('')); return $request; })); - //$handlerStack->push(Middleware::mapResponse(function (Response $response) { - // return $response; - //})); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { + $body = Psr7\Utils::streamFor('123456789991'); + + return $response + ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($body); + })); $api = new Api($this->application_key, $this->application_secret, 'ovh-ca', $this->consumer_key, $this->client); $api->get('/me/api/credential'); @@ -450,9 +478,14 @@ class ApiTest extends TestCase ->withQuery('')); return $request; })); - //$handlerStack->push(Middleware::mapResponse(function (Response $response) { - // return $response; - //})); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { + $body = Psr7\Utils::streamFor('123456789991'); + + return $response + ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($body); + })); $api = new Api($this->application_key, $this->application_secret, 'http://api.ovh.com/1.0', $this->consumer_key, $this->client); $api->get('/me/api/credential'); @@ -484,9 +517,14 @@ class ApiTest extends TestCase ->withQuery('')); return $request; })); - //$handlerStack->push(Middleware::mapResponse(function (Response $response) { - // return $response; - //})); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { + $body = Psr7\Utils::streamFor('123456789991'); + + return $response + ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($body); + })); $api = new Api($this->application_key, $this->application_secret, 'https://api.ovh.com/1.0', $this->consumer_key, $this->client); $api->get('/me/api/credential'); @@ -514,10 +552,16 @@ class ApiTest extends TestCase return $request; })); $handlerStack->push(Middleware::mapResponse(function (Response $response) { - return $response->withStatus(200); + $body = Psr7\Utils::streamFor('{"validationUrl":"https://api.ovh.com/login/?credentialToken=token","consumerKey":"consumer_remote","state":"pendingValidation"}'); + + return $response + ->withStatus(200) + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($body); })); $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $api->requestCredentials([]); } + }