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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Test\Cache;
|
||||
|
||||
use PayPal\Cache\AuthorizationCache;
|
||||
|
||||
/**
|
||||
@@ -9,6 +10,7 @@ use PayPal\Cache\AuthorizationCache;
|
||||
*/
|
||||
class AuthorizationCacheTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
const CACHE_FILE = 'tests/var/test.cache';
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, opens a network connection.
|
||||
@@ -16,7 +18,6 @@ class AuthorizationCacheTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,17 +33,13 @@ class AuthorizationCacheTest extends \PHPUnit_Framework_TestCase
|
||||
return array(
|
||||
array(array('cache.enabled' => 'true'), true),
|
||||
array(array('cache.enabled' => true), true),
|
||||
array(array(), false),
|
||||
array(null, false)
|
||||
);
|
||||
}
|
||||
|
||||
public static function CachePathProvider()
|
||||
{
|
||||
return array(
|
||||
array(array('cache.FileName' => 'temp.cache'), 'temp.cache'),
|
||||
array(array(), 'auth.cache'),
|
||||
array(null, 'auth.cache')
|
||||
array(array('cache.FileName' => 'temp.cache'), 'temp.cache')
|
||||
);
|
||||
}
|
||||
|
||||
@@ -65,4 +62,46 @@ class AuthorizationCacheTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertContains($expected, $result);
|
||||
}
|
||||
|
||||
public function testCacheDisabled()
|
||||
{
|
||||
// 'cache.enabled' => true,
|
||||
AuthorizationCache::push(array('cache.enabled' => false), 'clientId', 'accessToken', 'tokenCreateTime', 'tokenExpiresIn');
|
||||
AuthorizationCache::pull(array('cache.enabled' => false), 'clientId');
|
||||
}
|
||||
|
||||
public function testCachePush()
|
||||
{
|
||||
AuthorizationCache::push(array('cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE), 'clientId', 'accessToken', 'tokenCreateTime', 'tokenExpiresIn');
|
||||
$contents = file_get_contents(AuthorizationCacheTest::CACHE_FILE);
|
||||
$tokens = json_decode($contents, true);
|
||||
$this->assertNotNull($contents);
|
||||
$this->assertEquals('clientId', $tokens['clientId']['clientId']);
|
||||
$this->assertEquals('accessToken', $tokens['clientId']['accessToken']);
|
||||
$this->assertEquals('tokenCreateTime', $tokens['clientId']['tokenCreateTime']);
|
||||
$this->assertEquals('tokenExpiresIn', $tokens['clientId']['tokenExpiresIn']);
|
||||
|
||||
}
|
||||
|
||||
public function testCachePullNonExisting()
|
||||
{
|
||||
$result = AuthorizationCache::pull(array('cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE), 'clientIdUndefined');
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCachePush
|
||||
*/
|
||||
public function testCachePull()
|
||||
{
|
||||
$result = AuthorizationCache::pull(array('cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE), 'clientId');
|
||||
$this->assertNotNull($result);
|
||||
$this->assertTrue(is_array($result));
|
||||
$this->assertEquals('clientId', $result['clientId']);
|
||||
$this->assertEquals('accessToken', $result['accessToken']);
|
||||
$this->assertEquals('tokenCreateTime', $result['tokenCreateTime']);
|
||||
$this->assertEquals('tokenExpiresIn', $result['tokenExpiresIn']);
|
||||
|
||||
unlink(AuthorizationCacheTest::CACHE_FILE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
73
tests/PayPal/Test/Handler/OauthHandlerTest.php
Normal file
73
tests/PayPal/Test/Handler/OauthHandlerTest.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Test\Handler;
|
||||
|
||||
use PayPal\Core\PayPalHttpConfig;
|
||||
use PayPal\Exception\PayPalConfigurationException;
|
||||
use PayPal\Rest\ApiContext;
|
||||
use PayPal\Auth\OAuthTokenCredential;
|
||||
use PayPal\Handler\OauthHandler;
|
||||
|
||||
class OauthHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \PayPal\Handler\OauthHandler
|
||||
*/
|
||||
public $handler;
|
||||
|
||||
/**
|
||||
* @var PayPalHttpConfig
|
||||
*/
|
||||
public $httpConfig;
|
||||
|
||||
/**
|
||||
* @var ApiContext
|
||||
*/
|
||||
public $apiContext;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $config;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->apiContext = new ApiContext(
|
||||
new OAuthTokenCredential(
|
||||
'clientId',
|
||||
'clientSecret'
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public function modeProvider()
|
||||
{
|
||||
return array(
|
||||
array( array('mode' => 'sandbox') ),
|
||||
array( array('mode' => 'live')),
|
||||
array( array( 'mode' => 'sandbox','oauth.EndPoint' => 'http://localhost/')),
|
||||
array( array('mode' => 'sandbox','service.EndPoint' => 'http://service.localhost/'))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider modeProvider
|
||||
* @param $configs
|
||||
*/
|
||||
public function testGetEndpoint($configs)
|
||||
{
|
||||
$config = $configs + array(
|
||||
'cache.enabled' => true,
|
||||
'http.headers.header1' => 'header1value'
|
||||
);
|
||||
$this->apiContext->setConfig($config);
|
||||
$this->httpConfig = new PayPalHttpConfig(null, 'POST', $config);
|
||||
$this->handler = new OauthHandler($this->apiContext);
|
||||
$this->handler->handle($this->httpConfig, null, $this->config);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user