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

tests WIP

parent b166372d
Branches
Tags
1 merge request!1526.6 to master
......@@ -33,6 +33,16 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
*/
public function __construct($mode, $paymentProcessor) {
$this->_paymentProcessor = $paymentProcessor;
if (defined('STRIPE_PHPUNIT_TEST') && isset($GLOBALS['mockStripeClient'])) {
// When under test, prefer the mock.
$this->stripeClient = $GLOBALS['mockStripeClient'];
}
else {
// Normally we create a new stripe client.
$this->stripeClient = new \Stripe\StripeClient(self::getSecretKey($this->_paymentProcessor));
}
}
/**
......@@ -209,8 +219,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
\Stripe\Stripe::setAppInfo('CiviCRM', CRM_Utils_System::version(), CRM_Utils_System::baseURL());
\Stripe\Stripe::setApiKey(self::getSecretKey($this->_paymentProcessor));
\Stripe\Stripe::setApiVersion(CRM_Stripe_Check::API_VERSION);
$this->stripeClient = new \Stripe\StripeClient(self::getSecretKey($this->_paymentProcessor));
}
/**
......@@ -560,7 +568,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// This is where we save the customer card
// @todo For a recurring payment we have to save the card. For a single payment we'd like to develop the
// save card functionality but should not save by default as the customer has not agreed.
$paymentMethod = $this->stripeClient->paymentMethods->retrieve($propertyBag->getCustomProperty('paymentMethodID'));
$paymentMethodID = $propertyBag->getCustomProperty('paymentMethodID');
$paymentMethod = $this->stripeClient->paymentMethods->retrieve($paymentMethodID);
$paymentMethod->attach(['customer' => $stripeCustomer->id]);
$stripeCustomer = $this->stripeClient->customers->retrieve($stripeCustomer->id);
......@@ -724,6 +733,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// Update the recurring payment
civicrm_api3('ContributionRecur', 'create', $recurParams);
// artfulrobot: Q. what do we normally get here?
if ($stripeSubscription->latest_invoice) {
// Get the paymentIntent for the latest invoice
$intent = $stripeSubscription->latest_invoice['payment_intent'];
......@@ -1207,4 +1217,16 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
return $text;
}
/*
* Sets a mock stripe client object for this object and all future created
* instances. This should only be called by phpunit tests.
*
* Nb. cannot change other already-existing instances.
*/
public function setMockStripeClient($stripeClient) {
if (!defined('STRIPE_PHPUNIT_TEST')) {
throw new \RuntimeException("setMockStripeClient was called while not in a STRIPE_PHPUNIT_TEST");
}
$GLOBALS['mockStripeClient'] = $this->stripeClient = $stripeClient;
}
}
......@@ -141,7 +141,7 @@ class CRM_Stripe_Customer {
$stripeCustomerParams = self::getStripeCustomerMetadata($params);
try {
$stripeCustomer = \Stripe\Customer::create($stripeCustomerParams);
$stripeCustomer = $stripe->stripeClient->customers->create($stripeCustomerParams);
}
catch (Exception $e) {
$err = CRM_Core_Payment_Stripe::parseStripeException('create_customer', $e, FALSE);
......
......@@ -316,6 +316,7 @@ function civicrm_api3_stripe_Listevents($params) {
// Query the last month of Stripe events.
elseif ($source == 'stripe') {
// Here we need to get a singleton xxx
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['ppid']]));
$processor->setAPIParams();
$args = [];
......
......@@ -29,7 +29,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
// This test is particularly dirty for some reason so we have to
// force a reset.
public function setUpHeadless() {
$force = TRUE;
$force = false;
return \Civi\Test::headless()
->install('mjwshared')
->installMe(__DIR__)
......@@ -41,6 +41,109 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
* update it after creation. The membership should also be updated.
*/
public function testIPNContribution() {
// Mock the Stripe API. xxx
$this->assertInstanceOf('CRM_Core_Payment_Stripe', $this->paymentObject);
// Create a mock stripe client.
$stripeClient = $this->createMock('Stripe\\StripeClient');
// Update our CRM_Core_Payment_Stripe object and ensure any others
// instantiated separately will also use it.
$this->paymentObject->setMockStripeClient($stripeClient);
// Mock the payment methods service.
$mockPaymentMethod = $this->createMock('Stripe\\PaymentMethod');
$mockPaymentMethod->method('__get')
->will($this->returnValueMap([
[ 'id', 'PM_ID_MOCK']
]));
$stripeClient->paymentMethods = $this->createMock('Stripe\\Service\\PaymentMethodService');
// When create called, return something with an ID.
$stripeClient->paymentMethods
->method('create')
->willReturn($mockPaymentMethod);
// new PropertySpy('paymentMethod.create', ['id' => 'PM_ID_MOCK']));
$stripeClient->paymentMethods
->method('retrieve')
->willReturn($mockPaymentMethod);
// Mock the Customers service
$stripeClient->customers = $this->createMock('Stripe\\Service\\CustomerService');
$stripeClient->customers
->method('create')
->willReturn(
new PropertySpy('customers.create', ['id' => 'CU_ID_MOCK'])
);
$stripeClient->customers
->method('retrieve')
->willReturn(
new PropertySpy('customers.create', ['id' => 'CU_ID_MOCK'])
);
$mockPlan = $this->createMock('Stripe\\Plan');
$stripeClient->plans = $this->createMock('Stripe\\Service\\PlanService');
$stripeClient->plans
->method('retrieve')
->willReturn($mockPlan);
// $stripeSubscription = $this->stripeClient->subscriptions->create($subscriptionParams);
// Need a mock intent with id and status, and
$mockCharge = $this->createMock('Stripe\\Charge');
$mockCharge
->method('__get')
->will($this->returnValueMap([
['id', 'CH_ID_MOCK'],
['balance_transaction', NULL],
]));
$mockPaymentIntent = $this->createMock('Stripe\\PaymentIntent');
$mockPaymentIntent
->method('__get')
->will($this->returnValueMap([
['id', 'PI_ID_MOCK'],
['status', 'requires_capture'],
['charges', (object) ['data' => [ $mockCharge ]]]
]));
$stripeClient->subscriptions = $this->createMock('Stripe\\Service\\SubscriptionService');
$stripeClient->subscriptions
->method('create')
->willReturn(new PropertySpy('subscription.create', [
'id' => 'SB_ID_MOCK',
'latest_invoice' => ['id' => 'IN_ID_MOCK', 'payment_intent' => $mockPaymentIntent],
'pending_setup_intent' => '',
]));
$stripeClient->balanceTransactions = $this->createMock('Stripe\\Service\\BalanceTransactionService');
$stripeClient->balanceTransactions
->method('retrieve')
->willReturn(new PropertySpy('balanceTransaction', [
'fee' => 1,
'currency' => 'USD',
'exchange_rate' => 1,
]));
$stripeClient->paymentIntents = $this->createMock('Stripe\\Service\\PaymentIntentService');
// todo change the status from requires_capture to ?
//$stripeClient->paymentIntents ->method('update') ->willReturn();
$stripeClient->invoices = $this->createMock('Stripe\\Service\\InvoiceService');
$stripeClient->invoices
->method('all')
->willReturn(['data' => (object) ['charge' => 'CH_ID_MOCK', 'created' => date('Y-m-d H:i:s')]]);
//$invoices = $processor->stripeClient->invoices->all(['subscription' => $subscription]);
// Mock Event service.
$stripeClient->events = $this->createMock('Stripe\\Service\\EventService');
// @todo create Mock events.
$stripeClient->events
->method('all')
->willReturn(['data' => [$mockEvent]]);
// Setup a recurring contribution for $200 per month.
$this->setupRecurringTransaction();
......@@ -60,6 +163,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->assertEquals(2, $status_id);
// Now check to see if an event was triggered and if so, process it.
// xxx
$payment_object = $this->getEvent('invoice.payment_succeeded');
if ($payment_object) {
$this->ipn($payment_object);
......@@ -153,3 +257,19 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
}
}
class PropertySpy {
protected $_name;
protected $_props;
public function __construct($name, $props) {
$this->_name = $name;
$this->_props = $props;
}
public function __get($prop) {
if (array_key_exists($prop, $this->_props)) {
return $this->_props[$prop];
}
print "$this->_name property '$prop' requested\n";
return NULL;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment