1
0
mirror of https://github.com/ovh/php-ovh.git synced 2023-11-05 03:20:26 +01:00

fix: tests now pass with PHPUnit and PHP7.4

Fixes unit tests for #106
This commit is contained in:
Romain Beuque 2021-01-19 19:43:10 +00:00 committed by Stéphane Bour
parent 3dd7f3ecbe
commit 951e4adedb
3 changed files with 83 additions and 44 deletions

View File

@ -11,4 +11,4 @@ before_script:
- composer self-update - composer self-update
- composer install - composer install
script: vendor/bin/phpunit script: vendor/bin/phpunit tests/ApiTest.php

View File

@ -308,10 +308,9 @@ class Api
* *
* @param Response $response * @param Response $response
* *
* @return array
* @throws \JsonException * @throws \JsonException
*/ */
private function decodeResponse(Response $response): array private function decodeResponse(Response $response)
{ {
return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR); 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 array headers custom HTTP headers to add on the request
* @param bool is_authenticated if the request need to be authenticated * @param bool is_authenticated if the request need to be authenticated
* *
* @return array
* @throws ClientException if http request is an error * @throws ClientException if http request is an error
* @throws \JsonException * @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)) { if (preg_match('/^\/[^\/]+\.json$/', $path)) {
// Schema description must be access without authentication // 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 array headers custom HTTP headers to add on the request
* @param bool is_authenticated if the request need to be authenticated * @param bool is_authenticated if the request need to be authenticated
* *
* @return array
* @throws 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): array public function post($path, $content = null, $headers = null, $is_authenticated = true)
{ {
return $this->decodeResponse( return $this->decodeResponse(
$this->rawCall("POST", $path, $content, $is_authenticated, $headers) $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 array headers custom HTTP headers to add on the request
* @param bool is_authenticated if the request need to be authenticated * @param bool is_authenticated if the request need to be authenticated
* *
* @return array
* @throws 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): array public function put($path, $content, $headers = null, $is_authenticated = true)
{ {
return $this->decodeResponse( return $this->decodeResponse(
$this->rawCall("PUT", $path, $content, $is_authenticated, $headers) $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 array headers custom HTTP headers to add on the request
* @param bool is_authenticated if the request need to be authenticated * @param bool is_authenticated if the request need to be authenticated
* *
* @return array
* @throws 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): array public function delete($path, $content = null, $headers = null, $is_authenticated = true)
{ {
return $this->decodeResponse( return $this->decodeResponse(
$this->rawCall("DELETE", $path, $content, $is_authenticated, $headers) $this->rawCall("DELETE", $path, $content, $is_authenticated, $headers)

View File

@ -30,6 +30,7 @@ namespace Ovh\tests;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Middleware; use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Request;
use Ovh\Api; use Ovh\Api;
@ -145,14 +146,19 @@ class ApiTest extends TestCase
return $request; return $request;
} }
$request = $request->withUri($request->getUri() return null;
->withHost('httpbin.org') }));
->withPath('/') $handlerStack->push(Middleware::mapResponse(function (Response $response) {
->withQuery('')); $body = Psr7\Utils::streamFor('{}');
return $request;
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 = 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 = $this->client->getConfig('handler');
$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
$body = $response->getBody(); $body = Psr7\Utils::streamFor(time() - 10);
$body->write(time() - 10);
return $response return $response
->withStatus(200) ->withStatus(200)
->withHeader('Content-Type', 'application/json; charset=utf-8')
->withBody($body); ->withBody($body);
})); }));
@ -220,16 +226,19 @@ class ApiTest extends TestCase
$handlerStack = $this->client->getConfig('handler'); $handlerStack = $this->client->getConfig('handler');
$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
$body = $response->getBody(); $body = Psr7\Utils::streamFor('{"validationUrl":"https://api.ovh.com/login/?credentialToken=token","consumerKey":"consumer_remote","state":"pendingValidation"}');
$body->write('{"validationUrl":"https://api.ovh.com/login/?credentialToken=token","consumerKey":"consumer_remote","state":"pendingValidation"}');
return $response return $response
->withStatus(200) ->withStatus(200)
->withHeader('Content-Type', 'application/json; charset=utf-8')
->withBody($body); ->withBody($body);
})); }));
$property = self::getPrivateProperty('consumer_key'); $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); $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
$accessRules = [json_decode(' { "method": "GET", "path": "/*" } ')]; $accessRules = [json_decode(' { "method": "GET", "path": "/*" } ')];
@ -238,6 +247,7 @@ class ApiTest extends TestCase
$consumer_key = $property->getValue($api); $consumer_key = $property->getValue($api);
$this->assertEquals($consumer_key, $credentials["consumerKey"]); $this->assertEquals($consumer_key, $credentials["consumerKey"]);
$this->assertEquals('consumer_remote', $credentials["consumerKey"]);
$this->assertNotEquals($consumer_key, $this->consumer_key); $this->assertNotEquals($consumer_key, $this->consumer_key);
} }
@ -252,8 +262,7 @@ class ApiTest extends TestCase
$handlerStack = $this->client->getConfig('handler'); $handlerStack = $this->client->getConfig('handler');
$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
$body = $response->getBody(); $body = Psr7\Utils::streamFor('{\"message\":\"Invalid application key\"}');
$body->write('{\"message\":\"Invalid application key\"}');
return $response return $response
->withStatus(401, 'POUET') ->withStatus(401, 'POUET')
@ -283,8 +292,7 @@ class ApiTest extends TestCase
$handlerStack = $this->client->getConfig('handler'); $handlerStack = $this->client->getConfig('handler');
$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
$body = $response->getBody(); $body = Psr7\Utils::streamFor('{\"message\":\"Invalid credentials\"}');
$body->write('{\"message\":\"Invalid credentials\"}');
return $response return $response
->withStatus(403) ->withStatus(403)
@ -326,9 +334,14 @@ class ApiTest extends TestCase
->withQuery('')); ->withQuery(''));
return $request; return $request;
})); }));
//$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
// return $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 = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
$api->get('/me/api/credential?applicationId=49', ['status' => 'pendingValidation']); $api->get('/me/api/credential?applicationId=49', ['status' => 'pendingValidation']);
@ -354,9 +367,14 @@ class ApiTest extends TestCase
->withQuery('')); ->withQuery(''));
return $request; return $request;
})); }));
//$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
// return $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 = 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"]); $api->get('/me/api/credential?applicationId=49&status=pendingValidation', ['status' => 'expired', 'test' => "success"]);
@ -382,9 +400,14 @@ class ApiTest extends TestCase
->withQuery('')); ->withQuery(''));
return $request; return $request;
})); }));
//$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
// return $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 = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
$api->get('/me/api/credential', ['dryRun' => true, 'notDryRun' => false]); $api->get('/me/api/credential', ['dryRun' => true, 'notDryRun' => false]);
@ -416,9 +439,14 @@ class ApiTest extends TestCase
->withQuery('')); ->withQuery(''));
return $request; return $request;
})); }));
//$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
// return $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 = new Api($this->application_key, $this->application_secret, 'ovh-ca', $this->consumer_key, $this->client);
$api->get('/me/api/credential'); $api->get('/me/api/credential');
@ -450,9 +478,14 @@ class ApiTest extends TestCase
->withQuery('')); ->withQuery(''));
return $request; return $request;
})); }));
//$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
// return $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 = new Api($this->application_key, $this->application_secret, 'http://api.ovh.com/1.0', $this->consumer_key, $this->client);
$api->get('/me/api/credential'); $api->get('/me/api/credential');
@ -484,9 +517,14 @@ class ApiTest extends TestCase
->withQuery('')); ->withQuery(''));
return $request; return $request;
})); }));
//$handlerStack->push(Middleware::mapResponse(function (Response $response) { $handlerStack->push(Middleware::mapResponse(function (Response $response) {
// return $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 = new Api($this->application_key, $this->application_secret, 'https://api.ovh.com/1.0', $this->consumer_key, $this->client);
$api->get('/me/api/credential'); $api->get('/me/api/credential');
@ -514,10 +552,16 @@ class ApiTest extends TestCase
return $request; return $request;
})); }));
$handlerStack->push(Middleware::mapResponse(function (Response $response) { $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 = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
$api->requestCredentials([]); $api->requestCredentials([]);
} }
} }