jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/338303 )

Change subject: Move Ingenico base API wrapper functions to own class
......................................................................


Move Ingenico base API wrapper functions to own class

Tests for higher level functions shouldn't have to mock all the way
down at the cURL response level.

Bug: T158374
Change-Id: Icafa897a0a79430170a35b5c85427527132e5baf
---
A PaymentProviders/Ingenico/Api.php
M PaymentProviders/Ingenico/BankPaymentProvider.php
M PaymentProviders/Ingenico/IngenicoPaymentProvider.php
A PaymentProviders/Ingenico/Tests/phpunit/ApiTest.php
M SmashPig.yaml
5 files changed, 138 insertions(+), 49 deletions(-)

Approvals:
  jenkins-bot: Verified
  Awight: Looks good to me, approved



diff --git a/PaymentProviders/Ingenico/Api.php 
b/PaymentProviders/Ingenico/Api.php
new file mode 100644
index 0000000..2ef21d2
--- /dev/null
+++ b/PaymentProviders/Ingenico/Api.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace SmashPig\PaymentProviders\Ingenico;
+
+use DateTime;
+use DateTimeZone;
+use SmashPig\Core\Context;
+use SmashPig\Core\Http\OutboundRequest;
+
+/**
+ * Prepares and sends requests to the Ingenico Connect API.
+ */
+class Api {
+
+       const API_VERSION = 'v1';
+
+       /**
+        * @var Authenticator
+        */
+       protected $authenticator;
+       protected $baseUrl;
+       protected $merchantId;
+
+       public function __construct( $baseUrl, $merchantId ) {
+               // Ensure trailing slash
+               if ( substr( $baseUrl, -1 ) !== '/' ) {
+                       $baseUrl .= '/';
+               }
+               $this->baseUrl = $baseUrl;
+               $this->merchantId = $merchantId;
+               // FIXME: provide objects in constructor
+               $config = Context::get()->getConfiguration();
+               $this->authenticator = $config->object( 'authenticator' );
+       }
+
+       public function makeApiCall( $path, $method = 'POST', $data = null ) {
+               if ( is_array( $data ) ) {
+                       // FIXME: this is weird, maybe OutboundRequest should 
handle this part
+                       if ( $method === 'GET' ) {
+                               $path .= '?' . http_build_query( $data );
+                               $data = null;
+                       } else {
+                               $data = json_encode( $data );
+                       }
+               }
+               $url = $this->baseUrl . self::API_VERSION . 
"/{$this->merchantId}/$path";
+               $request = new OutboundRequest( $url, $method );
+               $request->setBody( $data );
+               if ( $method !== 'GET' ) {
+                       $request->setHeader( 'Content-Type', 'application/json' 
);
+               }
+               // Set date header manually so we can use it in signature 
generation
+               $date = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
+               $request->setHeader( 'Date', $date->format( 'D, d M Y H:i:s T' 
) );
+
+               // set more headers...
+
+               $this->authenticator->signRequest( $request );
+
+               $response = $request->execute();
+               // TODO error handling
+               return $response;
+       }
+}
diff --git a/PaymentProviders/Ingenico/BankPaymentProvider.php 
b/PaymentProviders/Ingenico/BankPaymentProvider.php
index 9bd376b..67bda0b 100644
--- a/PaymentProviders/Ingenico/BankPaymentProvider.php
+++ b/PaymentProviders/Ingenico/BankPaymentProvider.php
@@ -50,7 +50,7 @@
                                'currencyCode' => $currency
                        );
                        $path = "products/$productId/directory";
-                       $response = $this->makeApiCall( $path, 'GET', $query );
+                       $response = $this->api->makeApiCall( $path, 'GET', 
$query );
 
                        // TODO: api class should probably decode
                        $decoded = json_decode( $response['body'] );
diff --git a/PaymentProviders/Ingenico/IngenicoPaymentProvider.php 
b/PaymentProviders/Ingenico/IngenicoPaymentProvider.php
index 85e2c9a..9fe7ad2 100644
--- a/PaymentProviders/Ingenico/IngenicoPaymentProvider.php
+++ b/PaymentProviders/Ingenico/IngenicoPaymentProvider.php
@@ -2,12 +2,7 @@
 
 namespace SmashPig\PaymentProviders\Ingenico;
 
-use DateTime;
-use DateTimeZone;
 use SmashPig\Core\Context;
-use SmashPig\Core\Configuration;
-use SmashPig\Core\Http\OutboundRequest;
-use SmashPig\Core\UtcDate;
 
 /**
  * Base class for Ingenico payments. Each payment product group should get
@@ -15,48 +10,12 @@
  */
 abstract class IngenicoPaymentProvider {
 
-       const API_VERSION = 'v1';
-       /**
-        * @var Configuration
-        */
+       protected $api;
        protected $config;
 
        public function __construct( $options = array() ) {
+               // FIXME: provide objects in constructor
                $this->config = Context::get()->getConfiguration();
-       }
-
-       protected function makeApiCall( $path, $method = 'POST', $data = null ) 
{
-               if ( is_array( $data ) ) {
-                       // FIXME: this is weird, maybe OutboundRequest should 
handle this part
-                       if ( $method === 'GET' ) {
-                               $path .= '?' . http_build_query( $data );
-                               $data = null;
-                       } else {
-                               $data = json_encode( $data );
-                       }
-               }
-               $base = $this->config->val( 'base-url' );
-               if ( substr( $base, -1 ) !== '/' ) {
-                       $base .= '/';
-               }
-               $merchantId = $this->config->val( 'credentials/merchant-id' );
-               $url = $base . self::API_VERSION . "/$merchantId/$path";
-               $request = new OutboundRequest( $url, $method );
-               $request->setBody( $data );
-               if ( $method !== 'GET' ) {
-                       $request->setHeader( 'Content-Type', 'application/json' 
);
-               }
-               // Set date header manually so we can use it in signature 
generation
-               $date = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
-               $request->setHeader( 'Date', $date->format( 'D, d M Y H:i:s T' 
) );
-
-               // set more headers...
-
-               $authenticator = $this->config->object( 'authenticator' );
-               $authenticator->signRequest( $request );
-
-               $response = $request->execute();
-               // TODO error handling
-               return $response;
+               $this->api = $this->config->object( 'api' );
        }
 }
diff --git a/PaymentProviders/Ingenico/Tests/phpunit/ApiTest.php 
b/PaymentProviders/Ingenico/Tests/phpunit/ApiTest.php
new file mode 100644
index 0000000..6944ed7
--- /dev/null
+++ b/PaymentProviders/Ingenico/Tests/phpunit/ApiTest.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace SmashPig\PaymentProviders\Ingenico\Tests;
+
+use DateTime;
+use PHPUnit_Framework_MockObject_MockObject;
+use SmashPig\Core\Http\OutboundRequest;
+use SmashPig\PaymentProviders\Ingenico\Api;
+use SmashPig\PaymentProviders\Ingenico\Authenticator;
+use SmashPig\Tests\BaseSmashPigUnitTestCase;
+
+/**
+ * @group Ingenico
+ */
+class ApiTest extends BaseSmashPigUnitTestCase {
+
+       /**
+        * @var PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $curlWrapper;
+
+       /**
+        * @var Authenticator
+        */
+       protected $authenticator;
+
+       /**
+        * @var Api
+        */
+       protected $api;
+
+       public function setUp() {
+               parent::setUp();
+               $config = $this->setConfig( 'ingenico' );
+               $this->curlWrapper = $this->getMock( 
'\SmashPig\Core\Http\CurlWrapper' );
+               $config->overrideObjectInstance( 'curl/wrapper', 
$this->curlWrapper );
+               $this->authenticator = new Authenticator(
+                       '5e45c937b9db33ae',
+                       'I42Zf4pVnRdroHfuHnRiJjJ2B6+22h0yQt/R3nZR8Xg='
+               );
+               $this->api = new Api(
+                       'https://example.com',
+                       '9876'
+               );
+       }
+
+       public function testCreateRequest() {
+               $headerVerification = function( $headers ) {
+                       $date = new DateTime( $headers['Date'] );
+                       return $date !== null &&
+                               $headers['Content-Type'] === 'application/json';
+               };
+
+               $this->curlWrapper->expects( $this->once() )
+                       ->method( 'execute' )
+                       ->with(
+                               $this->equalTo( 
'https://example.com/v1/9876/testPath' ),
+                               $this->equalTo( 'POST' ),
+                               $this->callback( $headerVerification ),
+                               $this->equalTo( '{"foo":"bar"}' )
+                       );
+
+               $this->api->makeApiCall( 'testPath', 'POST', array( 'foo' => 
'bar' ) );
+       }
+}
diff --git a/SmashPig.yaml b/SmashPig.yaml
index dd1a2f3..520c901 100644
--- a/SmashPig.yaml
+++ b/SmashPig.yaml
@@ -444,10 +444,11 @@
         listener:
             class: 
SmashPig\PaymentProviders\GlobalCollect\GlobalCollectListener
 
-    base-url: 'https://api-sandbox.globalcollect.com/'
-
-    credentials:
-        merchant-id: 1234
+    api:
+        class: SmashPig\PaymentProviders\Ingenico\Api
+        constructor-parameters:
+            - 'https://api-sandbox.globalcollect.com/'
+            - 1234 # numeric merchant ID
 
     authenticator:
         class: SmashPig\PaymentProviders\Ingenico\Authenticator

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Icafa897a0a79430170a35b5c85427527132e5baf
Gerrit-PatchSet: 3
Gerrit-Project: wikimedia/fundraising/SmashPig
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Cdentinger <[email protected]>
Gerrit-Reviewer: Eileen <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: Pcoombe <[email protected]>
Gerrit-Reviewer: XenoRyet <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to