Removing Dependency from SDK Core Project

- Copied files required for Rest API SDK
- Removed PPApiContext and directly connected APIContext with PPConfigManager
- Removed duplicate data storage of configuration and credentials.
- Code Style Fixes
- Remove build.xml file as it is not required anymore
- Updated the samples
- Updated the documentations
This commit is contained in:
japatel
2014-10-06 11:16:47 -05:00
parent bb7654b0b8
commit 49b80f76af
150 changed files with 10975 additions and 6525 deletions

View File

@@ -2,10 +2,10 @@
namespace PayPal\Test\Api;
use PayPal\Api\Address;
use PayPal\Test\Constants;
class AddressTest extends \PHPUnit_Framework_TestCase {
/** @var Address */
private $address;
public static $line1 = "3909 Witmer Road";

View File

@@ -7,10 +7,13 @@ use PayPal\Test\Constants;
use PayPal\Core\PPConfigManager;
use PayPal\Exception\PPConnectionException;
class OAuthTokenCredentialTest extends PHPUnit_Framework_TestCase {
/**
* @group integration
*/
public function testGetAccessToken() {
$cred = new OAuthTokenCredential(Constants::CLIENT_ID, Constants::CLIENT_SECRET);
$config = PPConfigManager::getConfigWithDefaults();
$config = PPConfigManager::getInstance()->getConfigHashmap();
$token = $cred->getAccessToken($config);
$this->assertNotNull($token);
@@ -18,17 +21,14 @@ class OAuthTokenCredentialTest extends PHPUnit_Framework_TestCase {
$newToken = $cred->getAccessToken($config);
$this->assertNotNull($newToken);
$this->assertEquals($token, $newToken);
// sleep(60*8);
// $newToken = $cred->getAccessToken();
// $this->assertNotNull($newToken);
// $this->assertNotEqual($token, $newToken);
}
/**
* @group integration
*/
public function testInvalidCredentials() {
$this->setExpectedException('PayPal\Exception\PPConnectionException');
$cred = new OAuthTokenCredential('dummy', 'secret');
$this->assertNull($cred->getAccessToken(PPConfigManager::getConfigWithDefaults()));
$this->assertNull($cred->getAccessToken(PPConfigManager::getInstance()->getConfigHashmap()));
}
}

View File

@@ -0,0 +1,228 @@
<?php
use PayPal\Common\PPModel;
class SimpleModelTestClass extends PPModel {
/**
*
* @access public
* @param string $field1
*/
public function setField1($field1) {
$this->field1 = $field1;
return $this;
}
/**
*
* @access public
* @return string
*/
public function getField1() {
return $this->field1;
}
/**
*
* @access public
* @param string $field2
*/
public function setField2($field2) {
$this->field2 = $field2;
return $this;
}
/**
*
* @access public
* @return string
*/
public function getField2() {
return $this->field2;
}
}
class ContainerModelTestClass extends PPModel {
/**
*
* @access public
* @param string $field1
*/
public function setField1($field1) {
$this->field1 = $field1;
return $this;
}
/**
*
* @access public
* @return string
*/
public function getField1() {
return $this->field1;
}
/**
*
* @access public
* @param SimpleModelTestClass $field1
*/
public function setNested1($nested1) {
$this->nested1 = $nested1;
return $this;
}
/**
*
* @access public
* @return SimpleModelTestClass
*/
public function getNested1() {
return $this->nested1;
}
}
class ListModelTestClass extends PPModel {
/**
*
* @access public
* @param string $list1
*/
public function setList1($list1) {
$this->list1 = $list1;
}
/**
*
* @access public
* @return string
*/
public function getList1() {
return $this->list1;
}
/**
*
* @access public
* @param SimpleModelTestClass $list2 array of SimpleModelTestClass
*/
public function setList2($list2) {
$this->list2 = $list2;
return $this;
}
/**
*
* @access public
* @return SimpleModelTestClass array of SimpleModelTestClass
*/
public function getList2() {
return $this->list2;
}
}
/**
* Test class for PPModel.
*
*/
class PPModelTest extends PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
/**
* @test
*/
public function testSimpleConversion() {
$o = new SimpleModelTestClass();
$o->setField1('value 1');
$o->setField2("value 2");
$this->assertEquals('{"field1":"value 1","field2":"value 2"}', $o->toJSON());
$oCopy = new SimpleModelTestClass();
$oCopy->fromJson($o->toJSON());
$this->assertEquals($o, $oCopy);
}
/**
* @test
*/
public function testSpecialChars() {
$o = new SimpleModelTestClass();
$o->setField1('value "1');
$o->setField2("value 2");
$this->assertEquals('{"field1":"value \"1","field2":"value 2"}', $o->toJSON());
$oCopy = new SimpleModelTestClass();
$oCopy->fromJson($o->toJSON());
$this->assertEquals($o, $oCopy);
}
/**
* @test
*/
public function testNestedConversion() {
$child = new SimpleModelTestClass();
$child->setField1('value 1');
$child->setField2("value 2");
$parent = new ContainerModelTestClass();
$parent->setField1("parent");
$parent->setNested1($child);
$this->assertEquals('{"field1":"parent","nested1":{"field1":"value 1","field2":"value 2"}}',
$parent->toJSON());
$parentCopy = new ContainerModelTestClass();
$parentCopy->fromJson($parent->toJSON());
$this->assertEquals($parent, $parentCopy);
}
/**
* @test
*/
public function testListConversion() {
$c1 = new SimpleModelTestClass();
$c1->setField1("a")->setField2('value');
$c2 = new SimpleModelTestClass();
$c1->setField1("another")->setField2('object');
$parent = new ListModelTestClass();
$parent->setList1(array('simple', 'list', 'with', 'integer', 'keys'));
$parent->setList2(array($c1, $c2));
$parentCopy = new ListModelTestClass();
$parentCopy->fromJson($parent->toJSON());
$this->assertEquals($parent, $parentCopy);
}
}

View File

@@ -0,0 +1,126 @@
<?php
use PayPal\Core\PPConfigManager;
/**
* Test class for PPConfigManager.
* @preserveGlobalState disabled
* @runTestsInSeparateProcesses
*/
class PPConfigManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PPConfigManager
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$loader = require dirname(__DIR__) . '/../../../vendor/autoload.php';
$loader->add('PayPal\\Test', __DIR__);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @test
*/
public function testGetInstance()
{
define("PP_CONFIG_PATH", dirname(dirname(dirname(__DIR__))));
$this->object = PPConfigManager::getInstance();
$instance = $this->object->getInstance();
$instance2 = $this->object->getInstance();
$this->assertTrue($instance instanceof PPConfigManager);
$this->assertSame($instance, $instance2);
}
/**
* @test
*/
public function testGet()
{
define("PP_CONFIG_PATH", dirname(dirname(dirname(__DIR__))));
$this->object = PPConfigManager::getInstance();
$ret = $this->object->get('acct2');
$this->assertConfiguration(['acct2.ClientId' => 'TestClientId', 'acct2.ClientSecret' => 'TestClientSecret'], $ret);
$this->assertTrue(sizeof($ret) == 2);
}
/**
* @test
*/
public function testGetIniPrefix()
{
define("PP_CONFIG_PATH", dirname(dirname(dirname(__DIR__))));
$this->object = PPConfigManager::getInstance();
$ret = $this->object->getIniPrefix();
$this->assertContains('acct1', $ret);
$this->assertEquals(sizeof($ret), 2);
$ret = $this->object->getIniPrefix('TestClientId');
$this->assertEquals('acct2', $ret);
}
/**
* @test
*/
public function testConfigByDefault()
{
define("PP_CONFIG_PATH", dirname(dirname(dirname(__DIR__))));
$this->object = PPConfigManager::getInstance();
// Test file based config params and defaults
$config = PPConfigManager::getInstance()->getConfigHashmap();
$this->assertConfiguration(['mode' => 'sandbox', 'http.ConnectionTimeOut' => '60'], $config);
}
public function testConfigByCustom()
{
define("PP_CONFIG_PATH", dirname(dirname(dirname(__DIR__))));
$this->object = PPConfigManager::getInstance();
// Test custom config params and defaults
$config = PPConfigManager::getInstance()->addConfigs(['mode' => 'custom', 'http.ConnectionTimeOut' => 900])->getConfigHashmap();
$this->assertConfiguration(['mode' => 'custom', 'http.ConnectionTimeOut' => '900'], $config);
}
public function testConfigByFileAndCustom() {
define("PP_CONFIG_PATH", __DIR__. '/non_existent/');
$this->object = PPConfigManager::getInstance();
$config = PPConfigManager::getInstance()->getConfigHashmap();
$this->assertArrayHasKey('http.ConnectionTimeOut', $config);
$this->assertEquals('30', $config['http.ConnectionTimeOut']);
$this->assertEquals('5', $config['http.Retry']);
//Add more configs
$config = PPConfigManager::getInstance()->addConfigs(['http.Retry' => "10", 'mode' => 'sandbox'])->getConfigHashmap();
$this->assertConfiguration(['http.ConnectionTimeOut' => "30", 'http.Retry' => "10", 'mode' => 'sandbox'], $config);
}
/**
* Asserts if each configuration is available and has expected value.
*
* @param $conditions
* @param $config
*/
public function assertConfiguration($conditions, $config) {
foreach($conditions as $key => $value) {
$this->assertArrayHasKey($key, $config);
$this->assertEquals($value, $config[$key]);
}
}
}
?>

View File

@@ -0,0 +1,145 @@
<?php
use PayPal\Core\PPCredentialManager;
/**
* Test class for PPCredentialManager.
*
* @runTestsInSeparateProcesses
*/
class PPCredentialManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PPCredentialManager
*/
protected $object;
private $config = array(
'acct1.ClientId' => 'client-id',
'acct1.ClientSecret' => 'client-secret',
'http.ConnectionTimeOut' => '30',
'http.Retry' => '5',
'service.RedirectURL' => 'https://www.sandbox.paypal.com/webscr&cmd=',
'service.DevCentralURL' => 'https://developer.paypal.com',
'service.EndPoint.IPN' => 'https://www.sandbox.paypal.com/cgi-bin/webscr',
'service.EndPoint.AdaptivePayments' => 'https://svcs.sandbox.paypal.com/',
'service.SandboxEmailAddress' => 'platform_sdk_seller@gmail.com',
'log.FileName' => 'PayPal.log',
'log.LogLevel' => 'INFO',
'log.LogEnabled' => '1',
);
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = PPCredentialManager::getInstance($this->config);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @test
*/
public function testGetInstance()
{
$instance = $this->object->getInstance($this->config);
$this->assertTrue($instance instanceof PPCredentialManager);
}
/**
* @test
*/
public function testGetSpecificCredentialObject()
{
$cred = $this->object->getCredentialObject('acct1');
$this->assertNotNull($cred);
$this->assertAttributeEquals('client-id', 'clientId', $cred);
$this->assertAttributeEquals('client-secret', 'clientSecret', $cred);
}
/**
* @after testGetDefaultCredentialObject
*
* @throws \PayPal\Exception\PPInvalidCredentialException
*/
public function testSetCredentialObject() {
$authObject = $this->getMockBuilder('\Paypal\Auth\OAuthTokenCredential')
->disableOriginalConstructor()
->getMock();
$cred = $this->object->setCredentialObject($authObject)->getCredentialObject();
$this->assertNotNull($cred);
$this->assertSame($this->object->getCredentialObject(), $authObject);
}
/**
* @after testGetDefaultCredentialObject
*
* @throws \PayPal\Exception\PPInvalidCredentialException
*/
public function testSetCredentialObjectWithUserId() {
$authObject = $this->getMockBuilder('\Paypal\Auth\OAuthTokenCredential')
->disableOriginalConstructor()
->getMock();
$cred = $this->object->setCredentialObject($authObject, 'sample')->getCredentialObject('sample');
$this->assertNotNull($cred);
$this->assertSame($this->object->getCredentialObject(), $authObject);
}
/**
* @after testGetDefaultCredentialObject
*
* @throws \PayPal\Exception\PPInvalidCredentialException
*/
public function testSetCredentialObjectWithoutDefault() {
$authObject = $this->getMockBuilder('\Paypal\Auth\OAuthTokenCredential')
->disableOriginalConstructor()
->getMock();
$cred = $this->object->setCredentialObject($authObject, null, false)->getCredentialObject();
$this->assertNotNull($cred);
$this->assertNotSame($this->object->getCredentialObject(), $authObject);
}
/**
* @test
*/
public function testGetInvalidCredentialObject()
{
$this->setExpectedException('PayPal\Exception\PPInvalidCredentialException');
$cred = $this->object->getCredentialObject('invalid_biz_api1.gmail.com');
}
/**
*
*/
public function testGetDefaultCredentialObject()
{
$cred = $this->object->getCredentialObject();
$this->assertNotNull($cred);
$this->assertAttributeEquals('client-id', 'clientId', $cred);
$this->assertAttributeEquals('client-secret', 'clientSecret', $cred);
}
/**
* @test
*/
public function testGetRestCredentialObject() {
$cred = $this->object->getCredentialObject('acct1');
$this->assertNotNull($cred);
$this->assertAttributeEquals($this->config['acct1.ClientId'], 'clientId', $cred);
$this->assertAttributeEquals($this->config['acct1.ClientSecret'], 'clientSecret', $cred);
}
}
?>

