forked from LiveCarta/PayPal-PHP-SDK
Updated OAuthToken to throw proper error on not receiving access token
- Updated Samples, Tests
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
<?php
|
||||
|
||||
// namespace PayPal\Test\Common;
|
||||
namespace PayPal\Test\Auth;
|
||||
|
||||
use PayPal\Auth\OAuthTokenCredential;
|
||||
use PayPal\Cache\AuthorizationCache;
|
||||
use PayPal\Exception\PayPalConnectionException;
|
||||
use PayPal\Rest\ApiContext;
|
||||
use PayPal\Test\Cache\AuthorizationCacheTest;
|
||||
use PayPal\Test\Constants;
|
||||
use PayPal\Core\PayPalConfigManager;
|
||||
use PayPal\Exception\PayPalConnectionException;
|
||||
|
||||
class OAuthTokenCredentialTest extends PHPUnit_Framework_TestCase
|
||||
class OAuthTokenCredentialTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -16,6 +19,8 @@ class OAuthTokenCredentialTest extends PHPUnit_Framework_TestCase
|
||||
public function testGetAccessToken()
|
||||
{
|
||||
$cred = new OAuthTokenCredential(Constants::CLIENT_ID, Constants::CLIENT_SECRET);
|
||||
$this->assertEquals(Constants::CLIENT_ID, $cred->getClientId());
|
||||
$this->assertEquals(Constants::CLIENT_SECRET, $cred->getClientSecret());
|
||||
$config = PayPalConfigManager::getInstance()->getConfigHashmap();
|
||||
$token = $cred->getAccessToken($config);
|
||||
$this->assertNotNull($token);
|
||||
@@ -35,4 +40,103 @@ class OAuthTokenCredentialTest extends PHPUnit_Framework_TestCase
|
||||
$cred = new OAuthTokenCredential('dummy', 'secret');
|
||||
$this->assertNull($cred->getAccessToken(PayPalConfigManager::getInstance()->getConfigHashmap()));
|
||||
}
|
||||
|
||||
public function testGetAccessTokenUnit()
|
||||
{
|
||||
$config = array(
|
||||
'mode' => 'sandbox',
|
||||
'cache.enabled' => true,
|
||||
'cache.FileName' => AuthorizationCacheTest::CACHE_FILE
|
||||
);
|
||||
//{"clientId":{"clientId":"clientId","accessToken":"accessToken","tokenCreateTime":1421204091,"tokenExpiresIn":288000000}}
|
||||
AuthorizationCache::push($config, 'clientId', 'accessToken', 1421204091, 288000000);
|
||||
$cred = new OAuthTokenCredential('clientId', 'clientSecret');
|
||||
$apiContext = new ApiContext($cred);
|
||||
$apiContext->setConfig($config);
|
||||
$this->assertEquals('clientId', $cred->getClientId());
|
||||
$this->assertEquals('clientSecret', $cred->getClientSecret());
|
||||
$result = $cred->getAccessToken($config);
|
||||
$this->assertNotNull($result);
|
||||
}
|
||||
|
||||
public function testGetAccessTokenUnitMock()
|
||||
{
|
||||
$config = array(
|
||||
'mode' => 'sandbox'
|
||||
);
|
||||
/** @var OAuthTokenCredential $auth */
|
||||
$auth = $this->getMockBuilder('\PayPal\Auth\OAuthTokenCredential')
|
||||
->setConstructorArgs(array('clientId', 'clientSecret'))
|
||||
->setMethods(array('getToken'))
|
||||
->getMock();
|
||||
|
||||
$auth->expects($this->any())
|
||||
->method('getToken')
|
||||
->will($this->returnValue(
|
||||
array('refresh_token' => 'refresh_token_value')
|
||||
));
|
||||
$response = $auth->getRefreshToken($config, 'auth_value');
|
||||
$this->assertNotNull($response);
|
||||
$this->assertEquals('refresh_token_value', $response);
|
||||
|
||||
}
|
||||
|
||||
public function testUpdateAccessTokenUnitMock()
|
||||
{
|
||||
$config = array(
|
||||
'mode' => 'sandbox'
|
||||
);
|
||||
/** @var OAuthTokenCredential $auth */
|
||||
$auth = $this->getMockBuilder('\PayPal\Auth\OAuthTokenCredential')
|
||||
->setConstructorArgs(array('clientId', 'clientSecret'))
|
||||
->setMethods(array('getToken'))
|
||||
->getMock();
|
||||
|
||||
$auth->expects($this->any())
|
||||
->method('getToken')
|
||||
->will($this->returnValue(
|
||||
array(
|
||||
'access_token' => 'accessToken',
|
||||
'expires_in' => 280
|
||||
)
|
||||
));
|
||||
|
||||
$response = $auth->updateAccessToken($config);
|
||||
$this->assertNotNull($response);
|
||||
$this->assertEquals('accessToken', $response);
|
||||
|
||||
$response = $auth->updateAccessToken($config, 'refresh_token');
|
||||
$this->assertNotNull($response);
|
||||
$this->assertEquals('accessToken', $response);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PayPal\Exception\PayPalConnectionException
|
||||
* @expectedExceptionMessage Could not generate new Access token. Invalid response from server:
|
||||
*/
|
||||
public function testUpdateAccessTokenNullReturnUnitMock()
|
||||
{
|
||||
$config = array(
|
||||
'mode' => 'sandbox'
|
||||
);
|
||||
/** @var OAuthTokenCredential $auth */
|
||||
$auth = $this->getMockBuilder('\PayPal\Auth\OAuthTokenCredential')
|
||||
->setConstructorArgs(array('clientId', 'clientSecret'))
|
||||
->setMethods(array('getToken'))
|
||||
->getMock();
|
||||
|
||||
$auth->expects($this->any())
|
||||
->method('getToken')
|
||||
->will($this->returnValue(
|
||||
array(
|
||||
)
|
||||
));
|
||||
|
||||
$response = $auth->updateAccessToken($config);
|
||||
$this->assertNotNull($response);
|
||||
$this->assertEquals('accessToken', $response);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user