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

Added 100% Code Coverage

Signed-off-by: Thierry Goettelmann <thierry@byscripts.info>
This commit is contained in:
Thierry Goettelmann 2015-08-06 15:15:19 +02:00
parent 47d3cd2ed7
commit 6ecf7c2773
2 changed files with 379 additions and 176 deletions

View File

@ -28,7 +28,6 @@
namespace Ovh\tests; namespace Ovh\tests;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Mock;
use Ovh\Api; use Ovh\Api;
/** /**
@ -37,8 +36,50 @@ use Ovh\Api;
* @package Ovh * @package Ovh
* @category Ovh * @category Ovh
* @author Vincent Cassé <vincent.casse@ovh.net> * @author Vincent Cassé <vincent.casse@ovh.net>
* @author Thierry Goettelmann <thierry@byscripts.info>
*/ */
class ApiFunctionalTest extends \PHPUnit_Framework_TestCase { class ApiFunctionalTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string
*/
private $application_key;
/**
* @var string
*/
private $application_secret;
/**
* @var string
*/
private $consumer_key;
/**
* @var string
*/
private $endpoint;
/**
* @var string
*/
private $rangeIP;
/**
* @var string
*/
private $alternativeRangeIP;
/**
* @var Client
*/
private $client;
/**
* @var Api
*/
private $api;
/** /**
* Define id to create object * Define id to create object
@ -50,28 +91,47 @@ class ApiFunctionalTest extends \PHPUnit_Framework_TestCase {
$this->consumer_key = getenv('CONSUMER'); $this->consumer_key = getenv('CONSUMER');
$this->endpoint = getenv('ENDPOINT'); $this->endpoint = getenv('ENDPOINT');
$this->rangeIP = '127.0.0.20/32'; $this->rangeIP = '127.0.0.20/32';
$this->alternativeRangeIP = '127.0.0.30/32';
$this->client = new Client(); $this->client = new Client();
$this->api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $this->api = new Api(
$this->application_key,
$this->application_secret,
$this->endpoint,
$this->consumer_key,
$this->client
);
} }
/** /**
* Get private and protected method to unit test it * Get private and protected method to unit test it
*
* @param string $name
*
* @return \ReflectionMethod
*/ */
protected static function getPrivateMethod($name) protected static function getPrivateMethod($name)
{ {
$class = new \ReflectionClass('Ovh\Api'); $class = new \ReflectionClass('Ovh\Api');
$method = $class->getMethod($name); $method = $class->getMethod($name);
$method->setAccessible(true); $method->setAccessible(true);
return $method; return $method;
} }
/**
* Get private and protected property to unit test it
*
* @param string $name
*
* @return \ReflectionProperty
*/
protected static function getPrivateProperty($name) protected static function getPrivateProperty($name)
{ {
$class = new \ReflectionClass('Ovh\Api'); $class = new \ReflectionClass('Ovh\Api');
$property = $class->getProperty($name); $property = $class->getProperty($name);
$property->setAccessible(true); $property->setAccessible(true);
return $property; return $property;
} }
@ -96,19 +156,20 @@ class ApiFunctionalTest extends \PHPUnit_Framework_TestCase {
} }
/** /**
* Test if put request on me * Test if post request on me
*/ */
public function testPutRestrictionAccessIp() public function testPostRestrictionAccessIp()
{ {
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $this->assertNull(
$invoker = self::getPrivateMethod('rawCall'); $this->api->post('/me/accessRestriction/ip', ['ip' => $this->rangeIP, 'rule' => 'deny', 'warning' => true])
$params = new \StdClass(); );
$params->ip = $this->rangeIP;
$params->rule = "deny";
$params->warning = true;
$result = $invoker->invokeArgs($api, array('POST', '/me/accessRestriction/ip', $params)) ; $this->assertNull(
$this->assertNull( $result ); $this->api->post('/me/accessRestriction/ip', ['ip' => $this->alternativeRangeIP,
'rule' => 'deny',
'warning' => true,
])
);
} }
/** /**
@ -116,17 +177,35 @@ class ApiFunctionalTest extends \PHPUnit_Framework_TestCase {
*/ */
public function testGetRestrictionAccessIP() public function testGetRestrictionAccessIP()
{ {
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $result = $this->api->get('/me/accessRestriction/ip');
$invoker = self::getPrivateMethod('rawCall');
$result = $invoker->invokeArgs($api, array('GET', '/me/accessRestriction/ip')) ;
foreach ($result as $restrictionId) {
$restriction = $invoker->invokeArgs($api, array('GET', '/me/accessRestriction/ip/' . $restrictionId)) ;
if ($restriction["ip"] == $this->rangeIP) { $restrictionIps = [];
$this->assertTrue(true);
break; foreach ($result as $restrictionId) {
$restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
$restrictionIps[] = $restriction['ip'];
} }
$this->assertContains($this->rangeIP, $restrictionIps);
$this->assertContains($this->alternativeRangeIP, $restrictionIps);
} }
/**
* Test if delete request on /me
*/
public function testPutRestrictionAccessIP()
{
$result = $this->api->get('/me/accessRestriction/ip');
$restrictionId = array_pop($result);
$this->assertNull(
$this->api->put('/me/accessRestriction/ip/' . $restrictionId, ['rule' => 'accept', 'warning' => true])
);
$restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
$this->assertEquals('accept', $restriction['rule']);
} }
/** /**
@ -134,15 +213,12 @@ class ApiFunctionalTest extends \PHPUnit_Framework_TestCase {
*/ */
public function testDeleteRestrictionAccessIP() public function testDeleteRestrictionAccessIP()
{ {
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $result = $this->api->get('/me/accessRestriction/ip');
$invoker = self::getPrivateMethod('rawCall');
$result = $invoker->invokeArgs($api, array('GET', '/me/accessRestriction/ip')) ;
foreach ($result as $restrictionId) { foreach ($result as $restrictionId) {
$restriction = $invoker->invokeArgs($api, array('GET', '/me/accessRestriction/ip/' . $restrictionId)) ; $restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
if ($restriction["ip"] == $this->rangeIP) { if (in_array($restriction["ip"], [$this->rangeIP, $this->alternativeRangeIP])) {
$result = $invoker->invokeArgs($api, array('DELETE', '/me/accessRestriction/ip/'. $restrictionId)) ; $result = $this->api->delete('/me/accessRestriction/ip/' . $restrictionId);
$this->assertNull($result); $this->assertNull($result);
break; break;
} }
@ -154,10 +230,18 @@ class ApiFunctionalTest extends \PHPUnit_Framework_TestCase {
*/ */
public function testIfRequestWithoutAuthenticationWorks() public function testIfRequestWithoutAuthenticationWorks()
{ {
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, NULL, $this->client); $api = new Api($this->application_key, $this->application_secret, $this->endpoint, null, $this->client);
$invoker = self::getPrivateMethod('rawCall'); $invoker = self::getPrivateMethod('rawCall');
$result = $invoker->invokeArgs($api, array('GET', '/xdsl/incidents')) ; $invoker->invokeArgs($api, ['GET', '/xdsl/incidents']);
} }
} /**
* Test Api::get
*/
public function testApiGetWithParameters()
{
$this->setExpectedException('\\GuzzleHttp\\Exception\\ClientException', '400');
$this->api->get('/me/accessRestriction/ip', ['foo' => 'bar']);
}
}

View File

@ -27,10 +27,10 @@
namespace Ovh\tests; namespace Ovh\tests;
use Ovh\Api;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Mock; use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Ovh\Api;
/** /**
* Test Api class * Test Api class
@ -38,8 +38,34 @@ use GuzzleHttp\Subscriber\Mock;
* @package Ovh * @package Ovh
* @category Ovh * @category Ovh
* @author Vincent Cassé <vincent.casse@ovh.net> * @author Vincent Cassé <vincent.casse@ovh.net>
* @author Thierry Goettelmann <thierry@byscripts.info>
*/ */
class ApiTest extends \PHPUnit_Framework_TestCase { class ApiTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Client
*/
private $client;
/**
* @var string
*/
private $application_key;
/**
* @var string
*/
private $consumer_key;
/**
* @var string
*/
private $endpoint;
/**
* @var string
*/
private $application_secret;
/** /**
* Define id to create object * Define id to create object
@ -56,38 +82,105 @@ class ApiTest extends \PHPUnit_Framework_TestCase {
/** /**
* Get private and protected method to unit test it * Get private and protected method to unit test it
*
* @param string $name
*
* @return \ReflectionMethod
*/ */
protected static function getPrivateMethod($name) protected static function getPrivateMethod($name)
{ {
$class = new \ReflectionClass('Ovh\Api'); $class = new \ReflectionClass('Ovh\Api');
$method = $class->getMethod($name); $method = $class->getMethod($name);
$method->setAccessible(true); $method->setAccessible(true);
return $method; return $method;
} }
/**
* Get private and protected property to unit test it
*
* @param string $name
*
* @return \ReflectionProperty
*/
protected static function getPrivateProperty($name) protected static function getPrivateProperty($name)
{ {
$class = new \ReflectionClass('Ovh\Api'); $class = new \ReflectionClass('Ovh\Api');
$property = $class->getProperty($name); $property = $class->getProperty($name);
$property->setAccessible(true); $property->setAccessible(true);
return $property; return $property;
} }
/**
* Test missing $application_key
*/
public function testMissingApplicationKey()
{
$this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Application key');
new Api(null, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
}
/**
* Test missing $application_secret
*/
public function testMissingApplicationSecret()
{
$this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Application secret');
new Api($this->application_key, null, $this->endpoint, $this->consumer_key, $this->client);
}
/**
* Test missing $api_endpoint
*/
public function testMissingApiEndpoint()
{
$this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Endpoint');
new Api($this->application_key, $this->application_secret, null, $this->consumer_key, $this->client);
}
/**
* Test bad $api_endpoint
*/
public function testBadApiEndpoint()
{
$this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Unknown');
new Api($this->application_key, $this->application_secret, 'i_am_invalid', $this->consumer_key, $this->client);
}
/**
* Test creating Client if none is provided
*/
public function testClientCreation()
{
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key);
$this->assertInstanceOf('\\GuzzleHttp\\Client', $api->getHttpClient());
}
/** /**
* Test the compute of time delta * Test the compute of time delta
*/ */
public function testTimeDeltaCompute() public function testTimeDeltaCompute()
{ {
$delay = 10; $delay = 10;
$mock = new Mock([
"HTTP/1.1 200 OK\r\n\r\n". (time()-$delay), $handlerStack = $this->client->getConfig('handler');
]); $handlerStack->push(Middleware::mapResponse(function (Response $response) {
$this->client->getEmitter()->attach($mock);
$body = $response->getBody();
$body->write(time() - 10);
return $response
->withStatus(200)
->withBody($body);
}));
$invoker = self::getPrivateMethod('calculateTimeDelta'); $invoker = self::getPrivateMethod('calculateTimeDelta');
$property = self::getPrivateProperty('time_delta'); $property = self::getPrivateProperty('time_delta');
$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);
$invoker->invokeArgs($api, array()) ; $invoker->invokeArgs($api, []);
$time_delta = $property->getValue($api); $time_delta = $property->getValue($api);
$this->assertNotNull($time_delta); $this->assertNotNull($time_delta);
@ -99,17 +192,24 @@ class ApiTest extends \PHPUnit_Framework_TestCase {
*/ */
public function testIfConsumerKeyIsReplace() public function testIfConsumerKeyIsReplace()
{ {
$delay = 10; $handlerStack = $this->client->getConfig('handler');
$mock = new Mock([ $handlerStack->push(Middleware::mapResponse(function (Response $response) {
"HTTP/1.1 200 OK\r\n\r\n". '{"validationUrl":"https://api.ovh.com/login/?credentialToken=token","consumerKey":"consumer_remote","state":"pendingValidation"}'
]); $body = $response->getBody();
$this->client->getEmitter()->attach($mock); $body->write('{"validationUrl":"https://api.ovh.com/login/?credentialToken=token","consumerKey":"consumer_remote","state":"pendingValidation"}');
return $response
->withStatus(200)
->withBody($body);
}));
$property = self::getPrivateProperty('consumer_key'); $property = self::getPrivateProperty('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 = array( json_decode(' { "method": "GET", "path": "/*" } ') ); $accessRules = [json_decode(' { "method": "GET", "path": "/*" } ')];
$credentials = $api->requestCredentials($accessRules); $credentials = $api->requestCredentials($accessRules);
$consumer_key = $property->getValue($api); $consumer_key = $property->getValue($api);
$this->assertEquals($consumer_key, $credentials["consumerKey"]); $this->assertEquals($consumer_key, $credentials["consumerKey"]);
@ -125,15 +225,22 @@ class ApiTest extends \PHPUnit_Framework_TestCase {
'\GuzzleHttp\Exception\ClientException' '\GuzzleHttp\Exception\ClientException'
); );
$delay = 10; $handlerStack = $this->client->getConfig('handler');
$mock = new Mock([ $handlerStack->push(Middleware::mapResponse(function (Response $response) {
"HTTP/1.1 401 Unauthorized\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: 37\r\n\r\n{\"message\":\"Invalid application key\"}"
]); $body = $response->getBody();
$this->client->getEmitter()->attach($mock); $body->write('{\"message\":\"Invalid application key\"}');
return $response
->withStatus(401, 'POUET')
->withHeader('Content-Type', 'application/json; charset=utf-8')
->withHeader('Content-Length', 37)
->withBody($body);
}));
$property = self::getPrivateProperty('consumer_key'); $property = self::getPrivateProperty('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 = array( json_decode(' { "method": "GET", "path": "/*" } ') ); $accessRules = [json_decode(' { "method": "GET", "path": "/*" } ')];
$credentials = $api->requestCredentials($accessRules); $credentials = $api->requestCredentials($accessRules);
$consumer_key = $property->getValue($api); $consumer_key = $property->getValue($api);
@ -151,16 +258,28 @@ class ApiTest extends \PHPUnit_Framework_TestCase {
'\GuzzleHttp\Exception\ClientException' '\GuzzleHttp\Exception\ClientException'
); );
$delay = 10; $handlerStack = $this->client->getConfig('handler');
$mock = new Mock([ $handlerStack->push(Middleware::mapResponse(function (Response $response) {
"HTTP/1.1 403 Forbidden\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: 37\r\n\r\n{\"message\":\"Invalid credentials\"}"
]); $body = $response->getBody();
$this->client->getEmitter()->attach($mock); $body->write('{\"message\":\"Invalid credentials\"}');
return $response
->withStatus(403)
->withHeader('Content-Type', 'application/json; charset=utf-8')
->withHeader('Content-Length', 37)
->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);
$invoker = self::getPrivateMethod('rawCall'); $invoker = self::getPrivateMethod('rawCall');
$invoker->invokeArgs($api, array('GET', '/me')) ; $invoker->invokeArgs($api, ['GET', '/me']);
}
} }
public function testGetConsumerKey()
{
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
$this->assertEquals($this->consumer_key, $api->getConsumerKey());
}
}