Skip to content

Commit b8fdbcd

Browse files
committed
Refactoring and clean up of sample test code
1 parent 80375a1 commit b8fdbcd

6 files changed

Lines changed: 237 additions & 120 deletions

File tree

classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
//Utils
219219
//'net\authorize\util\ObjectToXml' => $libDir . 'net/authorize/util/ObjectToXml.php',
220220
'net\authorize\util\HttpClient' => $libDir . 'net/authorize/util/HttpClient.php',
221+
'net\authorize\util\Helpers' => $libDir . 'net/authorize/util/Helpers.php',
221222

222223
//constants
223224
'net\authorize\api\constants\ANetEnvironment' => $libDir . 'net/authorize/api/constants/ANetEnvironment.php',

lib/net/authorize/api/controller/base/ApiOperationBase.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ abstract class ApiOperationBase implements IApiOperation
3636
*/
3737
public $httpClient = null;
3838

39+
private $_log_file = false;
3940

4041
/**
4142
* Constructor.
@@ -46,6 +47,9 @@ abstract class ApiOperationBase implements IApiOperation
4647
*/
4748
public function __construct(\net\authorize\api\contract\v1\AnetApiRequestType $request, $responseType)
4849
{
50+
date_default_timezone_set('UTC');
51+
$this->_log_file = (defined('AUTHORIZENET_LOG_FILE') ? AUTHORIZENET_LOG_FILE : false);
52+
4953
if ( null == $request)
5054
{
5155
throw new InvalidArgumentException( "request cannot be null");
@@ -62,8 +66,7 @@ public function __construct(\net\authorize\api\contract\v1\AnetApiRequestType $r
6266
}
6367

6468
$this->apiRequest = $request;
65-
66-
self::validate();
69+
$this->validate();
6770

6871
$this->apiResponseType = $responseType;
6972
$this->httpClient = new HttpClient;
@@ -106,7 +109,9 @@ public function execute($endPoint = \net\authorize\api\constants\ANetEnvironment
106109
{
107110
$this->beforeExecute();
108111

112+
file_put_contents($this->_log_file, sprintf("\n%s: Request Serialization Begin", $this->now()), FILE_APPEND);
109113
$xmlRequest = $this->serializer->serialize($this->apiRequest, 'xml');
114+
file_put_contents($this->_log_file, sprintf("\n%s: Request Serialization End", $this->now()), FILE_APPEND);
110115
/*
111116
//$xmlRequest = '<?xml version="1.0" encoding="UTF-8"?> <ARBGetSubscriptionListRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"> <merchantAuthentication> <name>4YJmeW7V77us</name> <transactionKey>4qHK9u63F753be4Z</transactionKey> </merchantAuthentication> <refId><![CDATA[ref1416999093]]></refId> <searchType><![CDATA[subscriptionActive]]></searchType> <sorting> <orderBy><![CDATA[firstName]]></orderBy> <orderDescending>false</orderDescending> </sorting> <paging> <limit>10</limit> <offset>1</offset> </paging> </ARBGetSubscriptionListRequest> ';
112117
*/
@@ -116,7 +121,9 @@ public function execute($endPoint = \net\authorize\api\constants\ANetEnvironment
116121
{
117122
throw new \Exception( "Error getting valid response from api. Check log file for error details");
118123
}
124+
file_put_contents($this->_log_file, sprintf("\n%s: Response De-Serialization Begin", $this->now()), FILE_APPEND);
119125
$this->apiResponse = $this->serializer->deserialize( $xmlResponse, $this->apiResponseType , 'xml');
126+
file_put_contents($this->_log_file, sprintf("\n%s: Response De-Serialization End", $this->now()), FILE_APPEND);
120127

121128
$this->afterExecute();
122129
}
@@ -135,4 +142,9 @@ private function validate()
135142
protected function beforeExecute() {}
136143
protected function afterExecute() {}
137144
protected function validateRequest() {} //need to make this abstract
145+
146+
protected function now()
147+
{
148+
return date( DATE_RFC2822);
149+
}
138150
}

lib/net/authorize/util/Helpers.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace net\authorize\util;
3+
4+
/**
5+
* A class defining helpers
6+
*
7+
* @package AuthorizeNet
8+
* @subpackage net\authorize\util
9+
*/
10+
class Helpers
11+
{
12+
private static $initialized = false;
13+
14+
/**
15+
* @return string current date-time
16+
*/
17+
public static function now()
18+
{
19+
//init only once
20+
if ( ! self::$initialized)
21+
{
22+
date_default_timezone_set('UTC');
23+
self::$initialized = true;
24+
}
25+
return date( DATE_RFC2822);
26+
}
27+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
namespace net\authorize\api\controller\test;
3+
4+
use net\authorize\api\contract\v1 AS apiContract;
5+
use net\authorize\api\controller AS apiController;
6+
7+
require_once __DIR__ . '/../../../../../autoload.php';
8+
//include if tests/bootstrap.php is not loaded automatically
9+
//require_once __DIR__ . '/../../../../bootstrap.php';
10+
11+
class ApiCoreTestBase extends \PHPUnit_Framework_TestCase
12+
{
13+
protected $paymentOne = null;
14+
protected $orderType = null;
15+
protected $customerDataOne = null;
16+
protected $customerAddressOne = null;
17+
protected $payPalOne = null;
18+
19+
protected $counter = 0;
20+
protected $counterStr = "0";
21+
protected $refId = '';
22+
23+
protected static $LoginName = '';
24+
protected static $TransactionKey = '';
25+
26+
protected static $TestEnvironment = \net\authorize\api\constants\ANetEnvironment::SANDBOX;
27+
protected static $log_file = false;
28+
29+
public static function setUpBeforeClass()
30+
{
31+
if (!defined('AUTHORIZENET_LOG_FILE'))
32+
{
33+
define( "AUTHORIZENET_LOG_FILE", __DIR__ . "/../../../../../log/authorize-net.log");
34+
}
35+
//self::$name = (defined('AUTHORIZENET_API_LOGIN_ID') && ''!=AUTHORIZENET_API_LOGIN_ID) ? AUTHORIZENET_API_LOGIN_ID : getenv("api_login_id");
36+
//self::$transactionKey = (defined('AUTHORIZENET_TRANSACTION_KEY') && ''!=AUTHORIZENET_TRANSACTION_KEY) ? AUTHORIZENET_TRANSACTION_KEY : getenv("transaction_key");
37+
self::$LoginName = '9q2yWeA3J3Q';
38+
self::$TransactionKey = '7KTb8aThMJ35y879';
39+
self::$log_file = (defined('AUTHORIZENET_LOG_FILE') ? AUTHORIZENET_LOG_FILE : false);
40+
}
41+
42+
public static function tearDownAfterClass()
43+
{
44+
}
45+
46+
public function __construct()
47+
{
48+
}
49+
50+
protected function setUp()
51+
{
52+
$this->refId = 'ref' . time();
53+
54+
$this->counter = rand();
55+
$this->counterStr = sprintf("%s", $this->counter);
56+
57+
$driversLicenseOne = new apiContract\DriversLicenseType();
58+
$driversLicenseOne->setNumber( $this->getRandomString( "DL-" ));
59+
$driversLicenseOne->setState( "WA");
60+
$driversLicenseOne->setDateOfBirth( "01/01/1960");
61+
62+
$customerOne = new apiContract\CustomerType();
63+
$customerOne->setType("individual"); //TODO: CHANGE TO ENUM
64+
$customerOne->setId( $this->getRandomString( "Id" ));
65+
$customerOne->setEmail ( $this->counterStr . ".customerOne@test.anet.net");
66+
$customerOne->setPhoneNumber("1234567890");
67+
$customerOne->setFaxNumber("1234567891");
68+
//$customerOne->setDriversLicense( $driversLicenseOne);
69+
$customerOne->setTaxId( "911011011");
70+
71+
$creditCardOne = new apiContract\CreditCardType();
72+
$creditCardOne->setCardNumber( "4111111111111111" );
73+
$creditCardOne->setExpirationDate( "2038-12");
74+
75+
$this->paymentOne = new apiContract\PaymentType();
76+
$this->paymentOne->setCreditCard( $creditCardOne);
77+
78+
$this->orderType = new apiContract\OrderType();
79+
$this->orderType->setInvoiceNumber( $this->getRandomString( "Inv:" ));
80+
$this->orderType->setDescription( $this->getRandomString( "Description" ));
81+
82+
$this->customerDataOne = new apiContract\CustomerDataType();
83+
//$this->customerDataOne->setDriversLicense( $customerOne->getDriversLicense());
84+
$this->customerDataOne->setEmail( $customerOne->getEmail());
85+
$this->customerDataOne->setId( $customerOne->getId());
86+
$this->customerDataOne->setTaxId( $customerOne->getTaxId());
87+
$this->customerDataOne->setType( $customerOne->getType());
88+
89+
$this->customerAddressOne = new apiContract\CustomerAddressType();
90+
$this->customerAddressOne->setFirstName( $this->getRandomString( "FName" ));
91+
$this->customerAddressOne->setLastName( $this->getRandomString( "LName" ));
92+
$this->customerAddressOne->setCompany( $this->getRandomString( "Company" ));
93+
$this->customerAddressOne->setAddress( $this->getRandomString( "StreetAdd" ));
94+
$this->customerAddressOne->setCity( "Bellevue");
95+
$this->customerAddressOne->setState( "WA");
96+
$this->customerAddressOne->setZip( "98004");
97+
$this->customerAddressOne->setCountry( "USA");
98+
$this->customerAddressOne->setPhoneNumber( $customerOne->getPhoneNumber());
99+
$this->customerAddressOne->setFaxNumber( $customerOne->getFaxNumber());
100+
101+
$this->payPalOne = new apiContract\PayPalType();
102+
$this->payPalOne->setPaypalLc( "IT" );
103+
$this->payPalOne->setPaypalPayflowcolor( "FFFFF0");
104+
$this->payPalOne->setSuccessUrl( $this->getRandomString( "http://success.anet.net"));
105+
$this->payPalOne->setCancelUrl( $this->getRandomString( "http://cancel.anet.net"));
106+
//payPalHdrImg = GetRandomString("Hdr"),
107+
//payerID = GetRandomString("PayerId"),
108+
}
109+
110+
protected function tearDown()
111+
{
112+
113+
}
114+
115+
protected function getRandomString( $title)
116+
{
117+
return sprintf( "%s%s", $title, $this->counterStr);
118+
}
119+
120+
protected function setValidAmount( $number)
121+
{
122+
return min( $number, self::MaxTransactionAmount);
123+
}
124+
125+
const MaxTransactionAmount = 10000; //214747;
126+
}

0 commit comments

Comments
 (0)