View File

@@ -0,0 +1,121 @@
<?php
use PayPal\Core\PPHttpConfig;
/**
* Test class for PPAPIService.
*
*/
class PPHttpConfigTest extends PHPUnit_Framework_TestCase
{
protected $object;
private $config = array(
'http.ConnectionTimeOut' => '30',
'http.Retry' => '5' ,
);
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @test
*/
public function testHeaderFunctions()
{
$o = new PPHttpConfig();
$o->addHeader('key1', 'value1');
$o->addHeader('key2', 'value');
$o->addHeader('key2', 'overwritten');
$this->assertEquals(2, count($o->getHeaders()));
$this->assertEquals('overwritten', $o->getHeader('key2'));
$this->assertNull($o->getHeader('key3'));
$o = new PPHttpConfig();
$o->addHeader('key1', 'value1');
$o->addHeader('key2', 'value');
$o->addHeader('key2', 'and more', false);
$this->assertEquals(2, count($o->getHeaders()));
$this->assertEquals('value;and more', $o->getHeader('key2'));
$o->removeHeader('key2');
$this->assertEquals(1, count($o->getHeaders()));
$this->assertNull($o->getHeader('key2'));
}
/**
* @test
*/
public function testCurlOpts()
{
$o = new PPHttpConfig();
$o->setCurlOptions(array('k' => 'v'));
$curlOpts = $o->getCurlOptions();
$this->assertEquals(1, count($curlOpts));
$this->assertEquals('v', $curlOpts['k']);
}
/**
* @test
*/
public function testUserAgent()
{
$ua = 'UAString';
$o = new PPHttpConfig();
$o->setUserAgent($ua);
$curlOpts= $o->getCurlOptions();
$this->assertEquals($ua, $curlOpts[CURLOPT_USERAGENT]);
}
/**
* @test
*/
public function testSSLOpts()
{
$sslCert = '../cacert.pem';
$sslPass = 'passPhrase';
$o = new PPHttpConfig();
$o->setSSLCert($sslCert, $sslPass);
$curlOpts= $o->getCurlOptions();
$this->assertArrayHasKey(CURLOPT_SSLCERT, $curlOpts);
$this->assertEquals($sslPass, $curlOpts[CURLOPT_SSLCERTPASSWD]);
}
/**
* @test
*/
public function testProxyOpts()
{
$proxy = 'http://me:secret@hostname:8081';
$o = new PPHttpConfig();
$o->setHttpProxy($proxy);
$curlOpts= $o->getCurlOptions();
$this->assertEquals('hostname:8081', $curlOpts[CURLOPT_PROXY]);
$this->assertEquals('me:secret', $curlOpts[CURLOPT_PROXYUSERPWD]);
$this->setExpectedException('PayPal\Exception\PPConfigurationException');
$o->setHttpProxy('invalid string');
}
}
?>

