Skip to content
Snippets Groups Projects
Commit 5c0d8c45 authored by Rich's avatar Rich Committed by mattwire
Browse files

Refactor: move payment object into a property of the test class to enable mocking of Stripe API

parent 9867bbc6
No related branches found
No related tags found
1 merge request!1526.6 to master
...@@ -23,15 +23,27 @@ define('STRIPE_PHPUNIT_TEST', 1); ...@@ -23,15 +23,27 @@ define('STRIPE_PHPUNIT_TEST', 1);
*/ */
class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
/** @var int */
protected $contributionID; protected $contributionID;
/** @var int */
protected $financialTypeID = 1; protected $financialTypeID = 1;
/** @var array */
protected $contact; protected $contact;
/** @var int */
protected $contactID; protected $contactID;
/** @var int */
protected $paymentProcessorID; protected $paymentProcessorID;
/** @var array of payment processor configuration values */
protected $paymentProcessor; protected $paymentProcessor;
/** @var CRM_Core_Payment_Stripe */
protected $paymentObject;
/** @var string */
protected $trxn_id; protected $trxn_id;
/** @var string */
protected $processorID; protected $processorID;
/** @var string */
protected $cc = '4111111111111111'; protected $cc = '4111111111111111';
/** @var string */
protected $total = '400.00'; protected $total = '400.00';
public function setUpHeadless() { public function setUpHeadless() {
...@@ -87,13 +99,13 @@ class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements Headles ...@@ -87,13 +99,13 @@ class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements Headles
$processor = array_pop($result['values']); $processor = array_pop($result['values']);
$this->paymentProcessor = $processor; $this->paymentProcessor = $processor;
$this->paymentProcessorID = $result['id']; $this->paymentProcessorID = $result['id'];
$this->paymentObject = new CRM_Core_Payment_Stripe('test' /*mode*/, $this->paymentProcessor);
} }
/** /**
* Submit to stripe * Submit to stripe
*/ */
public function doPayment($params = []) { public function doPayment($params = []) {
$mode = 'test';
\Stripe\Stripe::setApiKey(CRM_Core_Payment_Stripe::getSecretKey($this->paymentProcessor)); \Stripe\Stripe::setApiKey(CRM_Core_Payment_Stripe::getSecretKey($this->paymentProcessor));
...@@ -128,7 +140,6 @@ class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements Headles ...@@ -128,7 +140,6 @@ class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements Headles
$paymentMethodID = $paymentMethod->id; $paymentMethodID = $paymentMethod->id;
} }
$stripe = new CRM_Core_Payment_Stripe($mode, $this->paymentProcessor);
$params = array_merge([ $params = array_merge([
'payment_processor_id' => $this->paymentProcessorID, 'payment_processor_id' => $this->paymentProcessorID,
'amount' => $this->total, 'amount' => $this->total,
...@@ -145,7 +156,7 @@ class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements Headles ...@@ -145,7 +156,7 @@ class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements Headles
'additional_participants' => [], 'additional_participants' => [],
], $params); ], $params);
$ret = $stripe->doPayment($params); $ret = $this->paymentObject->doPayment($params);
if (array_key_exists('trxn_id', $ret)) { if (array_key_exists('trxn_id', $ret)) {
$this->trxn_id = $ret['trxn_id']; $this->trxn_id = $ret['trxn_id'];
...@@ -169,6 +180,7 @@ class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements Headles ...@@ -169,6 +180,7 @@ class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implements Headles
public function assertValidTrxn() { public function assertValidTrxn() {
$this->assertNotEmpty($this->trxn_id, "A trxn id was assigned"); $this->assertNotEmpty($this->trxn_id, "A trxn id was assigned");
// @todo Consider mock this, though currently only used by Direct Test, so maybe not.
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $this->paymentProcessorID])); $processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $this->paymentProcessorID]));
$processor->setAPIParams(); $processor->setAPIParams();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment