Ejegg has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/390331 )

Change subject: Get Amazon SDK object from SmashPig config
......................................................................

Get Amazon SDK object from SmashPig config

De-duplicating some more config settings and testing code!
This will be an easy way to get proxy parameters.

Bug: T180168
Change-Id: Ib4d93306293dd0fe32785a27572f49cb84754b5a
---
M DonationInterface.class.php
M amazon_gateway/amazon.adapter.php
M tests/phpunit/Adapter/Amazon/AmazonApiTest.php
M tests/phpunit/Adapter/Amazon/AmazonTest.php
M tests/phpunit/DonationInterfaceApiTestCase.php
M tests/phpunit/DonationInterfaceTestCase.php
M tests/phpunit/TestConfiguration.php
D tests/phpunit/includes/MockAmazonClient.php
D tests/phpunit/includes/MockAmazonResponse.php
D tests/phpunit/includes/test_gateway/TestingAmazonAdapter.php
10 files changed, 50 insertions(+), 252 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DonationInterface 
refs/changes/31/390331/1

diff --git a/DonationInterface.class.php b/DonationInterface.class.php
index 84d72a3..44821e1 100644
--- a/DonationInterface.class.php
+++ b/DonationInterface.class.php
@@ -70,10 +70,7 @@
                $wgAutoloadClasses['DonationInterfaceTestCase'] = $testDir . 
'DonationInterfaceTestCase.php';
                $wgAutoloadClasses['DonationInterfaceApiTestCase'] = $testDir . 
'DonationInterfaceApiTestCase.php';
                $wgAutoloadClasses['BaseIngenicoTestCase'] = $testDir . 
'BaseIngenicoTestCase.php';
-               $wgAutoloadClasses['MockAmazonClient'] = $testDir . 
'includes/MockAmazonClient.php';
-               $wgAutoloadClasses['MockAmazonResponse'] = $testDir . 
'includes/MockAmazonResponse.php';
                $wgAutoloadClasses['TestingAdyenAdapter'] = $testDir . 
'includes/test_gateway/TestingAdyenAdapter.php';
-               $wgAutoloadClasses['TestingAmazonAdapter'] = $testDir . 
'includes/test_gateway/TestingAmazonAdapter.php';
                $wgAutoloadClasses['TestingAstroPayAdapter'] = $testDir . 
'includes/test_gateway/TestingAstroPayAdapter.php';
                $wgAutoloadClasses['TestingDonationLogger'] = $testDir . 
'includes/TestingDonationLogger.php';
                $wgAutoloadClasses['TestingGatewayPage'] = $testDir . 
'includes/TestingGatewayPage.php';
diff --git a/amazon_gateway/amazon.adapter.php 
b/amazon_gateway/amazon.adapter.php
index d4001e4..30914d5 100644
--- a/amazon_gateway/amazon.adapter.php
+++ b/amazon_gateway/amazon.adapter.php
@@ -3,6 +3,7 @@
 use PayWithAmazon\PaymentsClient as PwaClient;
 use PayWithAmazon\PaymentsClientInterface as PwaClientInterface;
 use Psr\Log\LogLevel;
+use SmashPig\Core\Context;
 
 /**
  * Wikimedia Foundation
@@ -145,14 +146,7 @@
         * @return PwaClientInterface
         */
        protected function getPwaClient() {
-               return new PwaClient( array(
-                       'merchant_id' => $this->account_config['SellerID'],
-                       'access_key' => $this->account_config['MWSAccessKey'],
-                       'secret_key' => $this->account_config['MWSSecretKey'],
-                       'client_id' => $this->account_config['ClientID'],
-                       'region' => $this->account_config['Region'],
-                       'sandbox' => $this->getGlobal( 'Test' ),
-               ) );
+               return Context::get()->getProviderConfiguration()->object( 
'payments-client' );
        }
 
        /**
diff --git a/tests/phpunit/Adapter/Amazon/AmazonApiTest.php 
b/tests/phpunit/Adapter/Amazon/AmazonApiTest.php
index fafc6db..acff8af 100644
--- a/tests/phpunit/Adapter/Amazon/AmazonApiTest.php
+++ b/tests/phpunit/Adapter/Amazon/AmazonApiTest.php
@@ -11,14 +11,14 @@
  * @group medium
  */
 class AmazonApiTest extends DonationInterfaceApiTestCase {
+       /**
+        * @var \SmashPig\PaymentProviders\Amazon\Tests\AmazonTestConfiguration
+        */
+       protected $providerConfig;
+
        public function setUp() {
                parent::setUp();
-               TestingAmazonAdapter::$mockClient = new MockAmazonClient();
-       }
-
-       public function tearDown() {
-               TestingAmazonAdapter::$mockClient = null;
-               parent::tearDown();
+               $this->providerConfig = 
DonationInterface_Adapter_Amazon_Test::setUpAmazonTestingContext( $this );
        }
 
        public function testDoPaymentSuccess() {
@@ -43,7 +43,7 @@
                $apiResult = $this->doApiRequest( $params, $session );
                $redirect = $apiResult[0]['redirect'];
                $this->assertEquals( 
'https://wikimediafoundation.org/wiki/Thank_You/en?country=US', $redirect );
-               $mockClient = TestingAmazonAdapter::$mockClient;
+               $mockClient = $this->providerConfig->object( 'payments-client' 
);
                $setOrderReferenceDetailsArgs = 
$mockClient->calls['setOrderReferenceDetails'][0];
                $oid = $session['Donor']['contribution_tracking_id'] . '-1';
                $this->assertEquals( $oid, 
$setOrderReferenceDetailsArgs['seller_order_id'], 'Did not set order id on 
order reference' );
diff --git a/tests/phpunit/Adapter/Amazon/AmazonTest.php 
b/tests/phpunit/Adapter/Amazon/AmazonTest.php
index b688acc..a949780 100644
--- a/tests/phpunit/Adapter/Amazon/AmazonTest.php
+++ b/tests/phpunit/Adapter/Amazon/AmazonTest.php
@@ -27,24 +27,33 @@
  */
 class DonationInterface_Adapter_Amazon_Test extends DonationInterfaceTestCase {
 
+       protected $testAdapterClass = 'AmazonAdapter';
        /**
-        * @param $name string The name of the test case
-        * @param $data array Any parameters read from a dataProvider
-        * @param $dataName string|int The name or index of the data set
+        * @var \SmashPig\PaymentProviders\Amazon\Tests\AmazonTestConfiguration
         */
-       public function __construct( $name = null, array $data = array(), 
$dataName = '' ) {
-               parent::__construct( $name, $data, $dataName );
-               $this->testAdapterClass = 'TestingAmazonAdapter';
+       protected $providerConfig;
+
+       public static function setUpAmazonTestingContext( $testCase ) {
+               $config = AmazonTestConfiguration::instance(
+                       $testCase->smashPigGlobalConfig
+               );
+               $config->override( [
+                       'payments-client' => [
+                               'constructor-parameters' => [
+                                       0 => [
+                                               'response-directory' =>
+                                                       __DIR__ . 
'/../../includes/Responses/amazon'
+                                       ]
+                               ]
+                       ]
+               ] );
+               TestingContext::get()->providerConfigurationOverride = $config;
+               return $config;
        }
 
        public function setUp() {
                parent::setUp();
-               TestingContext::get()->providerConfigurationOverride =
-                       AmazonTestConfiguration::instance(
-                               $this->smashPigGlobalConfig
-                       );
-
-               TestingAmazonAdapter::$mockClient = new MockAmazonClient();
+               $this->providerConfig = self::setUpAmazonTestingContext( $this 
);
 
                $this->setMwGlobals( array(
                        'wgAmazonGatewayEnabled' => true,
@@ -64,11 +73,6 @@
                ) );
        }
 
-       public function tearDown() {
-               TestingAmazonAdapter::$fakeGlobals = array();
-               parent::tearDown();
-       }
-
        /**
         * Integration test to verify that the Amazon gateway converts Canadian
         * dollars before redirecting
@@ -76,6 +80,7 @@
         * FIXME: Merge with currency fallback tests?
         *
         * @dataProvider canadaLanguageProvider
+        * @param string $language language code to test
         */
        function testCanadianDollarConversion( $language ) {
                $init = $this->getDonorTestData( 'CA' );
@@ -87,11 +92,10 @@
                $cadRate = $rates['CAD'];
 
                $expectedAmount = floor( $init['amount'] / $cadRate );
-
-               TestingAmazonAdapter::$fakeGlobals = array(
-                       'FallbackCurrency' => 'USD',
-                       'NotifyOnConvert' => true,
-               );
+               $this->setMwGlobals( array(
+                       'wgAmazonGatewayFallbackCurrency' => 'USD',
+                       'wgAmazonGatewayNotifyOnConvert' => true,
+               ) );
 
                $expectedNotification = wfMessage(
                        'donate_interface-fallback-currency-notice',
@@ -151,7 +155,7 @@
                $this->assertEquals( 'Testy', 
$gateway->getData_Unstaged_Escaped( 'first_name' ), 'Did not populate first 
name from Amazon data' );
                $this->assertEquals( 'Test', 
$gateway->getData_Unstaged_Escaped( 'last_name' ), 'Did not populate last name 
from Amazon data' );
                $this->assertEquals( 'nob...@wikimedia.org', 
$gateway->getData_Unstaged_Escaped( 'email' ), 'Did not populate email from 
Amazon data' );
-               $mockClient = TestingAmazonAdapter::$mockClient;
+               $mockClient = $this->providerConfig->object( 'payments-client' 
);
                $setOrderReferenceDetailsArgs = 
$mockClient->calls['setOrderReferenceDetails'][0];
                $oid = $gateway->getData_Unstaged_Escaped( 'order_id' );
                $this->assertEquals( $oid, 
$setOrderReferenceDetailsArgs['seller_order_id'], 'Did not set order id on 
order reference' );
@@ -175,7 +179,7 @@
                unset( $init['first_name'] );
                unset( $init['last_name'] );
 
-               $mockClient = TestingAmazonAdapter::$mockClient;
+               $mockClient = $this->providerConfig->object( 'payments-client' 
);
                $mockClient->returns['authorize'][] = 'InvalidPaymentMethod';
 
                $gateway = $this->getFreshGatewayObject( $init );
@@ -202,7 +206,7 @@
                unset( $init['first_name'] );
                unset( $init['last_name'] );
 
-               $mockClient = TestingAmazonAdapter::$mockClient;
+               $mockClient = $this->providerConfig->object( 'payments-client' 
);
                $mockClient->returns['authorize'][] = 'AmazonRejected';
 
                $gateway = $this->getFreshGatewayObject( $init );
@@ -227,7 +231,7 @@
                unset( $init['first_name'] );
                unset( $init['last_name'] );
 
-               $mockClient = TestingAmazonAdapter::$mockClient;
+               $mockClient = $this->providerConfig->object( 'payments-client' 
);
                $mockClient->returns['authorize'][] = 'TransactionTimedOut';
 
                $gateway = $this->getFreshGatewayObject( $init );
@@ -248,7 +252,7 @@
                unset( $init['first_name'] );
                unset( $init['last_name'] );
 
-               $mockClient = TestingAmazonAdapter::$mockClient;
+               $mockClient = $this->providerConfig->object( 'payments-client' 
);
                $mockClient->exceptions['authorize'][] = new Exception( 'Test' 
);
 
                $gateway = $this->getFreshGatewayObject( $init );
@@ -283,7 +287,7 @@
                $this->assertEquals( 'Testy', 
$gateway->getData_Unstaged_Escaped( 'first_name' ), 'Did not populate first 
name from Amazon data' );
                $this->assertEquals( 'Test', 
$gateway->getData_Unstaged_Escaped( 'last_name' ), 'Did not populate last name 
from Amazon data' );
                $this->assertEquals( 'nob...@wikimedia.org', 
$gateway->getData_Unstaged_Escaped( 'email' ), 'Did not populate email from 
Amazon data' );
-               $mockClient = TestingAmazonAdapter::$mockClient;
+               $mockClient = $this->providerConfig->object( 'payments-client' 
);
                $setBillingAgreementDetailsArgs = 
$mockClient->calls['setBillingAgreementDetails'][0];
                $oid = $gateway->getData_Unstaged_Escaped( 'order_id' );
                $this->assertEquals( $oid, 
$setBillingAgreementDetailsArgs['seller_billing_agreement_id'], 'Did not set 
order id on billing agreement' );
diff --git a/tests/phpunit/DonationInterfaceApiTestCase.php 
b/tests/phpunit/DonationInterfaceApiTestCase.php
index a5509bf..5f4d233 100644
--- a/tests/phpunit/DonationInterfaceApiTestCase.php
+++ b/tests/phpunit/DonationInterfaceApiTestCase.php
@@ -5,11 +5,12 @@
 use SmashPig\Tests\TestingGlobalConfiguration;
 
 class DonationInterfaceApiTestCase extends ApiTestCase {
+       public $smashPigGlobalConfig;
 
        public function setUp() {
                parent::setUp();
-               $config = TestingGlobalConfiguration::create();
-               TestingContext::init( $config );
+               $this->smashPigGlobalConfig = 
TestingGlobalConfiguration::create();
+               TestingContext::init( $this->smashPigGlobalConfig );
                $ctx = TestingContext::get();
                $ctx->setSourceType( 'payments' );
                $ctx->setSourceName( 'DonationInterface' );
diff --git a/tests/phpunit/DonationInterfaceTestCase.php 
b/tests/phpunit/DonationInterfaceTestCase.php
index 5ab3999..2ec16f8 100644
--- a/tests/phpunit/DonationInterfaceTestCase.php
+++ b/tests/phpunit/DonationInterfaceTestCase.php
@@ -51,7 +51,7 @@
        protected $gatewayAdapter;
 
        protected $testAdapterClass = TESTS_ADAPTER_DEFAULT;
-       protected $smashPigGlobalConfig;
+       public $smashPigGlobalConfig;
 
        /**
         * @param $name string The name of the test case
@@ -69,7 +69,6 @@
        public static function resetTestingAdapters() {
                $testing_adapters = array(
                        'TestingAdyenAdapter',
-                       'TestingAmazonAdapter',
                        'TestingAstroPayAdapter',
                        'TestingGenericAdapter',
                        'TestingGlobalCollectAdapter',
diff --git a/tests/phpunit/TestConfiguration.php 
b/tests/phpunit/TestConfiguration.php
index 0323198..0e041d4 100644
--- a/tests/phpunit/TestConfiguration.php
+++ b/tests/phpunit/TestConfiguration.php
@@ -66,6 +66,8 @@
        $wgPaypalExpressGatewayAccountInfo,
        $wgAmazonGatewayReturnURL,
        $wgAmazonGatewayAccountInfo,
+       $wgAmazonGatewayFallbackCurrency,
+       $wgAmazonGatewayNotifyOnConvert,
        $wgAdyenGatewayURL,
        $wgAdyenGatewayAccountInfo,
        $wgAstroPayGatewayURL,
@@ -97,7 +99,7 @@
 $wgDonationInterfaceGatewayAdapters = array(
        'globalcollect' => 'TestingGlobalCollectAdapter',
        'ingenico' => 'IngenicoAdapter',
-       'amazon' => 'TestingAmazonAdapter',
+       'amazon' => 'AmazonAdapter',
        'adyen' => 'TestingAdyenAdapter',
        'astropay' => 'TestingAstroPayAdapter',
        'paypal_ec' => 'TestingPaypalExpressAdapter',
@@ -150,6 +152,8 @@
                
'https://static-na.payments-amazon.com/OffAmazonPayments/us/sandbox/js/Widgets.js',
        'ReturnURL' => 
"https://example.org/index.php/Special:AmazonGateway?debug=true";,
 );
+$wgAmazonGatewayFallbackCurrency = false;
+$wgAmazonGatewayNotifyOnConvert = false;
 
 /** Adyen **/
 $wgAdyenGatewayURL = 'https://testorwhatever.adyen.com';
diff --git a/tests/phpunit/includes/MockAmazonClient.php 
b/tests/phpunit/includes/MockAmazonClient.php
deleted file mode 100644
index a6bc078..0000000
--- a/tests/phpunit/includes/MockAmazonClient.php
+++ /dev/null
@@ -1,145 +0,0 @@
-<?php
-
-/**
- * FIXME: Jenkins mwext-testextension-hhvm still not installing 
DonationInterface composer deps
- * use PayWithAmazon\PaymentsClientInterface;
- */
-
-/**
- * Stubs out the functionality of the Client class from the Login and Pay with
- * Amazon SDK.  TODO: replace with PHPUnit method return mocks when Jenkins
- * is running new enough PHPUnit.  Only mocking the stuff we use.
- */
-class MockAmazonClient { // FIXME: implements PaymentsClientInterface {
-
-       // Each key is a method name whose value is an array of times it's been
-       // called, recording all argument values.
-       public $calls = array();
-
-       // Keys are method names, values are arrays of error codes such as 
InvalidPaymentMethod
-       // When a code is not found, the operation will return a successful 
result
-       public $returns = array();
-
-       // Similar to $returns, but any entries here are thrown as exceptions
-       public $exceptions = array();
-
-       public function __construct( $config = null ) {
-       }
-
-       public function __get( $name ) {
-       }
-
-       protected function fakeCall( $functionName, $arguments ) {
-               $this->calls[$functionName][] = $arguments;
-               $status = null;
-               $returnIndex = count( $this->calls[$functionName] ) - 1;
-               if ( isset( $this->returns[$functionName] ) && isset( 
$this->returns[$functionName][$returnIndex] ) ) {
-                       $status = $this->returns[$functionName][$returnIndex];
-               }
-               if ( isset( $this->exceptions[$functionName] ) && isset( 
$this->exceptions[$functionName][$returnIndex] ) ) {
-                       throw $this->exceptions[$functionName][$returnIndex];
-               }
-               return new MockAmazonResponse( $functionName, $status );
-       }
-
-       public function authorize( $requestParameters = array() ) {
-               return $this->fakeCall( 'authorize', $requestParameters );
-       }
-
-       public function authorizeOnBillingAgreement( $requestParameters = 
array() ) {
-               return $this->fakeCall( 'authorizeOnBillingAgreement', 
$requestParameters );
-       }
-
-       public function cancelOrderReference( $requestParameters = array() ) {
-       }
-
-       public function capture( $requestParameters = array() ) {
-       }
-
-       public function charge( $requestParameters = array() ) {
-       }
-
-       public function closeAuthorization( $requestParameters = array() ) {
-       }
-
-       public function closeBillingAgreement( $requestParameters = array() ) {
-       }
-
-       public function closeOrderReference( $requestParameters = array() ) {
-               return $this->fakeCall( 'closeOrderReference', 
$requestParameters );
-       }
-
-       public function confirmBillingAgreement( $requestParameters = array() ) 
{
-               return $this->fakeCall( 'confirmBillingAgreement', 
$requestParameters );
-       }
-
-       public function confirmOrderReference( $requestParameters = array() ) {
-               return $this->fakeCall( 'confirmOrderReference', 
$requestParameters );
-       }
-
-       public function createOrderReferenceForId( $requestParameters = array() 
) {
-       }
-
-       public function getAuthorizationDetails( $requestParameters = array() ) 
{
-       }
-
-       public function getBillingAgreementDetails( $requestParameters = 
array() ) {
-               return $this->fakeCall( 'getBillingAgreementDetails', 
$requestParameters );
-       }
-
-       public function getCaptureDetails( $requestParameters = array() ) {
-               return $this->fakeCall( 'getCaptureDetails', $requestParameters 
);
-       }
-
-       public function getOrderReferenceDetails( $requestParameters = array() 
) {
-               return $this->fakeCall( 'getOrderReferenceDetails', 
$requestParameters );
-       }
-
-       public function getParameters() {
-       }
-
-       public function getProviderCreditDetails( $requestParameters = array() 
) {
-       }
-
-       public function getProviderCreditReversalDetails( $requestParameters = 
array() ) {
-       }
-
-       public function getRefundDetails( $requestParameters = array() ) {
-       }
-
-       public function getServiceStatus( $requestParameters = array() ) {
-       }
-
-       public function getUserInfo( $access_token ) {
-       }
-
-       public function refund( $requestParameters = array() ) {
-       }
-
-       public function reverseProviderCredit( $requestParameters = array() ) {
-       }
-
-       public function setBillingAgreementDetails( $requestParameters = 
array() ) {
-               return $this->fakeCall( 'setBillingAgreementDetails', 
$requestParameters );
-       }
-
-       public function setClientId( $value ) {
-       }
-
-       public function setMwsServiceUrl( $url ) {
-       }
-
-       public function setOrderReferenceDetails( $requestParameters = array() 
) {
-               return $this->fakeCall( 'setOrderReferenceDetails', 
$requestParameters );
-       }
-
-       public function setProxy( $proxy ) {
-       }
-
-       public function setSandbox( $value ) {
-       }
-
-       public function validateBillingAgreement( $requestParameters = array() 
) {
-       }
-
-}
diff --git a/tests/phpunit/includes/MockAmazonResponse.php 
b/tests/phpunit/includes/MockAmazonResponse.php
deleted file mode 100644
index bce94d4..0000000
--- a/tests/phpunit/includes/MockAmazonResponse.php
+++ /dev/null
@@ -1,42 +0,0 @@
-<?php
-
-/**
- * FIXME: Jenkins still not installing DonationInterface composer deps
- * use PayWithAmazon\ResponseInterface as PwaResponseInterface;
- */
-
-/**
- * Stubs out the functionality of the ResponseParser from the Login and Pay 
with
- * Amazon SDK.  TODO: replace with PHPUnit method return mocks when Jenkins
- * is running new enough PHPUnit.  We only use toArray.
- */
-class MockAmazonResponse { // FIXME: implements PwaResponseInterface {
-
-       protected $response;
-
-       /**
-        * Creates the fake response from JSON in 
tests/includes/Responses/amazon
-        * @param string $operation The PwaClient function call we're faking
-        * @param string $status Set to fake responses with an error status
-        *              Reads from $operation_$status.json
-        */
-       public function __construct( $operation, $status = null ) {
-               $statusPart = $status ? '_' . $status : '';
-               $filePath = __DIR__ . 
"/Responses/amazon/{$operation}{$statusPart}.json";
-               $json = file_get_contents( $filePath );
-               $this->response = json_decode( $json, true );
-       }
-
-       public function getBillingAgreementDetailsStatus( $response ) {
-       }
-
-       public function toArray() {
-               return $this->response;
-       }
-
-       public function toJson() {
-       }
-
-       public function toXml() {
-       }
-}
diff --git a/tests/phpunit/includes/test_gateway/TestingAmazonAdapter.php 
b/tests/phpunit/includes/test_gateway/TestingAmazonAdapter.php
deleted file mode 100644
index 2671872..0000000
--- a/tests/phpunit/includes/test_gateway/TestingAmazonAdapter.php
+++ /dev/null
@@ -1,14 +0,0 @@
-<?php
-
-/**
- * TestingAmazonAdapter
- */
-class TestingAmazonAdapter extends AmazonAdapter {
-       use TTestingAdapter;
-
-       public static $mockClient;
-
-       protected function getPwaClient() {
-               return self::$mockClient;
-       }
-}

-- 
To view, visit https://gerrit.wikimedia.org/r/390331
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib4d93306293dd0fe32785a27572f49cb84754b5a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Ejegg <ej...@ejegg.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to