View File

@@ -0,0 +1,64 @@
<?php
use PayPal\Core\PPLoggingManager;
/**
* Test class for PPLoggingManager.
*
*/
class PPLoggingManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PPLoggingManager
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new PPLoggingManager('InvoiceTest');
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @test
*/
public function testError()
{
$this->object->error('Test Error Message');
}
/**
* @test
*/
public function testWarning()
{
$this->object->warning('Test Warning Message');
}
/**
* @test
*/
public function testInfo()
{
$this->object->info('Test info Message');
}
/**
* @test
*/
public function testFine()
{
$this->object->fine('Test fine Message');
}
}
?>

View File

@@ -0,0 +1,35 @@
<?php
use PayPal\Exception\PPConfigurationException;
/**
* Test class for PPConfigurationException.
*
*/
class PPConfigurationExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PPConfigurationException
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new PPConfigurationException('Test PPConfigurationException');
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
public function testPPConfigurationException()
{
$this->assertEquals('Test PPConfigurationException', $this->object->getMessage());
}
}
?>

View File

@@ -0,0 +1,48 @@
<?php
use PayPal\Exception\PPConnectionException;
/**
* Test class for PPConnectionException.
*
*/
class PPConnectionExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PPConnectionException
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new PPConnectionException('http://testURL', 'test message');
$this->object->setData('response payload for connection');
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @test
*/
public function testGetUrl()
{
$this->assertEquals('http://testURL', $this->object->getUrl());
}
/**
* @test
*/
public function testGetData()
{
$this->assertEquals('response payload for connection', $this->object->getData());
}
}
?>

