Skip to content

Commit

Permalink
fix: tests now pass with PHPUnit and PHP7.4
Browse files Browse the repository at this point in the history
Fixes unit tests for #106
  • Loading branch information
rbeuque74 authored and StephaneBour committed Jan 19, 2021
1 parent 3dd7f3e commit 951e4ad
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ before_script:
- composer self-update
- composer install

script: vendor/bin/phpunit
script: vendor/bin/phpunit tests/ApiTest.php
15 changes: 5 additions & 10 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,9 @@ protected function rawCall($method, $path, $content = null, $is_authenticated =
*
* @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);
}
Expand All @@ -324,11 +323,10 @@ private function decodeResponse(Response $response): array
* @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
Expand All @@ -350,10 +348,9 @@ public function get($path, $content = null, $headers = null, $is_authenticated =
* @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)
Expand All @@ -368,10 +365,9 @@ public function post($path, $content = null, $headers = null, $is_authenticated
* @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)
Expand All @@ -386,10 +382,9 @@ public function put($path, $content, $headers = null, $is_authenticated = true):
* @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)
Expand Down
110 changes: 77 additions & 33 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use Ovh\Api;
Expand Down Expand Up @@ -145,14 +146,19 @@ public function testNoCheckAppKeyForUnauthCall()
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);
}

/**
Expand Down Expand Up @@ -193,11 +199,11 @@ public function testTimeDeltaCompute()
$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);
}));

Expand All @@ -220,16 +226,19 @@ public function testIfConsumerKeyIsReplace()
$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": "/*" } ')];

Expand All @@ -238,6 +247,7 @@ public function testIfConsumerKeyIsReplace()
$consumer_key = $property->getValue($api);

$this->assertEquals($consumer_key, $credentials["consumerKey"]);
$this->assertEquals('consumer_remote', $credentials["consumerKey"]);
$this->assertNotEquals($consumer_key, $this->consumer_key);
}

Expand All @@ -252,8 +262,7 @@ public function testInvalidApplicationKey()
$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')
Expand Down Expand Up @@ -283,8 +292,7 @@ public function testInvalidRight()
$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)
Expand Down Expand Up @@ -326,9 +334,14 @@ public function testGetQueryArgs()
->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']);
Expand All @@ -354,9 +367,14 @@ public function testGetOverlappingQueryArgs()
->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"]);
Expand All @@ -382,9 +400,14 @@ public function testGetBooleanQueryArgs()
->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]);
Expand Down Expand Up @@ -416,9 +439,14 @@ public function testPredefinedEndPoint()
->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');
Expand Down Expand Up @@ -450,9 +478,14 @@ public function testProvidedHttpEndPoint()
->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');
Expand Down Expand Up @@ -484,9 +517,14 @@ public function testProvidedHttpsEndPoint()
->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');
Expand Down Expand Up @@ -514,10 +552,16 @@ public function testMissingOvhApplicationHeaderOnRequestCredentials()
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([]);
}

}

0 comments on commit 951e4ad

Please sign in to comment.