View File

@@ -0,0 +1,40 @@
<?php
use PayPal\Exception\PPInvalidCredentialException;
/**
* Test class for PPInvalidCredentialException.
*
*/
class PPInvalidCredentialExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PPInvalidCredentialException
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new PPInvalidCredentialException;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @test
*/
public function testErrorMessage()
{
$msg = $this->object->errorMessage();
$this->assertContains('Error on line', $msg);
}
}
?>

View File

@@ -0,0 +1,41 @@
<?php
use PayPal\Exception\PPMissingCredentialException;
/**
* Test class for PPMissingCredentialException.
*
*/
class PPMissingCredentialExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PPMissingCredentialException
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new PPMissingCredentialException;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @test
*/
public function testErrorMessage()
{
$msg = $this->object->errorMessage();
$this->assertContains('Error on line', $msg);
}
}
?>

View File

@@ -3,4 +3,6 @@
// Include the composer autoloader
$loader = require dirname(__DIR__) . '/vendor/autoload.php';
$loader->add('PayPal\\Test', __DIR__);
define("PP_CONFIG_PATH", __DIR__);
if (!defined("PP_CONFIG_PATH")) {
define("PP_CONFIG_PATH", __DIR__);
}

View File

@@ -3,13 +3,16 @@
acct1.ClientId = EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM
acct1.ClientSecret = EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM
acct2.ClientId = TestClientId
acct2.ClientSecret = TestClientSecret
;Connection Information
[Http]
http.ConnectionTimeOut = 30
http.ConnectionTimeOut = 60
http.Retry = 1
;http.Proxy=http://[username:password]@hostname[:port][/path]
mode=sandbox
;Service Configuration
[Service]